diff --git a/lib/tcp_socket.hpp b/lib/tcp_socket.hpp index ce5261f..cb12596 100644 --- a/lib/tcp_socket.hpp +++ b/lib/tcp_socket.hpp @@ -25,6 +25,8 @@ class client; * Server klasa za TCP/IP soket * Instanca se incijalizira kada pokrećemo server */ +// template + class server { public: @@ -32,16 +34,17 @@ class server { struct sockaddr_in addr; SSL_CTX* securefds = NULL; - server (const ushort port, const uint limit = 1000, SSL_CTX* _securefds = NULL); + server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL); ~server (); // one klijent client* cli = NULL; - void accept(const uint timeout = 100); + // template + void sync(const uint timeout = 100, void (*func)(Args ...vars)); - vector thr; - vector clis; - void asyncli(const uint limit, void (*handlecli)(client*) , const uint timeout = 100); + // vector thr; + // vector clis; + // void async(const uint limit, void (*handlecli)(Args ...args) , const uint timeout = 100); }; diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 5cdf4e9..74467fb 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -4,9 +4,8 @@ * Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju */ -server::server (const ushort port, const uint limit, SSL_CTX* _securefds) { - securefds = _securefds; // dodati parametar red čekanja queue koji će se koristiti kao limit u socketu - // a stavrni limit ćemo korisitit kao broj threadova +server::server (const ushort port, const uint queue, SSL_CTX* _securefds) { + securefds = _securefds; addr.sin_family = AF_INET; addr.sin_addr.s_addr = INADDR_ANY; @@ -27,38 +26,43 @@ server::server (const ushort port, const uint limit, SSL_CTX* _securefds) { throw string("[ERROR] Unable to bind socket "); } - if (listen(sock, limit) < 0) { + if (listen(sock, queue) < 0) { throw string("[ERROR] It is not possible to set the allowed number of waiting clients "); } } -void server::accept(const uint timeout) { - if (cli != NULL) { - cli->~client(); - cli = NULL; - } - cli = new client(this, timeout, securefds); -} - - -void server::asyncli(const uint limit, void (*handlecli)(client*), const uint timeout) { +template +void server::sync(const uint timeout, void (*func)(Args ...vars)) { do { - for (uint i=0; i~client(); + cli = NULL; } - - for (uint i=0; i~client(); - } - thr.clear(); - clis.clear(); - + cli = new client(this, timeout, securefds); + // callback + func(); } 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(); + +// } while (true); +// } + /** * Destruktor varijable tipa server diff --git a/test/server.cpp b/test/server.cpp index df8c024..d8c0eb6 100644 --- a/test/server.cpp +++ b/test/server.cpp @@ -41,7 +41,13 @@ int main() { server myserver(5000, 100); - myserver.asyncli(8, [](client *cli) { + // 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(); cout << "S klijenta " << fromclient << endl;