Simple TCP socket library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
socket/lib/tcp_socket.hpp

127 lines
2.8 KiB

1 year ago
#ifndef TCP_SCK
#define TCP_SCK
#include <iostream>
#include <string>
#include <vector>
#include <thread>
#include <mutex>
1 year ago
#include <string.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
1 year ago
#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
1 year ago
#include "ip.hpp"
using namespace std;
class client;
// class secure;
// class server;
/**
* Server klasa za TCP/IP soket
* Instanca se incijalizira kada pokrećemo server
*/
1 year ago
class server {
public:
#if __linux__
int sock;
#elif _WIN32
WSADATA wsa;
SOCKET sock;
#endif
1 year ago
struct sockaddr_in addr;
SSL_CTX* securefds = NULL;
vector<thread> thr;
1 year ago
server (const ushort port, const uint queue = 1000, SSL_CTX* _securefds = NULL);
1 year ago
~server ();
void sync(void (*handlecli)(client&), const uint timeout = 100);
void async(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
void pool(const uint limit, void (*handlecli)(client&, mutex&), const uint timeout = 100);
1 year ago
};
/**
* Klasa za konstrukciju i destrukciju SSL certifikata
* Pokreće se za server i client - (različiti konstruktori)
*/
class secure {
public:
SSL_CTX* fds;
secure(); // client
secure(const string cert, const string priv); //server
~secure();
};
1 year ago
/**
* Klasa za definiranje TCP soketa kod klijentskih aplikacija
*/
1 year ago
class client {
public:
// zajedničke
#if __linux__
int conn; // mijenja sock
#elif _WIN32
WSADATA wsa;
SOCKET conn; // mijenja sock
#endif
1 year ago
struct sockaddr_in addr;
SSL* ssl = NULL;
// server s klijentima
const server* srv;
// klijent sa serverom
1 year ago
string ipv4;
string ipv6;
// konstruktor za klijente bez servera
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 ();
bool push (const string msg);
string pull (size_t byte_limit = 1024);
/**
* ustvari ne znam jel konekcija aktivna
* kod za connect i disconnect je konstruktoru - destruktoru
*/
};
class Pool {
public:
uint numcli;
vector<pair<mutex*, client*>> drops;
// konstruktor za klijente bez servera
Pool (const uint _numcli, const string address, const ushort port, const uint timeout = 100, SSL_CTX* securefds = NULL);
// konstruktor za klijente sa serverom
Pool (const server *_srv, const uint _numcli, const uint timeout = 100, SSL_CTX* securefds = NULL);
~Pool();
void run();
1 year ago
};
#endif