Working on pool..

pool
marcelb 12 months ago
parent 5fa791cf5e
commit 03a8df4042
  1. 4
      .vscode/tasks.json
  2. 28
      src/tcp_socket.cpp
  3. 69
      test/client.cpp
  4. BIN
      test/client.o
  5. 9
      test/server.cpp
  6. BIN
      test/server.o

@ -6,11 +6,11 @@
"command": "/usr/bin/g++",
"args": [
"-g",
"${fileDirname}/server.cpp",
"${fileDirname}/client.cpp",
"${fileDirname}/../src/*.cpp",
// "${fileDirname}../include/*/src/*.cpp",
"-o",
"${fileDirname}/server.o",
"${fileDirname}/client.o",
"-lssl",
"-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) {
mutex io;
do {
// do {
for (uint i=0; i<limit; i++) {
thr.push_back(thread([&](){
client cli(this, timeout, securefds);
handlecli(cli, io);
client *cli = new client(this, timeout, securefds);
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++) {
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++) {
thr.push_back(thread( [&]() {
while (true) {
pair<mutex*, client*>* cli = clipool.pickup();
while (true) {
try {
handlecli(*(cli->second));
} catch (const string err) {
cout << err << endl;
cli->second->~client();
cli->second = new client(this, timeout, securefds);
}
clipool.release(cli);
}
}));
@ -420,7 +434,7 @@ string client::pull(size_t byte_limit) {
break;
} else if (received == 0) {
// Veza je prekinuta - treba pozvati destruktor
cout << "Destruktor " << endl;
// cout << "Destruktor " << endl;
// this->~client();
throw string("[WARNING] Socket closed remotely");
break;

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

Binary file not shown.

@ -80,19 +80,10 @@ int main() {
myserver.async(4, [](client &cli, mutex &io) {
while (true) {
try {
string fromclient = cli.pull();
cout << "> " << fromclient << endl;
// fromclient += teststr;
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