Compare commits

...

13 Commits
dev ... queue

  1. 4
      .vscode/settings.json
  2. 13
      lib/ip.hpp
  3. 89
      lib/tcp_socket.hpp
  4. 361
      src/tcp_socket.cpp
  5. 85
      test/client.cpp
  6. BIN
      test/client.exe
  7. BIN
      test/client.o
  8. 1
      test/compile-client.ps1
  9. 1
      test/compile-server.ps1
  10. 2
      test/compile-server.sh
  11. 68
      test/server.cpp
  12. BIN
      test/server.exe
  13. BIN
      test/server.o

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

@ -4,9 +4,16 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include <netdb.h> #if __linux__
#include <unistd.h> #include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
// #include <sstream>
#include <WinSock.h>
#include <ws2tcpip.h>
#endif
using namespace std; using namespace std;

@ -3,17 +3,35 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector>
#include <deque>
#include <thread>
#include <mutex>
#include <string.h> #include <string.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <openssl/ssl.h> #include <openssl/ssl.h>
#include <openssl/err.h> #include <openssl/err.h>
#if __linux__
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#elif _WIN32
#include <WinSock.h>
#include <ws2tcpip.h>
#pragma comment(lib,"ws2_32.lib")
#define ushort u_short
#define uint u_int
#endif
#include "ip.hpp" #include "ip.hpp"
using namespace std; using namespace std;
class client;
// class secure;
// class server;
/** /**
* 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
@ -21,12 +39,22 @@ using namespace std;
class server { class server {
public: public:
#if __linux__
int sock; int sock;
#elif _WIN32
WSADATA wsa;
SOCKET sock;
#endif
struct sockaddr_in addr; struct sockaddr_in addr;
SSL_CTX* securefds = NULL;
vector<thread> thr;
server (const ushort port, const uint limit = 1000); server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL);
~server (); ~server ();
void sync(void (*handlecli)(client&), const uint timeout = 100);
void async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
}; };
/** /**
@ -51,40 +79,53 @@ class secure {
class client { class client {
public: public:
int sock; // zajedničke
#if __linux__
int conn; // mijenja sock
#elif _WIN32
WSADATA wsa;
SOCKET conn; // mijenja sock
#endif
struct sockaddr_in addr; struct sockaddr_in addr;
SSL* ssl = NULL; SSL* ssl = NULL;
// server s klijentima
const server* srv;
// klijent sa serverom
string ipv4;
string ipv6;
// konstruktor za klijente bez servera
client (const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL); client (const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL);
// konstruktor za klijente sa serverom
client (const server *_srv, const uint timeout = 100, SSL_CTX* securefds = NULL);
~client (); ~client ();
bool tell (const string msg); bool push (const string msg);
string obey (size_t byte_limit = 1024); string pull (size_t byte_limit = 1024);
}; };
/** class queue { // FIFO
* Klasa za inicijalizaciju dolaznih veza
* Definira se na serverskom tipu aplikacija i predstavlja identifikator klijenta
*/
class comming {
public: public:
const server *srv; mutex io;
struct sockaddr_in addr; uint limit;
int conn; string address;
string ipv4; ushort port;
string ipv6; uint timeout;
SSL* ssl = NULL; server *srv = NULL;
SSL_CTX* securefds = NULL;
comming(const server *_srv, const uint timeout = 100, SSL_CTX* securefds = NULL); deque<client*> clients;
~comming(); thread controller;
bool tell (const string msg);
string obey (size_t byte_limit = 1024);
queue (const uint _limit, const string _address, const ushort _port, const uint _timeout = 100, SSL_CTX* _securefds = NULL);
queue (const uint _limit, server *_srv, const uint _timeout = 100, SSL_CTX* _securefds = NULL);
}; ~queue();
void enqueue();
client dequeue();
};
#endif #endif

