Working on pool socket support

pool
marcelb 12 months ago
parent 031853d8ee
commit c030b22446
  1. 22
      lib/tcp_socket.hpp
  2. 37
      src/tcp_socket.cpp
  3. 37
      test/client.cpp
  4. BIN
      test/client.o
  5. 2
      test/compile-server.sh
  6. 22
      test/server.cpp
  7. BIN
      test/server.o

@ -53,6 +53,7 @@ class server {
void sync(void (*handlecli)(client&), const uint timeout = 100); void sync(void (*handlecli)(client&), const uint timeout = 100);
void async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100); void async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
void pool(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
}; };
@ -100,6 +101,27 @@ class client {
~client (); ~client ();
bool push (const string msg); bool push (const string msg);
string pull (size_t byte_limit = 1024); string pull (size_t byte_limit = 1024);
/**
* ustvari ne znam jel konekcija aktivna
* kod za connect i disconnect je konstruktoru - destruktoru
*/
};
class Pool {
public:
uint numcli;
vector<pair<mutex*, client*>> drops;
// konstruktor za klijente bez servera
Pool (const uint _numcli, const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL);
// konstruktor za klijente sa serverom
Pool (const server *_srv, const uint _numcli, const uint timeout = 100, SSL_CTX* securefds = NULL);
~Pool();
void run();
}; };
#endif #endif

@ -380,3 +380,40 @@ string client::pull(size_t byte_limit) {
return string(res); return string(res);
} }
Pool::Pool(const uint _numcli, const string address, const ushort port, const uint timeout, SSL_CTX* securefds) {
if (_numcli > 1 ) {
numcli = _numcli;
}
else {
throw string("[ERROR] Invalid number of instances in pool ");
}
for (uint i=0; i<numcli; i++) {
drops.push_back(make_pair(new mutex, new client(address, port, timeout, securefds)));
}
}
Pool::Pool(const server *_srv, const uint _numcli, const uint timeout = 100, SSL_CTX* securefds = NULL) {
if (_numcli > 1 ) {
numcli = _numcli;
}
else {
throw string("[ERROR] Invalid number of instances in pool ");
}
for (uint i=0; i<numcli; i++) {
drops.push_back(make_pair(new mutex, new client(_srv, timeout, securefds)));
}
}
Pool::~Pool() {
numcli = 0;
drops.clear();
}

@ -9,22 +9,31 @@ int main() {
try { try {
uint n = 10000; // uint n = 10000;
vector<thread> thr; // vector<thread> thr;
for (uint i=0; i<n; i++) { // for (uint i=0; i<n; i++) {
thr.push_back(thread([](uint a){ // thr.push_back(thread([](uint a){
client myserver("127.0.0.1", 5000, 500); // client myserver("127.0.0.1", 5000, 500);
string sends = "Hello world " + to_string(a); // string sends = "Hello world " + to_string(a);
myserver.push(sends); // myserver.push(sends);
cout << myserver.pull() << endl; // cout << myserver.pull() << endl;
}, i)); // }, i));
// }
// for (uint i=0; i<n; i++) {
// thr[i].join();
// }
uint i = 0;
client mycli("localhost", 5000);
while (true) {
mycli.push("Helllo " + to_string(i++));
cout << mycli.pull() << endl;
usleep(10000);
} }
for (uint i=0; i<n; i++) {
thr[i].join();
}
// secure crypto; // secure crypto;
// cout << "init cert " << endl; // cout << "init cert " << endl;

Binary file not shown.

@ -1 +1 @@
g++ server.cpp ../src/* -o server.o -lssl -lcrypto g++ server.cpp ../src/* -o server.o -lssl -lcrypto -lpthread

@ -49,16 +49,28 @@ int main() {
// sleep(80); // sleep(80);
cout << "init server " << endl; // cout << "init server " << endl;
// server myserver(5000, 100);
// cout << "init client " << endl;
// myserver.async(8, [](client &cli, mutex &io) {
// cout << "Klijent " << cli.ipv4 << endl;
// string fromclient = cli.pull();
// io.lock();
// cout << "S klijenta " << fromclient << endl;
// io.unlock();
// // fromclient += teststr;
// cli.push(fromclient);
// }, 200);
server myserver(5000, 100); server myserver(5000, 100);
cout << "init client " << endl; myserver.pool(10, [](client &cli, mutex &io) {
myserver.async(8, [](client &cli, mutex &io) {
cout << "Klijent " << cli.ipv4 << endl; cout << "Klijent " << cli.ipv4 << endl;
string fromclient = cli.pull(); string fromclient = cli.pull();
io.lock(); // io.lock();
cout << "S klijenta " << fromclient << endl; cout << "S klijenta " << fromclient << endl;
io.unlock(); // io.unlock();
// fromclient += teststr; // fromclient += teststr;
usleep(10000);
cli.push(fromclient); cli.push(fromclient);
}, 200); }, 200);

Binary file not shown.
Loading…
Cancel
Save