diff --git a/.vscode/settings.json b/.vscode/settings.json index adb4a79..11e5997 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -44,6 +44,7 @@ "stdexcept": "cpp", "streambuf": "cpp", "cinttypes": "cpp", - "typeinfo": "cpp" + "typeinfo": "cpp", + "thread": "cpp" } } \ No newline at end of file diff --git a/lib/tcp_socket.hpp b/lib/tcp_socket.hpp index cb12596..e38eac1 100644 --- a/lib/tcp_socket.hpp +++ b/lib/tcp_socket.hpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -25,26 +26,19 @@ class client; * Server klasa za TCP/IP soket * Instanca se incijalizira kada pokrećemo server */ -// template - class server { public: int sock; struct sockaddr_in addr; SSL_CTX* securefds = NULL; + vector thr; server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL); ~server (); - // one klijent - client* cli = NULL; - // template - void sync(const uint timeout = 100, void (*func)(Args ...vars)); - - // vector thr; - // vector clis; - // void async(const uint limit, void (*handlecli)(Args ...args) , 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); }; diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 74467fb..90644b0 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -32,36 +32,42 @@ server::server (const ushort port, const uint queue, SSL_CTX* _securefds) { } -template -void server::sync(const uint timeout, void (*func)(Args ...vars)) { +/** + * Metoda za sinkroni rad s klijentima, prima pokazivač na funkciju i timeout; + * Funkcija handlecli prima referencu tipa client - važno za definiranje funkcija koje se šalju; + * Nije moguće proslijediti druge parametre; +*/ + +void server::sync(void (*handlecli)(client&), const uint timeout) { do { - if (cli != NULL) { - cli->~client(); - cli = NULL; - } - cli = new client(this, timeout, securefds); - // callback - func(); + client cli(this, timeout, securefds); + handlecli(cli); } while (true); } -// template -// void server::async(const uint limit, void (*handlecli)(Args ...args), const uint timeout) { -// do { -// for (uint i=0; i~client(); -// } -// thr.clear(); -// clis.clear(); +void server::async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout) { + mutex io; + do { + for (uint i=0; i~client(); - cli = NULL; - if (sock<=0) { throw string("[ERROR] The socket is already closed "); } diff --git a/test/server.cpp b/test/server.cpp index d8c0eb6..58d0edc 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -41,19 +41,28 @@ int main() { server myserver(5000, 100); - // myserver.async(8, [](client *cli) { - // cout << "Klijent " << cli->ipv4 << endl; - // string fromclient = cli->pull(); - // cout << "S klijenta " << fromclient << endl; - // cli->push(fromclient); - // }, 200); - myserver.sync(8, [](client *cli) { - cout << "Klijent " << cli->ipv4 << endl; - string fromclient = cli->pull(); + myserver.async(8, [](client &cli, mutex &io) { + cout << "Klijent " << cli.ipv4 << endl; + string fromclient = cli.pull(); + io.lock(); cout << "S klijenta " << fromclient << endl; - cli->push(fromclient); + io.unlock(); + // fromclient += teststr; + cli.push(fromclient); }, 200); + // string teststr = " Idemooo"; + + // myserver.sync([](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); + // }); + } catch(const string err) { cout << err << endl; diff --git a/test/server.o b/test/server.o index 5a4e551..65bc4f7 100755 Binary files a/test/server.o and b/test/server.o differ