diff --git a/.vscode/tasks.json b/.vscode/tasks.json index b2ee7b3..989190b 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -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" ], diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 0b44b6c..6ff06b8 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -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~client(); + cli = new client(this, timeout, securefds); + } + } })); } for (uint i=0; i* cli = clipool.pickup(); while (true) { - pair* cli = clipool.pickup(); - handlecli(*(cli->second)); + 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; diff --git a/test/client.cpp b/test/client.cpp index 21a7745..6f282eb 100644 --- a/test/client.cpp +++ b/test/client.cpp @@ -27,22 +27,75 @@ int main() { // thr[i].join(); // } - uint i = 0; - - auto t1 = high_resolution_clock::now(); - client mycli("localhost", 7000); - auto t2 = high_resolution_clock::now(); - cout << "Connected: " << duration_cast(t2 - t1).count() << endl; - - while (true) { - sleep(4); - mycli.push("Helllo " + to_string(i++)); - cout << "> " << mycli.pull() << endl; - auto t3 = high_resolution_clock::now(); - cout << "Sending and recive: " << duration_cast(t3 - t2).count() << endl; - // usleep(10000); - sleep(1); - } + // uint i = 0; + + // auto t1 = high_resolution_clock::now(); + // client mycli("localhost", 7000); + // auto t2 = high_resolution_clock::now(); + // cout << "Connected: " << duration_cast(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(t3 - t2).count() << endl; + // // usleep(10000); + // } + + 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(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(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(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(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(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(t3 - t2).count() << endl; + clies.release(socks); + } + }); + + + t1.join(); + t2.join(); + t3.join(); // secure crypto; // cout << "init cert " << endl; diff --git a/test/client.o b/test/client.o index 45534e7..1a54071 100755 Binary files a/test/client.o and b/test/client.o differ diff --git a/test/server.cpp b/test/server.cpp index df074c4..2492613 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -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; - } - } + string fromclient = cli.pull(); + cout << "> " << fromclient << endl; + // fromclient += teststr; + cli.push(fromclient); } }); diff --git a/test/server.o b/test/server.o index b2275d5..59682d1 100755 Binary files a/test/server.o and b/test/server.o differ