-
Notifications
You must be signed in to change notification settings - Fork 3
/
quicknet_udpsocket.cpp
225 lines (191 loc) · 6 KB
/
quicknet_udpsocket.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
// Copyright (c) 2017 Santiago Fernandez Ortiz
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "quicknet_udpsocket.h"
#include "quicknet_address.h"
#include "quicknet_log.h"
namespace quicknet
{
#ifndef _WIN32
// for linux
# define INVALID_SOCKET ( (int32_t)-1 )
# define SOCKET_ERROR ( (int32_t)-1 )
# include <errno.h>
// socket option parameter pointer type
# define sockoptpp const void*
#else
// socket option parameter pointer type
# define sockoptpp const char*
// Windows requires to start WSA before any socket operation
static bool s_networkInitialized = false;
static void initializeNetwork()
{
if (!s_networkInitialized)
{
WORD wVersionRequested = MAKEWORD(2, 2);
WSADATA wsaData;
WSAStartup(wVersionRequested, &wsaData);
s_networkInitialized = true;
}
}
#endif
static const uint32_t s_defaultMTU = 1400;
// TODO: set these to (lower) good values
static const int32_t s_receiveBufferSize = 256 * 1024;
static const int32_t s_sendBufferSize = 256 * 1024;
UDPSocket::UDPSocket(RawSocket udpSocket)
{
#ifdef _WIN32
initializeNetwork();
#endif
this->m_udpSocket = udpSocket;
}
UDPSocket::UDPSocket(bool isIPv6, int32_t timeout)
{
#ifdef _WIN32
initializeNetwork();
#endif
m_udpSocket = socket(isIPv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (m_udpSocket == INVALID_SOCKET)
{
return;
}
// this is crucial
this->setBlockingMode(false);
setsockopt(m_udpSocket, SOL_SOCKET, SO_RCVBUF, (sockoptpp)&s_receiveBufferSize, sizeof(int32_t));
setsockopt(m_udpSocket, SOL_SOCKET, SO_SNDBUF, (sockoptpp)&s_sendBufferSize, sizeof(int32_t));
// this can be handy but not crucial
int32_t so_reuse = 1;
setsockopt(m_udpSocket, SOL_SOCKET, SO_REUSEADDR, (sockoptpp)&so_reuse, sizeof(so_reuse));
if (timeout > 0)
{
if (!this->SetTimeout(timeout))
{
this->Close();
return;
}
}
}
UDPSocket::~UDPSocket()
{
this->Close();
}
bool UDPSocket::Close()
{
if (m_udpSocket != INVALID_SOCKET)
{
#ifdef _WIN32
closesocket(m_udpSocket);
#else
close(m_udpSocket);
#endif
m_udpSocket = INVALID_SOCKET;
return true;
}
return false;
}
bool UDPSocket::Bind(const Address& address)
{
if (!this->IsValid()) { return false; }
int32_t success = bind(m_udpSocket, &address.SockAddrc(), sizeof(address.SockAddrc()));
return !(success < 0);
}
bool UDPSocket::Send(const Address& remote, uint8_t* data, uint32_t dataLength, uint32_t* bytesSent)
{
if (!this->IsValid() || (data == nullptr)) { return false; }
if (dataLength >= s_defaultMTU)
{
Log::Warn("Trying to send a buffer bigger than MTU");
}
int32_t result = sendto(m_udpSocket, (const char*)data, dataLength, 0, &remote.SockAddrc(), sizeof(remote.SockAddrc()));
if (result == SOCKET_ERROR)
{
return false;
}
*bytesSent = result;
return true;
}
bool UDPSocket::Recv(uint8_t* buffer, uint32_t bufferSize, uint32_t* bytesRead, Address& remote)
{
if (!this->IsValid() || (buffer == nullptr)) { return false; }
*bytesRead = 0;
#ifdef _WIN32
int32_t remoteSize;
#else
socklen_t remoteSize;
#endif
remoteSize = sizeof(remote.SockAddr());
int32_t result = recvfrom(m_udpSocket, (char*)buffer, bufferSize, 0, &remote.SockAddr(), &remoteSize);
if (result == SOCKET_ERROR)
{
int32_t error = this->getLastNetworkError();
return (error == EWOULDBLOCK || error == EAGAIN);
}
*bytesRead = result;
return true;
}
bool UDPSocket::SetTimeout(int32_t timeout)
{
if (!this->IsValid()) { return false; }
#ifdef _WIN32
int32_t success = setsockopt(m_udpSocket, SOL_SOCKET, SO_RCVTIMEO, (sockoptpp)&timeout, sizeof(int32_t));
#else
struct timeval timeoutv;
timeoutv.tv_sec = 0;
timeoutv.tv_usec = timeout * 1000;
int32_t success = setsockopt(m_udpSocket, SOL_SOCKET, SO_RCVTIMEO, (sockoptpp)&timeoutv, sizeof(timeout));
#endif
return (success == SOCKET_ERROR);
}
bool UDPSocket::AllowBroadcast(bool allow)
{
// TODO: set INADDR_BROADCAST? Not required in Windows at least
if (!this->IsValid()) { return false; }
uint32_t enable = allow ? 1 : 0;
int32_t success = setsockopt(m_udpSocket, SOL_SOCKET, SO_BROADCAST, (sockoptpp)&enable, sizeof(int32_t));
return (success == SOCKET_ERROR);
}
bool UDPSocket::IsValid()
{
return m_udpSocket != INVALID_SOCKET;
}
bool UDPSocket::setBlockingMode(bool blocking)
{
if (!this->IsValid()) { return false; }
#ifdef _WIN32
uint64_t mode = blocking ? 0 : 1;
int result = ioctlsocket(m_udpSocket, FIONBIO, (u_long*)&mode);
#else
int flags = fcntl(m_udpSocket, F_GETFL, 0);
if (flags < 0) return false;
flags = blocking ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);
int result = fcntl(m_udpSocket, F_SETFL, flags);
#endif
return result == 0;
}
uint32_t UDPSocket::getLastNetworkError()
{
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
}