@ -4,12 +4,19 @@
* Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju * Kontrustruktor varijable tipa server, prima port i limit za ograničenje liste klijenata na čekanju
*/ */
server::server (const ushort port, const uint limit) { server::server (const ushort port, const uint queue, SSL_CTX* _securefds) {
securefds = _securefds;
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port); addr.sin_port = htons(port);
#if _WIN32
if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
throw string("[ERROR] WSA Startup. Detail: " + to_string(WSAGetLastError()));
}
#endif
sock = socket(AF_INET, SOCK_STREAM, 0); sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock <= 0) { if (sock <= 0) {
throw string("[ERROR] Unable to open TCP socket "); throw string("[ERROR] Unable to open TCP socket ");
@ -17,20 +24,65 @@ server::server (const ushort port, const uint limit) {
} }
int opt=1; int opt=1;
#if __linux__
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket "); throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
} }
#elif _WIN32
if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt))) {
throw string("[ERROR] Unable to set REUSEADDR or REUSEPORT on socket ");
}
#endif
if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
throw string("[ERROR] Unable to bind socket "); 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 "); throw string("[ERROR] It is not possible to set the allowed number of waiting clients ");
} }
} }
/**
* 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 {
client cli(this, timeout, securefds);
handlecli(cli);
} while (true);
}
/**
* 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;
*/
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);
}
/** /**
* Destruktor varijable tipa server * Destruktor varijable tipa server
*/ */
@ -42,10 +94,21 @@ server::~server () {
throw string("[ERROR] The socket is already closed "); throw string("[ERROR] The socket is already closed ");
} }
else if (close(sock) != 0) { else {
#if __linux__
if (close(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
#elif _WIN32
if (closesocket(sock) != 0) {
throw string("[ERROR] Unable to close socket "); throw string("[ERROR] Unable to close socket ");
} }
WSACleanup();
#endif
}
} }
/** /**
@ -110,8 +173,14 @@ secure::~secure () {
client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) { client::client(const string address, const ushort port, const uint timeout, SSL_CTX* securefds) {
sock = socket(AF_INET, SOCK_STREAM, 0); #if _WIN32
if (sock < 0) { if (WSAStartup(MAKEWORD(2,2),&wsa) != 0) {
throw string("[ERROR] Unable to set WinSock " + to_string(WSAGetLastError()));
}
#endif
conn = socket(AF_INET, SOCK_STREAM, 0);
if (conn < 0) {
throw string("[ERROR] Unable to open TCP socket "); throw string("[ERROR] Unable to open TCP socket ");
} }
@ -121,24 +190,31 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
addr.sin_addr.s_addr = inet_addr(_address.c_str()); addr.sin_addr.s_addr = inet_addr(_address.c_str());
addr.sin_port = htons(port); addr.sin_port = htons(port);
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) { if (connect(conn, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) {
throw string("Unable to connect to server "); throw string("Unable to connect to server ");
} }
#if __linux__
struct timeval tv; struct timeval tv;
tv.tv_sec = timeout/1000; tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000; tv.tv_usec = (timeout%1000)*1000;
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) { if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout ");
}
#elif _WIN32
DWORD tv = timeout;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
throw string("[ERROR] Unable to set timeout "); throw string("[ERROR] Unable to set timeout ");
} }
#endif
if (securefds) { if (securefds) {
ssl = SSL_new(securefds); ssl = SSL_new(securefds);
if (!ssl) { if (!ssl) {
throw string("[ERROR] Creating SSL object "); throw string("[ERROR] Creating SSL object ");
} }
SSL_set_fd(ssl, sock); SSL_set_fd(ssl, conn);
// Perform the SSL handshake // Perform the SSL handshake
if (SSL_connect(ssl) <= 0) { if (SSL_connect(ssl) <= 0) {
@ -150,65 +226,6 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
} }
/**
* Destruktor varijable tipa client
*/
client::~client () {
if (ssl) {
SSL_shutdown(ssl);
SSL_free(ssl);
}
if (sock <= 0) {
throw string("[ERROR] The socket is already closed ");
}
else if (close(sock) != 0) {
throw string("[ERROR] Unable to close socket ");
}
}
/**
* Metoda klase client za slanje podataka preko soketa
* Prima string koji će biti poslan
* Vraća logički statu poređenja psolanih karaktera i karaktera u stringu
*/
bool client::tell (const string msg) {
size_t sended = 0;
if (ssl) {
sended = SSL_write(ssl, msg.c_str(), msg.length());
}
else {
sended = write(sock, msg.c_str(), msg.length());
}
return sended == msg.length();
}
/**
* Metoda klase client za primanje poruke preko soketa
* Prima dozvoljeni broj karaktera koji će primiti
* Vraća string primljene poruke
*/
string client::obey (size_t byte_limit) {
char res[byte_limit] = {0};
if (ssl) {
SSL_read(ssl, res, byte_limit);
}
else {
read(sock , res, byte_limit);
}
return string(res);
}
/** /**
* Konstruktor varijable tipa commint * Konstruktor varijable tipa commint
* Prima pokazivač na inicijaliziranu varijablu tipa, port, * Prima pokazivač na inicijaliziranu varijablu tipa, port,
@ -217,7 +234,7 @@ string client::obey (size_t byte_limit) {
*/ */
comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) { client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
srv = _srv; srv = _srv;
socklen_t len = sizeof(struct sockaddr_in); socklen_t len = sizeof(struct sockaddr_in);
@ -225,6 +242,7 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
throw string("[ERROR] Unable to accept client connection "); throw string("[ERROR] Unable to accept client connection ");
} }
#if __linux__
struct timeval tv; struct timeval tv;
tv.tv_sec = timeout/1000; tv.tv_sec = timeout/1000;
tv.tv_usec = (timeout%1000)*1000; tv.tv_usec = (timeout%1000)*1000;
@ -232,7 +250,13 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) { if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
throw string("[ERROR] Unable to set timeout "); throw string("[ERROR] Unable to set timeout ");
} }
#elif _WIN32
DWORD tv = timeout;
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(tv))) {
throw string("[ERROR] Unable to set timeout ");
}
#endif
if (securefds) { if (securefds) {
ssl = SSL_new(securefds); ssl = SSL_new(securefds);
@ -259,11 +283,12 @@ comming::comming(const server *_srv, const uint timeout, SSL_CTX* securefds) {
} }
/** /**
* Destruktor varijable tipa comming * Destruktor varijable tipa client
*/ */
comming::~comming() { client::~client () {
if (ssl) { if (ssl) {
SSL_shutdown(ssl); SSL_shutdown(ssl);
@ -274,44 +299,222 @@ comming::~comming() {
throw string("[ERROR] The socket is already closed "); throw string("[ERROR] The socket is already closed ");
} }
else if (close(conn) != 0) { else {
#if __linux__
if (close(conn) != 0) {
throw string("[ERROR] Unable to close socket "); throw string("[ERROR] Unable to close socket ");
} }
#elif _WIN32
if (closesocket(conn) != 0) {
throw string("[ERROR] Unable to close socket ");
}
//WSACleanup();
#endif
}
} }
/** /**
* Metoda klase comming za slanje podataka preko soketa * Metoda klase client za slanje podataka preko soketa
* Prima string koji će biti poslan * Prima string koji će biti poslan
* Vraća logički statu poređenja psolanih karaktera i karaktera u stringu * Vraća logički statu poređenja psolanih karaktera i karaktera u stringu
*/ */
bool comming::tell (const string msg) {
ssize_t sended = 0; bool client::push (const string msg) {
size_t sended = 0;
if (ssl) { if (ssl) {
sended = SSL_write(ssl, msg.c_str(), msg.length()); sended = SSL_write(ssl, msg.c_str(), msg.length());
} }
else { else {
sended = write(conn, msg.c_str(), msg.length()); sended = send(conn, msg.c_str(), msg.length(), 0);
} }
return sended == msg.length(); return sended == msg.length();
} }
/** /**
* Metoda klase comming za primanje poruke preko soketa * Metoda klase client za primanje poruke preko soketa
* Prima dozvoljeni broj karaktera koji će primiti * Prima dozvoljeni broj karaktera koji će primiti
* Vraća string primljene poruke * Vraća string primljene poruke
*/ */
string comming::obey (size_t byte_limit) { string client::pull (size_t byte_limit) {
char res[byte_limit] = {0}; char res[byte_limit] = {0};
if (ssl) { if (ssl) {
SSL_read(ssl, res, byte_limit); SSL_read(ssl, res, byte_limit);
} }
else { else {
read(conn , res, byte_limit); recv(conn , res, byte_limit, 0);
} }
return string(res); return string(res);
} }
queue::queue (const uint _limit, const string _address, const ushort _port, const uint _timeout, SSL_CTX* _securefds) {
if (_limit > 1) {
limit = _limit;
}
else {
throw string("[ERROR] Limit out of range ");
}
address = _address;
port = _port;
timeout = _timeout;
securefds = _securefds;
// for (uint i=0; i<limit; i++) {
// if (address.empty() && srv != NULL) {
// clients.push_back(new client(srv, timeout, securefds));
// }
// else {
// clients.push_back(new client(address, port, timeout, securefds));
// }
// }
// controller = thread([&](){
// cout << "Upao u tred "<< endl;
// while(true) {
// // io.lock();
// // uint missing = limit-clients.size();
// // cout << "nedostaje " << missing << endl;
// // if (missing > 0.6*limit) {
// // enqueue();
// if (clients.size() <= 1) {
// if (address.empty() && srv != NULL) {
// clients.push_back(new client(srv, timeout, securefds));
// }
// else {
// clients.push_back(new client(address, port, timeout, securefds));
// }
// }
// // }
// // usleep(missing*100000);
// // io.unlock();
// // usleep(1000);
// sleep(1);
// }
// });
}
queue::queue (const uint _limit, server *_srv, const uint _timeout, SSL_CTX* _securefds) {
if (_limit > 1) {
limit = _limit;
}
else {
throw string("[ERROR] Limit out of range ");
}
srv = _srv;
timeout = _timeout;
securefds = _securefds;
controller = thread([this](){
cout << "Upao u tred "<< endl;
while(true) {
io.lock();
// uint missing = limit-clients.size();
// cout << "nedostaje " << missing << endl;
// if (missing > 0.6*limit) {
// enqueue();
if (clients.size()< limit) {
if (address.empty() && srv != NULL) {
clients.push_back(new client(srv, timeout, securefds));
}
else {
clients.push_back(new client(address, port, timeout, securefds));
}
}
// }
// usleep(missing*100000);
io.unlock();
// usleep(1000);
}
});
}
void queue::enqueue () {
vector<thread> worker;
lock_guard<mutex> master(io);
for (uint i=0; i<limit-clients.size(); i++) {
cout << "idemo dodati klijente " << endl;
worker.push_back(thread([&](){
if (address.empty() && srv != NULL) {
clients.push_back(new client(srv, timeout, securefds));
}
else {
cout << "dodajem " << i << endl;
clients.push_back(new client(address, port, timeout, securefds));
}
}));
}
for (uint i=0; i<worker.size(); i++) {
cout << "čekam da se dodaju " << endl;
worker[i].join();
}
cout << "dodali su se " << endl;
worker.clear();
}
client queue::dequeue() {
cout << "uzimam jednog iz stacka " << endl;
lock_guard<mutex> master(io);
client *cli;
while (true) {
if (clients.size() >= 1) {
cli = clients.front();
clients.pop_front();
cout << "uzimam jednog iz stacka uzeo sam " << endl;
return *cli;
}
else if (clients.size() == 1) {
controller = thread([&](){
cout << "Upao u tred "<< endl;
while(true) {
// io.lock();
// uint missing = limit-clients.size();
// cout << "nedostaje " << missing << endl;
// if (missing > 0.6*limit) {
// enqueue();
if (clients.size() <= 1) {
if (address.empty() && srv != NULL) {
clients.push_back(new client(srv, timeout, securefds));
}
else {
clients.push_back(new client(address, port, timeout, securefds));
}
}
// }
// usleep(missing*100000);
// io.unlock();
// usleep(1000);
sleep(1);
}
});
}
usleep(1000);
}
}
queue::~queue () {
for (uint i=0; i<clients.size(); i++) {
clients[i]->~client();
}
clients.clear();
}

@ -1,26 +1,93 @@
#include <iostream> #include <iostream>
#include <string>
#include <chrono>
#include "../lib/tcp_socket.hpp" #include "../lib/tcp_socket.hpp"
using namespace std; using namespace std;
using namespace chrono;
int main() { int main() {
try { try {
cout << "inicijalizacija reda" << endl;
// queue sockets(2, "127.0.0.1", 5000, 500);
secure crypto;
cout << "init cert " << endl;
client myserver("127.0.0.1", 5000, 5000, crypto.fds); // thread t1([&]() {
// client myserver("localhost", 5000); // cout << "korištenje prvog klijenta" << endl;
cout << "init client " << endl; // int a = 0;
// while(true) {
// try {
// auto start = high_resolution_clock::now();
// client myserver = sockets.dequeue();
// auto stop = high_resolution_clock::now();
// auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
// string sends = "Socket I " + to_string(a++);
// myserver.push(sends);
// cout << myserver.pull() << endl;
// } catch (const string err) {
// cout << err << endl;
// }
// // usleep(100000);
// }
// });
string sends = "Hello world!"; // // usleep(20000);
cout << myserver.tell(sends) << " " << sends.length() << endl;
cout << "wait client " << endl;
cout << myserver.obey(); // thread t2([&]() {
// cout << "korištenje drugog klijenta" << endl;
// int a = 0;
// while(true) {
// try {
// auto start = high_resolution_clock::now();
// client myserver = sockets.dequeue();
// auto stop = high_resolution_clock::now();
// auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
// string sends = "Socket II " + to_string(a++);
// myserver.push(sends);
// cout << myserver.pull() << endl;
// } catch (const string err) {
// cout << err << endl;
// }
// // usleep(100000);
// }
// });
// t1.join();
// t2.join();
// int a = 0;
// auto start = high_resolution_clock::now();
// client myserver ("127.0.0.1", 5000, 500);
// // auto stop = high_resolution_clock::now();
// // auto duration = duration_cast<microseconds>(stop - start);
// // cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
// string sends = "Socket II " + to_string(a++);
// myserver.push(sends);
// cout << myserver.pull() << endl;
// auto stop = high_resolution_clock::now();
// auto duration = duration_cast<microseconds>(stop - start);
// cout << "Time taken by function: " << duration.count() << " microseconds" << endl;
vector<client*> clis;
for (uint i =0 ; i<5; i++) {
clis.push_back(new client("127.0.0.1", 5000, 500));
}
cout << "Dodalo je đubre "<< endl;
sleep(5);
string sends = "Socket I ";
clis[1]->push(sends);
cout << clis[1]->pull() << endl;
sends = "Socket III ";
clis[3]->push(sends);
cout << clis[3]->pull() << endl;
} }
catch (const string err) { catch (const string err) {

Binary file not shown.

Binary file not shown.

@ -0,0 +1 @@
g++ client.cpp ../src/* -o client.exe -lssl -lcrypto -lws2_32

@ -0,0 +1 @@
g++ server.cpp ../src/* -o server.exe -lssl -lcrypto -lws2_32

@ -1 +1 @@
g++ server.cpp ../src/* -o server.o -lssl -lcrypto g++ server.cpp ../src/* -o server.o -lssl -lcrypto -lpthread

@ -6,21 +6,36 @@ using namespace std;
int main() { int main() {
try{ try{
cout << "init server " << endl; // cout << "init cert " << endl;
server myserver(5000); // secure crypto ("../example/cert.pem", "../example/privkey.pem");
cout << "init cert " << endl; // cout << "init server " << endl;
secure crypto ("../example/cert.pem", "../example/privkey.pem"); // server myserver(5000, 100, crypto.fds);
cout << "init client " << endl; // cout << "init cert " << endl;
// secure crypto ("../example/cert.pem", "../example/privkey.pem");
comming myclient(&myserver, 100, crypto.fds); // cout << "init server " << endl;
// comming myclient(&myserver, 100); // server myserver(8000, 100, crypto.fds);
cout << "wait client " << myclient.ipv4 << endl;
// cout << "init client " << endl;
string fromclient = myclient.obey();
cout << "tell client " << fromclient << endl; // // client myclient(&myserver, 100, crypto.fds);
// // comming myclient(&myserver, 100);
// myserver.accept();
// // cout << "wait client " << myclient.ipv4 << endl;
// cout << "wait client " << myserver.cli->ipv4 << endl;
// string fromclient = myserver.cli->pull();
// // string fromclient = myclient.pull();
// cout << "tell client " << fromclient << endl;
// // usleep(600*1000);
// sleep(5);
// myserver.cli->push(fromclient);
// string fromclient = myserver.cli->pull();
// string fromclient = myclient.pull();
// cout << "tell client " << fromclient << endl;
// usleep(600*1000); // usleep(600*1000);
sleep(5); //sleep(5);
myclient.tell(fromclient); // myserver.cli->push(fromclient);
// myclient.push(fromclient);
// myclient.~comming(); // myclient.~comming();
// while (true) { // while (true) {
@ -33,6 +48,31 @@ int main() {
// } // }
// sleep(80); // sleep(80);
server myserver(5000, 100);
myserver.async(8, [](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);
}, 0);
// 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.

Binary file not shown.
Loading…
Cancel
Save