-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.cpp
234 lines (213 loc) · 5.79 KB
/
Server.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include "Server.h"
#include "Utils.h"
#include <sys/socket.h>
#include <poll.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
using std::vector;
using std::map;
Server::Server(short port, Logger& logger, const Config& config) :
m_mainSocket(0),
m_sockets(0),
m_socketCount(0),
m_port(port),
m_shouldQuit(false),
m_logger(logger),
m_config(config)
{
m_address.sin_family = AF_INET;
m_address.sin_port = htons(m_port);
m_address.sin_addr.s_addr = htonl(INADDR_ANY);
}
bool Server::bindPort()
{
m_mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (m_mainSocket == -1)
{
m_logger.logError("Socket error(" + Utils::llToString(errno) + "): " + strerror(errno));
return false;
}
int reuseAddr = 1;
if (setsockopt(m_mainSocket, SOL_SOCKET, SO_REUSEADDR, (char*)&reuseAddr, sizeof(reuseAddr)))
{
m_logger.logError("Setsockopt error(" + Utils::llToString(errno) + "): " + strerror(errno));
close(m_mainSocket);
return false;
}
if (bind(m_mainSocket, (struct sockaddr*)&m_address, sizeof(m_address)))
{
m_logger.logError("Bind error(" + Utils::llToString(errno) + "): " + strerror(errno));
close(m_mainSocket);
return false;
}
return true;
}
bool Server::start()
{
if (listen(m_mainSocket, 10))
{
m_logger.logError("Listen error(" + Utils::llToString(errno) + "): " + strerror(errno));
close(m_mainSocket);
return false;
}
initSocketList();
while (m_shouldQuit == false)
{
int n = poll(m_sockets, m_socketCount, 1000);
if (n == -1)
{
if (errno == EINTR)
{
// This happens when we try and exit. For a legit exit, m_shouldQuit should be set. For some other signal,
// we'll just try and continue.
continue;
}
m_logger.logError("Poll error(" + Utils::llToString(errno) + "): " + strerror(errno));
close(m_mainSocket);
return false;
}
// No socket activity, just start the next loop.
if (n == 0)
{
continue;
}
int socketToWatch = -1;
vector<int> socketsToUnwatch;
for (int i = 0; i < m_socketCount; ++i)
{
// General error check.
if (m_sockets[i].revents & POLLERR)
{
socketsToUnwatch.push_back(m_sockets[i].fd);
continue;
}
if (m_sockets[i].revents & POLLHUP)
{
socketsToUnwatch.push_back(m_sockets[i].fd);
continue;
}
if (m_sockets[i].revents & POLLNVAL)
{
socketsToUnwatch.push_back(m_sockets[i].fd);
continue;
}
// Special socket.
if (m_sockets[i].fd == m_mainSocket)
{
if (m_sockets[i].revents & POLLIN)
{
struct sockaddr_in newAddress;
socklen_t newAddressSize = sizeof(newAddress);
int newSocket = accept(m_mainSocket, (struct sockaddr*)&m_address, &newAddressSize);
if (newSocket != -1)
{
socketToWatch = newSocket;
}
else
{
m_logger.logError("Accept error(" + Utils::llToString(errno) + "): " + strerror(errno));
}
}
}
else
{
bool isFinished = m_connections[m_sockets[i].fd]->process(m_sockets[i].events, m_sockets[i].revents);
if (isFinished)
{
socketsToUnwatch.push_back(m_sockets[i].fd);
}
}
}
// @TODO: Timeout expiration goes here (or possibly after the 'unwatchSockets' call below).
// Rebuild our socket list if anything has changed.
unwatchSockets(socketsToUnwatch);
watchSocket(socketToWatch);
}
return true;
}
void Server::stop()
{
m_shouldQuit = true;
}
void Server::initSocketList()
{
m_socketCount = 1;
free(m_sockets);
m_sockets = (struct pollfd*)malloc(sizeof(struct pollfd) * m_socketCount);
if (m_sockets == 0)
{
m_logger.logError("InitSocketList malloc error: Out of Memory");
exit(1);
}
m_sockets[0].fd = m_mainSocket;
m_sockets[0].events = POLLIN;
m_sockets[0].revents = 0;
}
void Server::watchSocket(int socket)
{
if (socket < 0)
{
// not a real socket, do nothing.
return;
}
m_socketCount++;
m_sockets = (struct pollfd*)realloc(m_sockets, sizeof(struct pollfd) * m_socketCount);
if (m_sockets == 0)
{
// Try and log the error and close the socket. Maybe someone will free some RAM.
// In all likelihood, we're hosed if we even get here, though.
m_logger.logError("InitSocketList realloc error: Out of Memory");
shutdown(socket, SHUT_RDWR);
close(socket);
return;
}
m_sockets[m_socketCount - 1].fd = socket;
m_sockets[m_socketCount - 1].events = POLLIN;
m_sockets[m_socketCount - 1].revents = 0;
m_connections[socket] = new Connection(socket, m_logger, m_config);
}
void Server::unwatchSockets(const vector<int>& socketsToUnwatch)
{
if (socketsToUnwatch.size() == 0)
{
return;
}
int shiftBy = 0;
vector<int>::const_iterator it = socketsToUnwatch.begin();
for (int i = 0; i < m_socketCount; ++i)
{
// If this is one of the sockets we're closing, we'll do that.
if (m_sockets[i].fd == *it)
{
shutdown(*it, SHUT_RDWR);
close(*it);
delete m_connections[*it];
m_connections.erase(*it);
++shiftBy;
++it;
}
// Otherwise, we shift our array entries down.
else if(shiftBy != 0)
{
memcpy(&m_sockets[i - shiftBy], &m_sockets[i], sizeof(struct pollfd));
}
}
m_socketCount -= shiftBy;
m_sockets = (struct pollfd*)realloc(m_sockets, sizeof(struct pollfd) * m_socketCount);
if (m_sockets == 0)
{
// Seems pretty much impossible since we're *shrinking* it, but whatever.
m_logger.logError("Unwatchsockets realloc error.");
}
}
Server::~Server()
{
vector<int> socketsToUnwatch;
for (int i = 0; i < m_socketCount; ++i)
{
socketsToUnwatch.push_back(m_sockets[i].fd);
}
unwatchSockets(socketsToUnwatch);
free(m_sockets);
}