Support for multiclient (async)

mc-support
marcelb 1 year ago
parent 1f82e94296
commit 7be0716f61
  1. 5
      lib/tcp_socket.hpp
  2. 19
      src/tcp_socket.cpp
  3. 33
      test/client.cpp
  4. BIN
      test/client.o
  5. 46
      test/server.cpp
  6. BIN
      test/server.o

@ -4,6 +4,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <thread>
#include <string.h> #include <string.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
@ -38,6 +39,10 @@ class server {
client* cli; client* cli;
void accept(const uint timeout = 100); void accept(const uint timeout = 100);
vector<thread> thr;
vector<client*> clis;
void asyncli(const uint limit, void (*handlecli)(client*) , const uint timeout = 100);
}; };
/** /**

@ -36,6 +36,25 @@ void server::accept(const uint timeout) {
cli = new client(this, timeout, securefds); cli = new client(this, timeout, securefds);
} }
void server::asyncli(const uint limit, void (*handlecli)(client*), const uint timeout) {
do {
thr.clear();
clis.clear();
for (uint i=0; i<limit; i++) {
clis.push_back(new client(this, timeout, securefds));
thr.push_back(thread(handlecli, clis[clis.size()-1]));
}
for (uint i=0; i<limit; i++) {
thr[i].join();
clis[i]->~client();
}
} while (true);
}
/** /**
* Destruktor varijable tipa server * Destruktor varijable tipa server
*/ */

@ -1,4 +1,5 @@
#include <iostream> #include <iostream>
#include <string>
#include "../lib/tcp_socket.hpp" #include "../lib/tcp_socket.hpp"
@ -8,19 +9,35 @@ int main() {
try { try {
secure crypto; uint n = 10000;
cout << "init cert " << endl;
client myserver("127.0.0.1", 5000, 5000, crypto.fds); vector<thread> thr;
for (uint i=0; i<n; i++) {
thr.push_back(thread([](uint a){
client myserver("127.0.0.1", 5000, 500);
string sends = "Hello world " + to_string(a);
myserver.push(sends);
cout << myserver.pull() << endl;
}, i));
}
for (uint i=0; i<n; i++) {
thr[i].join();
}
// secure crypto;
// cout << "init cert " << endl;
// client myserver("127.0.0.1", 5000, 5000, crypto.fds);
// client myserver("localhost", 5000); // client myserver("localhost", 5000);
cout << "init client " << endl; // cout << "init client " << endl;
string sends = "Hello world!"; // string sends = "Hello world!";
cout << myserver.push(sends) << " " << sends.length() << endl; // cout << myserver.push(sends) << " " << sends.length() << endl;
cout << "wait client " << endl; // cout << "wait client " << endl;
cout << myserver.pull(); // cout << myserver.pull();
} }
catch (const string err) { catch (const string err) {

Binary file not shown.

@ -6,25 +6,25 @@ using namespace std;
int main() { int main() {
try{ try{
cout << "init cert " << endl; // cout << "init cert " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem"); // secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init server " << endl; // cout << "init server " << endl;
server myserver(5000, 100, crypto.fds); // server myserver(5000, 100, crypto.fds);
cout << "init client " << endl; // cout << "init client " << endl;
// client myclient(&myserver, 100, crypto.fds); // // client myclient(&myserver, 100, crypto.fds);
// comming myclient(&myserver, 100); // // comming myclient(&myserver, 100);
myserver.accept(); // myserver.accept();
// cout << "wait client " << myclient.ipv4 << endl; // // cout << "wait client " << myclient.ipv4 << endl;
cout << "wait client " << myserver.cli->ipv4 << endl; // cout << "wait client " << myserver.cli->ipv4 << endl;
string fromclient = myserver.cli->pull(); // string fromclient = myserver.cli->pull();
// string fromclient = myclient.pull(); // // string fromclient = myclient.pull();
cout << "tell client " << fromclient << endl; // cout << "tell client " << fromclient << endl;
// usleep(600*1000); // // usleep(600*1000);
sleep(5); // sleep(5);
myserver.cli->push(fromclient); // myserver.cli->push(fromclient);
// myclient.push(fromclient); // myclient.push(fromclient);
// myclient.~comming(); // myclient.~comming();
@ -38,6 +38,16 @@ int main() {
// } // }
// sleep(80); // sleep(80);
server myserver(5000, 100);
myserver.asyncli(8, [](client *cli) {
cout << "Klijent " << cli->ipv4 << endl;
string fromclient = cli->pull();
cout << "S klijenta " << fromclient << endl;
cli->push(fromclient);
}, 200);
} }
catch(const string err) { catch(const string err) {
cout << err << endl; cout << err << endl;

Binary file not shown.
Loading…
Cancel
Save