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",
"streambuf": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp"
"typeinfo": "cpp",
"thread": "cpp"
}
}

@ -5,6 +5,7 @@
#include <string>
#include <vector>
#include <thread>
#include <mutex>
#include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
@ -25,26 +26,19 @@ class client;
* Server klasa za TCP/IP soket
* Instanca se incijalizira kada pokrećemo server
*/
// template<typename... Args>
class server {
public:
int sock;
struct sockaddr_in addr;
SSL_CTX* securefds = NULL;
vector<thread> thr;
server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL);
~server ();
// one klijent
client* cli = NULL;
// 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);
void sync(void (*handlecli)(client&), const uint timeout = 100);
void async(const uint limit, void (*handlecli)(client&, mutex&), 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 {
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<typename... Args>
// void server::async(const uint limit, void (*handlecli)(Args ...args), const uint timeout) {
// do {
// for (uint i=0; i<limit; i++) {
// clis.push_back(new client(this, timeout, securefds));
// thr.push_back(thread(handlecli, args...));
// }
/**
* Metoda za asinkdorni rad s klijentima, prima limit, 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;
*/
// for (uint i=0; i<limit; i++) {
// thr[i].join();
// clis[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<limit; i++) {
thr.push_back(thread([&](){
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 () {
cli->~client();
cli = NULL;
if (sock<=0) {
throw string("[ERROR] The socket is already closed ");
}

@ -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;

Binary file not shown.
Loading…
Cancel
Save