-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.h
84 lines (57 loc) · 1.8 KB
/
Server.h
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
#ifndef SERVER_H
#define SERVER_H
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <unistd.h>
#include "ClientInterface.h"
namespace TP1{
enum IP_TYPE { IPV4, IPV6};
typedef std::vector<Client> ClientList;
/*************************
* Singleton Server class
*************************/
class Server {
public:
//There can only be one server, which will be initialized on demand
static Server* getInstance() {
if (_serverInstance == NULL) {
_serverInstance = new Server();
}
return _serverInstance;
}
static void deleteInstance() {
if (_serverInstance->_serverSocketId != -1) {
close(_serverInstance->_serverSocketId);
}
if (_serverInstance->_clientSocketId != -1) {
close(_serverInstance->_clientSocketId);
}
if (_serverInstance != NULL) {
delete _serverInstance;
}
}
void setUp(std::string port);
void run(); //NOT IMPLEMENTED
void stop(); //NOT IMPLEMENTED
//Accepts a client, adds it to _clientList (TODO), and returns a reference to it
int acceptClient();
void sendMessageToClient(int, std::string);
std::string getMessageFromClient(int);
//Closes connection to client and removes him/her from _clientList
void closeConnection(int);
private:
static Server* _serverInstance;
Server() {};
ClientList _clientList;
sockaddr_in6 _serverSocket;
int _serverSocketId, _clientSocketId;
unsigned _addrLen;
};
} //namespace
#endif