-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.h
More file actions
56 lines (41 loc) · 1.79 KB
/
server.h
File metadata and controls
56 lines (41 loc) · 1.79 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
#ifndef SERVER_H
#define SERVER_H
#include "connection.h"
#include <memory>
#include <vector>
/* A server listens to a port and handles multiple connections */
class Server {
public:
/* Creates a server that listens to a port */
explicit Server(int port);
/* Removes all registered connections */
virtual ~Server();
/* Returns true if the server has been initialized correctly */
bool isReady() const;
/* Waits for activity on the port. Returns a previously registered
connection object if an existing client wishes to communicate,
nullptr if a new client wishes to communicate */
std::shared_ptr<Connection> waitForActivity() const;
/* Registers a new connection */
void registerConnection(const std::shared_ptr<Connection>& conn);
/* Deregisters a connection */
void deregisterConnection(const std::shared_ptr<Connection>& conn);
/* Servers cannot be copied or assigned*/
Server(const Server&) = delete;
Server& operator=(const Server&) = delete;
Server& operator=(Server&&) = delete;
/* Servers can be move constructed */
Server(Server&& o) :my_socket{o.my_socket},
connections(std::move(o.connections)),
pending_socket{o.pending_socket} {}
protected:
/* The number of the communication socket */
int my_socket {Connection::no_socket};
/* List of registered connections */
std::vector<std::shared_ptr<Connection>> connections;
/* Socket for a connection waiting to be registered */
mutable int pending_socket {Connection::no_socket};
/* Prints error message and exita */
void error(const char* msg) const;
};
#endif