-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.cpp
More file actions
264 lines (225 loc) · 6.89 KB
/
socket.cpp
File metadata and controls
264 lines (225 loc) · 6.89 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
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
#include "socket.h"
#include "helpers.h"
#include <iostream>
int getMaxSocketDescriptorValue(const std::vector<Socket*>& first,
const std::vector<Socket*>& second,
const std::vector<Socket*>& third);
SelectResult buildSelectResult(bool ready, fd_set& r, fd_set& w, fd_set& e,
const std::vector<Socket*>& r_original,
const std::vector<Socket*>& w_original,
const std::vector<Socket*>& e_original);
Socket::Socket() {}
Socket::Socket(SocketType type, const std::string& ip, unsigned short port)
{
if (asBool(Create(type)))
if (ip != "" && port)
Bind(ip, port);
}
Socket::Socket(const sockets::Socket& socket)
{
isValid = socket.isValid;
this->socket = socket;
}
Socket::Socket(Socket&& rhs)
{
operator =(std::move(rhs));
}
Socket& Socket::operator=(Socket&& rhs)
{
this->~Socket();
socket = rhs.socket;
isValid = rhs.isValid;
isBound = rhs.isBound;
ip = rhs.ip;
port = rhs.port;
rhs.isValid = false;
return *this;
}
Socket& Socket::operator=(const sockets::Socket& rhs)
{
isValid = rhs.isValid;
socket = rhs;
return *this;
}
Socket::~Socket()
{
if (isValid)
{
std::cerr << "Closing socket" << std::endl;
isValid = false;
sockets::closeSocket(socket);
}
}
OperationResult Socket::Bind(const std::string& ip, unsigned short port)
{
this->ip = ip;
this->port = port;
isValid = sockets::bindSocket(socket, {port, ip, sockets::INET});
isBound = isValid;
return fromBool(isValid);
}
OperationResult Socket::Listen(int maxClientsNumber)
{
return fromBool(sockets::listen(socket, maxClientsNumber));
}
OperationResult Socket::Create(SocketType type)
{
socket = sockets::createSocket(sockets::INET, helpers::integral(type), 0);
isValid = socket.isValid;
return fromBool(isValid);
}
OperationResult Socket::Send(Buffer& data, size_t size) const
{
if (!isValid)
return OperationResult::Error;
return sockets::sendData(socket, data, size);
}
OperationResult Socket::SendTo(Buffer &data, size_t size, const std::string& ip, unsigned short port)
{
if (!isValid)
return OperationResult::Error;
return sockets::sendDataTo(socket, data, size, {port, ip, sockets::INET});
}
OperationResult Socket::Recieve(Buffer& buff, size_t size, bool peek) const
{
if (!isValid)
return OperationResult::Error;
return sockets::getData(socket, buff, size, peek);
}
OperationResult Socket::RecieveFrom(Buffer &buff, size_t size, std::string& ip, unsigned short& port, bool peek)
{
if (!isValid)
return OperationResult::Error;
sockets::AddressIn addr;
OperationResult res = sockets::getDataFrom(socket, buff, size, addr, peek);
ip = addr.ip;
port = addr.port;
return res;
}
void Socket::Invalidate()
{
isValid = false;
}
sockets::Socket& Socket::getSocket()
{
return socket;
}
const sockets::Socket& Socket::getSocket() const
{
return socket;
}
bool Socket::IsValid() const
{
return isValid;
}
OperationResult Socket::Accept(Socket& client) const
{
sockets::AddressIn clientAddr;
if (!isValid || !isBound)
return OperationResult::Error;
client = sockets::accept(socket, clientAddr);
client.ip = clientAddr.ip;
client.port = clientAddr.port;
return fromBool(client.isValid);
}
OperationResult Socket::Connect(const std::string& ip, unsigned short port)
{
return fromBool(sockets::connect(socket, {port, ip, sockets::INET}));
}
std::string Socket::getIp() const
{
return ip;
}
unsigned short Socket::getPort() const
{
return port;
}
bool Socket::IsConnected(bool OOB) const
{
return sockets::isConnectionAlive(socket, OOB);
}
void Socket::SetReadTimeout(int timeout)
{
sockets::setReadTimeout(socket, timeout);
}
void Socket::SetWriteTimeout(int timeout)
{
sockets::setWriteTimeout(socket, timeout);
}
bool Socket::operator==(const Socket& rhs)
{
return (ip == rhs.ip) && (port == rhs.port);
}
SelectResult select(const std::vector<Socket*>& readable, const std::vector<Socket*>& writeable, const std::vector<Socket*>& haveExceptions, int timeout)
{
fd_set r, w, o;
bool read = readable.size() != 0;
bool write = writeable.size() != 0;
bool oob = haveExceptions.size() != 0;
timeval time = {0, timeout};
int maxDescriptorValue = getMaxSocketDescriptorValue(writeable, readable, haveExceptions);
FD_ZERO(&r);
FD_ZERO(&w);
FD_ZERO(&o);
for (const Socket* sock: readable)
FD_SET(sock->getSocket().socket, &r);
for (const Socket* sock: writeable)
FD_SET(sock->getSocket().socket, &w);
for (const Socket* sock: haveExceptions)
FD_SET(sock->getSocket().socket, &o);
bool isAnyReady = ::select(maxDescriptorValue + 1,
read ? &r : nullptr,
write ? &w : nullptr,
oob ? &o : nullptr,
&time) > 0;
return buildSelectResult(isAnyReady, r, w, o, readable, writeable, haveExceptions);
}
int getMaxSocketDescriptorValue(const std::vector<Socket*>& first,
const std::vector<Socket*>& second,
const std::vector<Socket*>& third)
{
int maxVal = -1, tmp = -1;
if ((tmp = helpers::getMaxSocketDescriptor(first)) > maxVal)
maxVal = tmp;
if ((tmp = helpers::getMaxSocketDescriptor(second)) > maxVal)
maxVal = tmp;
if ((tmp = helpers::getMaxSocketDescriptor(third)) > maxVal)
maxVal = tmp;
return maxVal;
}
SelectResult buildSelectResult(bool ready, fd_set& r, fd_set& w, fd_set& e,
const std::vector<Socket*>& r_original,
const std::vector<Socket*>& w_original,
const std::vector<Socket*>& e_original)
{
SelectResult result;
result.isAnyReady = ready;
for (const Socket* sock: r_original)
if (FD_ISSET(sock->getSocket().socket, &r))
result.readable.push_back((Socket*)sock);
for (const Socket* sock: w_original)
if (FD_ISSET(sock->getSocket().socket, &w))
result.writeable.push_back((Socket*)sock);
for (const Socket* sock: e_original)
if (FD_ISSET(sock->getSocket().socket, &e))
result.haveExceptions.push_back((Socket*)sock);
return result;
}
OperationResult fromBool(bool result)
{
if (result)
return OperationResult::Success;
return OperationResult::Error;
}
bool asBool(const OperationResult& result)
{
if (result == OperationResult::Error || result == OperationResult::ConnectionClosed)
return false;
return true;
}
bool isEndpointState(const OperationResult& result)
{
if (result == OperationResult::PartiallyFinished || result == OperationResult::Timeout)
return false;
return true;
}