Sync and async server

mc-support v0.4_mc-async
marcelb 1 year ago
parent e0feb8b2da
commit 043d0b26c6
  1. 3
      .vscode/settings.json
  2. 14
      lib/tcp_socket.hpp
  3. 57
      src/tcp_socket.cpp
  4. 29
      test/server.cpp
  5. BIN
      test/server.o

@ -44,6 +44,7 @@
"stdexcept": "cpp", "stdexcept": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"cinttypes": "cpp", "cinttypes": "cpp",
"typeinfo": "cpp" "typeinfo": "cpp",
"thread": "cpp"
} }
} }

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <thread> #include <thread>
#include <mutex>
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
@ -25,26 +26,19 @@ class client;
* Server klasa za TCP/IP soket * Server klasa za TCP/IP soket
* Instanca se incijalizira kada pokrećemo server * Instanca se incijalizira kada pokrećemo server
*/ */
// template<typename... Args>
class server { class server {
public: public:
int sock; int sock;
struct sockaddr_in addr; struct sockaddr_in addr;
SSL_CTX* securefds = NULL; SSL_CTX* securefds = NULL;
vector<thread> thr;
server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL); server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL);
~server (); ~server ();
// one klijent void sync(void (*handlecli)(client&), const uint timeout = 100);
client* cli = NULL; void async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
// template<typename... Args>
void sync(const uint timeout = 100, void (*func)(Args ...vars));
// vector<thread> thr;
// vector<client*> clis;
// void async(const uint limit, void (*handlecli)(Args ...args) , const uint timeout = 100);
}; };

@ -32,36 +32,42 @@ server::server (const ushort port, const uint queue, SSL_CTX* _securefds) {
} }
template<typename... Args> /**
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 { do {
if (cli != NULL) { client cli(this, timeout, securefds);
cli->~client(); handlecli(cli);
cli = NULL;
}
cli = new client(this, timeout, securefds);
// callback
func();
} while (true); } while (true);
} }
// template<typename... Args> /**
// void server::async(const uint limit, void (*handlecli)(Args ...args), const uint timeout) { * Metoda za asinkdorni rad s klijentima, prima limit, pokazivač na funkciju i timeout;
// do { * Funkcija handlecli prima referencu tipa client - važno za definiranje funkcija koje se šalju;
// for (uint i=0; i<limit; i++) { * Nije moguće proslijediti druge parametre;
// clis.push_back(new client(this, timeout, securefds)); */
// thr.push_back(thread(handlecli, args...));
// }
// for (uint i=0; i<limit; i++) { void server::async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout) {
// thr[i].join(); mutex io;
// clis[i]->~client(); do {
// } for (uint i=0; i<limit; i++) {
// thr.clear(); thr.push_back(thread([&](){
// clis.clear(); client cli(this, timeout, securefds);
handlecli(cli, io);
}));
}
for (uint i=0; i<limit; i++) {
thr[i].join();
}
thr.clear();
// } while (true); } while (true);
// } }
/** /**
@ -71,9 +77,6 @@ void server::sync(const uint timeout, void (*func)(Args ...vars)) {
server::~server () { server::~server () {
cli->~client();
cli = NULL;
if (sock<=0) { if (sock<=0) {
throw string("[ERROR] The socket is already closed "); throw string("[ERROR] The socket is already closed ");
} }

@ -41,19 +41,28 @@ int main() {
server myserver(5000, 100); server myserver(5000, 100);
// myserver.async(8, [](client *cli) { myserver.async(8, [](client &cli, mutex &io) {
// cout << "Klijent " << cli->ipv4 << endl; cout << "Klijent " << cli.ipv4 << endl;
// string fromclient = cli->pull(); string fromclient = cli.pull();
// cout << "S klijenta " << fromclient << endl; io.lock();
// cli->push(fromclient);
// }, 200);
myserver.sync(8, [](client *cli) {
cout << "Klijent " << cli->ipv4 << endl;
string fromclient = cli->pull();
cout << "S klijenta " << fromclient << endl; cout << "S klijenta " << fromclient << endl;
cli->push(fromclient); io.unlock();
// fromclient += teststr;
cli.push(fromclient);
}, 200); }, 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) { catch(const string err) {
cout << err << endl; cout << err << endl;

Binary file not shown.
Loading…
Cancel
Save