Compare commits
No commits in common. "v0.4_mc-async" and "dev" have entirely different histories.
v0.4_mc-as
...
dev
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@ -44,7 +44,6 @@
|
|||||||
"stdexcept": "cpp",
|
"stdexcept": "cpp",
|
||||||
"streambuf": "cpp",
|
"streambuf": "cpp",
|
||||||
"cinttypes": "cpp",
|
"cinttypes": "cpp",
|
||||||
"typeinfo": "cpp",
|
"typeinfo": "cpp"
|
||||||
"thread": "cpp"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,9 +3,6 @@
|
|||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
#include <thread>
|
|
||||||
#include <mutex>
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
@ -17,11 +14,6 @@
|
|||||||
|
|
||||||
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
|
||||||
@ -31,15 +23,10 @@ class server {
|
|||||||
public:
|
public:
|
||||||
int sock;
|
int sock;
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
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 limit = 1000);
|
||||||
~server ();
|
~server ();
|
||||||
|
|
||||||
void sync(void (*handlecli)(client&), const uint timeout = 100);
|
|
||||||
void async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -64,23 +51,40 @@ class secure {
|
|||||||
|
|
||||||
class client {
|
class client {
|
||||||
public:
|
public:
|
||||||
// zajedničke
|
int sock;
|
||||||
int conn; // mijenja sock
|
|
||||||
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 push (const string msg);
|
bool tell (const string msg);
|
||||||
string pull (size_t byte_limit = 1024);
|
string obey (size_t byte_limit = 1024);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasa za inicijalizaciju dolaznih veza
|
||||||
|
* Definira se na serverskom tipu aplikacija i predstavlja identifikator klijenta
|
||||||
|
*/
|
||||||
|
|
||||||
|
class comming {
|
||||||
|
public:
|
||||||
|
const server *srv;
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
int conn;
|
||||||
|
string ipv4;
|
||||||
|
string ipv6;
|
||||||
|
SSL* ssl = NULL;
|
||||||
|
|
||||||
|
comming(const server *_srv, const uint timeout = 100, SSL_CTX* securefds = NULL);
|
||||||
|
~comming();
|
||||||
|
bool tell (const string msg);
|
||||||
|
string obey (size_t byte_limit = 1024);
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -4,8 +4,7 @@
|
|||||||
* 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 queue, SSL_CTX* _securefds) {
|
server::server (const ushort port, const uint limit) {
|
||||||
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;
|
||||||
@ -26,50 +25,12 @@ server::server (const ushort port, const uint queue, SSL_CTX* _securefds) {
|
|||||||
throw string("[ERROR] Unable to bind socket ");
|
throw string("[ERROR] Unable to bind socket ");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listen(sock, queue) < 0) {
|
if (listen(sock, limit) < 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
|
||||||
*/
|
*/
|
||||||
@ -149,8 +110,8 @@ 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) {
|
||||||
|
|
||||||
conn = socket(AF_INET, SOCK_STREAM, 0);
|
sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
if (conn < 0) {
|
if (sock < 0) {
|
||||||
throw string("[ERROR] Unable to open TCP socket ");
|
throw string("[ERROR] Unable to open TCP socket ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,7 +121,7 @@ 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(conn, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) {
|
if (connect(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) != 0) {
|
||||||
throw string("Unable to connect to server ");
|
throw string("Unable to connect to server ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -168,7 +129,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
|
|||||||
tv.tv_sec = timeout/1000;
|
tv.tv_sec = timeout/1000;
|
||||||
tv.tv_usec = (timeout%1000)*1000;
|
tv.tv_usec = (timeout%1000)*1000;
|
||||||
|
|
||||||
if (setsockopt(conn, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
|
if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval))) {
|
||||||
throw string("[ERROR] Unable to set timeout ");
|
throw string("[ERROR] Unable to set timeout ");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +138,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
|
|||||||
if (!ssl) {
|
if (!ssl) {
|
||||||
throw string("[ERROR] Creating SSL object ");
|
throw string("[ERROR] Creating SSL object ");
|
||||||
}
|
}
|
||||||
SSL_set_fd(ssl, conn);
|
SSL_set_fd(ssl, sock);
|
||||||
|
|
||||||
// Perform the SSL handshake
|
// Perform the SSL handshake
|
||||||
if (SSL_connect(ssl) <= 0) {
|
if (SSL_connect(ssl) <= 0) {
|
||||||
@ -189,6 +150,65 @@ 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,
|
||||||
@ -197,7 +217,7 @@ client::client(const string address, const ushort port, const uint timeout, SSL_
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
|
comming::comming(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);
|
||||||
|
|
||||||
@ -239,12 +259,11 @@ client::client(const server *_srv, const uint timeout, SSL_CTX* securefds) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destruktor varijable tipa client
|
* Destruktor varijable tipa comming
|
||||||
*/
|
*/
|
||||||
|
|
||||||
client::~client () {
|
comming::~comming() {
|
||||||
|
|
||||||
if (ssl) {
|
if (ssl) {
|
||||||
SSL_shutdown(ssl);
|
SSL_shutdown(ssl);
|
||||||
@ -258,18 +277,16 @@ client::~client () {
|
|||||||
else if (close(conn) != 0) {
|
else if (close(conn) != 0) {
|
||||||
throw string("[ERROR] Unable to close socket ");
|
throw string("[ERROR] Unable to close socket ");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Metoda klase client za slanje podataka preko soketa
|
* Metoda klase comming 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) {
|
||||||
bool client::push (const string msg) {
|
ssize_t sended = 0;
|
||||||
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());
|
||||||
}
|
}
|
||||||
@ -279,14 +296,15 @@ bool client::push (const string msg) {
|
|||||||
return sended == msg.length();
|
return sended == msg.length();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Metoda klase client za primanje poruke preko soketa
|
* Metoda klase comming 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 client::pull (size_t byte_limit) {
|
string comming::obey (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);
|
||||||
@ -297,6 +315,3 @@ string client::pull (size_t byte_limit) {
|
|||||||
|
|
||||||
return string(res);
|
return string(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "../lib/tcp_socket.hpp"
|
#include "../lib/tcp_socket.hpp"
|
||||||
|
|
||||||
@ -9,35 +8,19 @@ int main() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
uint n = 10000;
|
secure crypto;
|
||||||
|
cout << "init cert " << endl;
|
||||||
|
|
||||||
vector<thread> thr;
|
client myserver("127.0.0.1", 5000, 5000, crypto.fds);
|
||||||
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.tell(sends) << " " << sends.length() << endl;
|
||||||
// cout << "wait client " << endl;
|
cout << "wait client " << endl;
|
||||||
|
|
||||||
// cout << myserver.pull();
|
cout << myserver.obey();
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (const string err) {
|
catch (const string err) {
|
||||||
|
BIN
test/client.o
BIN
test/client.o
Binary file not shown.
@ -6,26 +6,21 @@ using namespace std;
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
try{
|
try{
|
||||||
// cout << "init cert " << endl;
|
cout << "init server " << endl;
|
||||||
// secure crypto ("../example/cert.pem", "../example/privkey.pem");
|
server myserver(5000);
|
||||||
// cout << "init server " << endl;
|
cout << "init cert " << endl;
|
||||||
// server myserver(5000, 100, crypto.fds);
|
secure crypto ("../example/cert.pem", "../example/privkey.pem");
|
||||||
|
cout << "init client " << endl;
|
||||||
|
|
||||||
// cout << "init client " << endl;
|
comming myclient(&myserver, 100, crypto.fds);
|
||||||
|
// comming myclient(&myserver, 100);
|
||||||
|
cout << "wait client " << myclient.ipv4 << endl;
|
||||||
|
|
||||||
// // client myclient(&myserver, 100, crypto.fds);
|
string fromclient = myclient.obey();
|
||||||
// // comming myclient(&myserver, 100);
|
cout << "tell client " << fromclient << endl;
|
||||||
// myserver.accept();
|
// usleep(600*1000);
|
||||||
// // cout << "wait client " << myclient.ipv4 << endl;
|
sleep(5);
|
||||||
// cout << "wait client " << myserver.cli->ipv4 << endl;
|
myclient.tell(fromclient);
|
||||||
|
|
||||||
// string fromclient = myserver.cli->pull();
|
|
||||||
// // string fromclient = myclient.pull();
|
|
||||||
// cout << "tell client " << fromclient << endl;
|
|
||||||
// // usleep(600*1000);
|
|
||||||
// sleep(5);
|
|
||||||
// myserver.cli->push(fromclient);
|
|
||||||
// myclient.push(fromclient);
|
|
||||||
// myclient.~comming();
|
// myclient.~comming();
|
||||||
|
|
||||||
// while (true) {
|
// while (true) {
|
||||||
@ -38,31 +33,6 @@ 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);
|
|
||||||
}, 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;
|
||||||
|
BIN
test/server.o
BIN
test/server.o
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user