-
Notifications
You must be signed in to change notification settings - Fork 3
/
Server.hh
48 lines (35 loc) · 839 Bytes
/
Server.hh
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
#ifndef SERVER_HH
#define SERVER_HH
#include <functional>
#include <memory>
#include <mutex>
#include <vector>
class ClientSocket;
class Server
{
public:
~Server();
void setBacklog( int backlog );
void setPort( int port );
void close();
void listen();
template <class F> void onAccept( F&& f )
{
_handleAccept = f;
}
template <class F> void onRead( F&& f )
{
_handleRead = f;
}
void close( int fileDescriptor );
private:
int _backlog = 1;
int _port = -1;
int _socket = -1;
std::function< void ( std::weak_ptr<ClientSocket> socket ) > _handleAccept;
std::function< void ( std::weak_ptr<ClientSocket> socket ) > _handleRead;
std::vector< std::shared_ptr<ClientSocket> > _clientSockets;
std::vector<int> _staleFileDescriptors;
std::mutex _staleFileDescriptorsMutex;
};
#endif