Working on pool..

pool
marcelb 1 year ago
parent 5fa791cf5e
commit 03a8df4042
  1. 4
      .vscode/tasks.json
  2. 30
      src/tcp_socket.cpp
  3. 85
      test/client.cpp
  4. BIN
      test/client.o
  5. 17
      test/server.cpp
  6. BIN
      test/server.o

@ -6,11 +6,11 @@
"command": "/usr/bin/g++", "command": "/usr/bin/g++",
"args": [ "args": [
"-g", "-g",
"${fileDirname}/server.cpp", "${fileDirname}/client.cpp",
"${fileDirname}/../src/*.cpp", "${fileDirname}/../src/*.cpp",
// "${fileDirname}../include/*/src/*.cpp", // "${fileDirname}../include/*/src/*.cpp",
"-o", "-o",
"${fileDirname}/server.o", "${fileDirname}/client.o",
"-lssl", "-lssl",
"-lcrypto" "-lcrypto"
], ],

@ -66,20 +66,28 @@ void server::sync(void (*handlecli)(client&), const uint timeout) {
void server::async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout) { void server::async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout) {
mutex io; mutex io;
do { // do {
for (uint i=0; i<limit; i++) { for (uint i=0; i<limit; i++) {
thr.push_back(thread([&](){ thr.push_back(thread([&](){
client cli(this, timeout, securefds); client *cli = new client(this, timeout, securefds);
handlecli(cli, io); while (true) {
try {
handlecli(*cli, io);
} catch (const string err) {
cout << err << endl;
cli->~client();
cli = new client(this, timeout, securefds);
}
}
})); }));
} }
for (uint i=0; i<limit; i++) { for (uint i=0; i<limit; i++) {
thr[i].join(); thr[i].join();
} }
thr.clear(); // thr.clear();
} while (true); // } while (true);
} }
@ -112,9 +120,15 @@ void server::asyncPool(const uint limit, void (*handlecli)(client&), const uint
for (uint i=0; i<limit; i++) { for (uint i=0; i<limit; i++) {
thr.push_back(thread( [&]() { thr.push_back(thread( [&]() {
pair<mutex*, client*>* cli = clipool.pickup();
while (true) { while (true) {
pair<mutex*, client*>* cli = clipool.pickup(); try {
handlecli(*(cli->second)); handlecli(*(cli->second));
} catch (const string err) {
cout << err << endl;
cli->second->~client();
cli->second = new client(this, timeout, securefds);
}
clipool.release(cli); clipool.release(cli);
} }
})); }));
@ -420,7 +434,7 @@ string client::pull(size_t byte_limit) {
break; break;
} else if (received == 0) { } else if (received == 0) {
// Veza je prekinuta - treba pozvati destruktor // Veza je prekinuta - treba pozvati destruktor
cout << "Destruktor " << endl; // cout << "Destruktor " << endl;
// this->~client(); // this->~client();
throw string("[WARNING] Socket closed remotely"); throw string("[WARNING] Socket closed remotely");
break; break;

@ -27,22 +27,75 @@ int main() {
// thr[i].join(); // thr[i].join();
// } // }
uint i = 0; // uint i = 0;
auto t1 = high_resolution_clock::now(); // auto t1 = high_resolution_clock::now();
client mycli("localhost", 7000); // client mycli("localhost", 7000);
auto t2 = high_resolution_clock::now(); // auto t2 = high_resolution_clock::now();
cout << "Connected: " << duration_cast<microseconds>(t2 - t1).count() << endl; // cout << "Connected: " << duration_cast<microseconds>(t2 - t1).count() << endl;
while (true) { // while (true) {
sleep(4); // t2 = high_resolution_clock::now();
mycli.push("Helllo " + to_string(i++)); // mycli.push("Helllo " + to_string(i++));
cout << "> " << mycli.pull() << endl; // cout << "> " << mycli.pull() << endl;
auto t3 = high_resolution_clock::now(); // auto t3 = high_resolution_clock::now();
cout << "Sending and recive: " << duration_cast<microseconds>(t3 - t2).count() << endl; // cout << "Sending and recive: " << duration_cast<microseconds>(t3 - t2).count() << endl;
// usleep(10000); // // usleep(10000);
sleep(1); // }
}
clientPool clies(5, "localhost", 7000);
thread t1([&]() {
int i = 0;
while (true) {
auto t1 = high_resolution_clock::now();
auto socks = clies.pickup();
auto t2 = high_resolution_clock::now();
cout << "Picking : " << duration_cast<microseconds>(t2 - t1).count() << endl;
socks->second->push("Helllo I " + to_string(i++));
cout << "> " << socks->second->pull() << endl;
auto t3 = high_resolution_clock::now();
cout << "Sending and recive: " << duration_cast<microseconds>(t3 - t2).count() << endl;
clies.release(socks);
}
});
thread t2([&]() {
int i = 0;
while (true) {
auto t1 = high_resolution_clock::now();
auto socks = clies.pickup();
auto t2 = high_resolution_clock::now();
cout << "Picking : " << duration_cast<microseconds>(t2 - t1).count() << endl;
socks->second->push("Helllo II " + to_string(i++));
cout << "> " << socks->second->pull() << endl;
auto t3 = high_resolution_clock::now();
cout << "Sending and recive: " << duration_cast<microseconds>(t3 - t2).count() << endl;
clies.release(socks);
}
});
thread t3([&]() {
int i = 0;
while (true) {
auto t1 = high_resolution_clock::now();
auto socks = clies.pickup();
auto t2 = high_resolution_clock::now();
cout << "Picking : " << duration_cast<microseconds>(t2 - t1).count() << endl;
socks->second->push("Helllo III " + to_string(i++));
cout << "> " << socks->second->pull() << endl;
auto t3 = high_resolution_clock::now();
cout << "Sending and recive: " << duration_cast<microseconds>(t3 - t2).count() << endl;
clies.release(socks);
}
});
t1.join();
t2.join();
t3.join();
// secure crypto; // secure crypto;
// cout << "init cert " << endl; // cout << "init cert " << endl;

Binary file not shown.

@ -80,19 +80,10 @@ int main() {
myserver.async(4, [](client &cli, mutex &io) { myserver.async(4, [](client &cli, mutex &io) {
while (true) { while (true) {
try { string fromclient = cli.pull();
string fromclient = cli.pull(); cout << "> " << fromclient << endl;
cout << "> " << fromclient << endl; // fromclient += teststr;
// fromclient += teststr; cli.push(fromclient);
cli.push(fromclient);
} catch (const string err) {
cout << err << endl;
try {
cli.reconnect();
} catch (const string err) {
cout << err << endl;
}
}
} }
}); });

Binary file not shown.
Loading…
Cancel
Save