-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.h
More file actions
120 lines (84 loc) · 2.87 KB
/
Server.h
File metadata and controls
120 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#ifndef SERVER_H
#define SERVER_H 1
#include <string>
#include <set>
#include <map>
#include <exception>
#include <pthread.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
typedef long long Server_Client_ID;
class Server;
class Server_Delegate
{
public:
virtual void on_client_connect(Server_Client_ID &client, Server* server) = 0;
virtual void on_client_disconnect(Server_Client_ID &client, Server* server) = 0;
virtual void recieve_message(std::string &message, Server_Client_ID &client, Server* server) = 0;
};
class Server
{
public:
class Server_Exception : public std::exception
{
public:
Server_Exception(std::string type)
{
this->type = type;
}
virtual ~Server_Exception() throw() {}
virtual const char* what() const throw()
{
return type.c_str();
}
private:
std::string type;
};
static Server_Exception IPv4_Initialization_Failure;
static Server_Exception IPv6_Initialization_Failure;
static Server_Exception Thread_Initialization_Failure;
static Server_Exception Sending_Without_Existence;
typedef enum {
IPv4_Server = 0,
IPv6_Server = 1,
Dual_IPv4_IPv6_Server = 2
} Server_Type;
Server(int port, Server_Type type, Server_Delegate *delegate = NULL, bool verbose = true);
Server(int port, Server_Type type, bool ssl, std::string cert_path, std::string key_path, Server_Delegate *delegate = NULL, bool verbose = true);
virtual ~Server();
virtual void on_client_connect(Server_Client_ID &client) {}
virtual void on_client_disconnect(Server_Client_ID &client) {}
virtual void recieve_message(std::string &message, Server_Client_ID &client) {}
void broadcast_message(std::string &message);
void broadcast_message_to_clients(std::string &message, std::set<Server_Client_ID> clients);
void send_message(std::string &message, const Server_Client_ID &client);
void accept_client(Server_Client_ID &client, SSL* ssl);
void accept_client(Server_Client_ID &client, int client_socket);
void disconnect_client(Server_Client_ID &client);
pthread_mutex_t* get_mutex() { return &server_mutex; }
Server_Delegate* get_delegate() { return delegate; }
std::string get_cert_path() { return cert_path; }
std::string get_key_path() { return key_path; }
std::map<Server_Client_ID, int>* get_clients() { return &clients; }
std::map<Server_Client_ID, SSL*>* get_ssl_clients() { return &ssl_clients; }
int client_id_to_socket(Server_Client_ID &client);
protected:
bool verbose;
private:
pthread_t server_thread_ipv4;
pthread_t server_thread_ipv6;
pthread_mutex_t server_mutex;
int server_sock_ipv4;
int server_sock_ipv6;
Server_Type server_type;
bool ssl;
Server_Delegate* delegate;
std::map<Server_Client_ID, int> clients;
std::map<Server_Client_ID, SSL*> ssl_clients;
void create_server(int port, Server_Type type);
std::string cert_path;
std::string key_path;
public:
static Server_Client_ID next_id();
};
#endif