-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpSocket.hpp
287 lines (236 loc) · 6.06 KB
/
TcpSocket.hpp
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#pragma once
#include "Winsock.hpp"
namespace OlympusWebServer
{
class TcpSocket
{
private: // data
bool isBlocking;
bool isListening;
SOCKET socket;
public: // methods
TcpSocket() :
isBlocking(false),
isListening(false),
socket(0)
{
}
TcpSocket(TcpSocket&& b)
{
*this = std::move(b);
}
TcpSocket& operator=(TcpSocket&& b)
{
isBlocking = b.isBlocking;
isListening = b.isListening;
socket = b.socket;
b.isBlocking = false;
b.isListening = false;
b.socket = 0;
return *this;
}
~TcpSocket()
{
Close();
}
bool Accept(TcpSocket& connection, bool blocking = true) const
{
if (!IsListening())
{
throw std::runtime_error("TcpSocket.Accept - Called on a socket that does not have listening mode enabled");
}
auto newSocket = SOCKET(0);
auto address = sockaddr_in();
auto tryAgain = bool();
do
{
tryAgain = false;
if (!Winsock::Accept(socket, address, newSocket))
{
switch (WSAGetLastError())
{
case WSAEWOULDBLOCK:
//if (!IsBlocking())
//{
// throw std::runtime_error("TcpSocket.Accept - Received would-block error for non-blocking socket");
//}
return false;
case WSAECONNRESET:
tryAgain = true;
break;
default:
return false;
}
}
} while (tryAgain);
if (connection.IsOpen())
{
connection.Close();
}
connection.socket = newSocket;
connection.isListening = false;
if (!Winsock::IoctlSocket(connection.socket, blocking))
{
throw std::runtime_error("TcpSocket.Accept - Unable to set blocking mode on new connection");
}
connection.isBlocking = blocking;
return true;
}
void Close()
{
if (!IsOpen())
{
return;
}
if (!Winsock::DestroySocket(socket))
{
throw std::runtime_error("TcpSocket.Close - Error shutting down socket");
}
}
bool IsBlocking() const
{
return isBlocking;
}
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4127)
#endif
bool IsConnected() const
{
if (!IsOpen())
{
return false;
}
auto timeout = timeval();
timeout.tv_sec = 0;
timeout.tv_usec = 1000 * 100;
fd_set writeSet;
FD_ZERO(&writeSet);
FD_SET(socket, &writeSet);
int result = select(0, NULL, &writeSet, NULL, &timeout);
switch (result)
{
case SOCKET_ERROR:
throw std::runtime_error("TcpSocket.IsConnected - Error calling select");
case 0:
return false;
default:
if (FD_ISSET(socket, &writeSet))
{
return true;
}
else
{
return false;
}
}
}
#ifdef _MSC_VER
#pragma warning(pop)
#endif
bool IsListening() const
{
return isListening;
}
bool IsOpen() const
{
return socket != 0;
}
bool Open(bool listen = false, unsigned short port = 0, bool blocking = true)
{
if (IsOpen())
{
throw std::runtime_error("TcpSocket.Open - Open called on socket that is already open");
}
Winsock::Initialize();
auto newSocket = Winsock::CreateTcpSocket();
if (newSocket == INVALID_SOCKET)
{
throw std::runtime_error("TcpSocket.Open - Failed to create a TCP socket");
}
auto loopback = Winsock::GetLoopbackAddress(port);
if (!Winsock::Bind(newSocket, loopback))
{
throw std::runtime_error("TcpSocket.Open - Failed to bind the socket to the given address");
}
if (listen && !Winsock::Listen(newSocket))
{
throw std::runtime_error("TcpSocket.Open - Failed to set socket to listening mode");
}
isListening = listen;
if (!Winsock::IoctlSocket(newSocket, blocking))
{
auto message = std::ostringstream();
message << "TcpSocket.Open - Failed to set socket blocking mode - Error: " << WSAGetLastError();
throw std::runtime_error(message.str());
}
isBlocking = blocking;
socket = newSocket;
return true;
}
std::string Receive()
{
if (!IsOpen())
{
throw std::runtime_error("TcpSocket.Receive - Called on a closed/invalid socket");
}
static const auto bufferSize = 64u * 1024u;
char buffer[bufferSize];
auto recvResult = Winsock::Receive(socket, buffer);
if (recvResult == SOCKET_ERROR)
{
switch (WSAGetLastError())
{
case WSAEWOULDBLOCK:
return std::string();
case WSAENOTCONN:
case WSAENETRESET:
case WSAESHUTDOWN:
case WSAECONNABORTED:
case WSAETIMEDOUT:
case WSAECONNRESET:
Close();
return std::string();
default:
return std::string();
//throw std::runtime_error("TcpSocket.Receive - Unable to receive data over socket");
}
}
if (recvResult == 0) // graceful close
{
Close();
}
return std::string(buffer, recvResult);
}
bool Send(std::string const& data)
{
if (data.empty())
{
return false;
}
if (!IsOpen())
{
throw std::runtime_error("TcpSocket.Send - Called on a closed/invalid socket");
}
if (!IsConnected())
{
throw std::runtime_error("TcpSocket.Send - Called on a socket that is not connected");
}
auto sendResult = Winsock::Send(socket, data);
if (sendResult == SOCKET_ERROR)
{
auto error = WSAGetLastError();
if (error != WSAEWOULDBLOCK)
{
throw std::runtime_error("TcpSocket.Send - Unable to send data over socket");
}
return false;
}
if (static_cast<unsigned>(sendResult) != data.size())
{
return false;
}
return true;
}
};
} // namespace OlympusWebServer