-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUdpSocket.cpp
More file actions
300 lines (218 loc) · 8.23 KB
/
UdpSocket.cpp
File metadata and controls
300 lines (218 loc) · 8.23 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <SFNUL/UdpSocket.hpp>
#include <SFNUL/ConfigInternal.hpp>
#include <SFNUL/Utility.hpp>
#include <SFNUL/Endpoint.hpp>
#include <SFNUL/IpAddressImpl.hpp>
#include <SFNUL/MakeUnique.hpp>
#include <asio/buffer.hpp>
#include <asio/ip/udp.hpp>
#include <asio/strand.hpp>
#include <functional>
#include <algorithm>
namespace {
struct UdpSocketMaker : public sfn::UdpSocket {};
}
namespace sfn {
class UdpSocket::UdpSocketImpl {
public:
UdpSocketImpl( UdpSocket* owner ) :
udp_socket{ owner },
asio_socket{ *static_cast<asio::io_context*>( owner->GetIOService() ) }
{
}
void SendHandler( const asio::error_code& error, std::size_t bytes_sent, asio::ip::udp::endpoint endpoint, std::shared_ptr<std::vector<char>> buffer ) {
{
auto lock = udp_socket->AcquireLock();
if( ( error == asio::error::operation_aborted ) || ( error == asio::error::connection_aborted ) || ( error == asio::error::connection_reset ) ) {
return;
}
else if( error ) {
ErrorMessage() << "Async Send Error: " << error.message() << "\n";
return;
}
if( bytes_sent == buffer->size() ) {
return;
}
else {
auto new_buffer = std::make_shared<std::vector<char>>( buffer->size() - bytes_sent );
std::copy_n( buffer->begin() + static_cast<int>( bytes_sent ), new_buffer->size(), new_buffer->begin() );
asio_socket.async_send_to( asio::buffer( *new_buffer ), endpoint,
static_cast<asio::io_context::strand*>( udp_socket->GetStrand() )->wrap(
std::bind(
[]( std::weak_ptr<UdpSocket> socket, const asio::error_code& handler_error, std::size_t handler_bytes_sent, asio::ip::udp::endpoint handler_endpoint, std::shared_ptr<std::vector<char>> handler_buffer ) {
auto shared_socket = socket.lock();
if( !shared_socket ) {
return;
}
auto handler_lock = shared_socket->AcquireLock();
shared_socket->m_impl->SendHandler( handler_error, handler_bytes_sent, handler_endpoint, handler_buffer );
},
std::weak_ptr<UdpSocket>( udp_socket->shared_from_this() ), std::placeholders::_1, std::placeholders::_2, endpoint, new_buffer
)
)
);
}
}
if( bytes_sent ) {
udp_socket->OnSent();
}
}
void ReceiveHandler( const asio::error_code& error, std::size_t bytes_received, std::shared_ptr<asio::ip::udp::endpoint> endpoint_ptr ) {
{
auto lock = udp_socket->AcquireLock();
if( receiving ) {
return;
}
if( ( error == asio::error::operation_aborted ) || ( error == asio::error::connection_aborted ) || ( error == asio::error::connection_reset ) ) {
return;
}
else if( error ) {
ErrorMessage() << "Async Receive Error: " << error.message() << "\n";
return;
}
if( bytes_received ) {
auto endpoint = *endpoint_ptr;
receive_buffer[endpoint].insert( receive_buffer[endpoint].end(), receive_memory.begin(), receive_memory.begin() + bytes_received );
pending_data += bytes_received;
}
if( pending_data < GetMaximumBlockSize() ) {
std::shared_ptr<asio::ip::udp::endpoint> receive_endpoint_ptr = std::make_shared<asio::ip::udp::endpoint>();
receiving = true;
asio_socket.async_receive_from( asio::buffer( receive_memory ), *receive_endpoint_ptr,
static_cast<asio::io_context::strand*>( udp_socket->GetStrand() )->wrap(
std::bind(
[]( std::weak_ptr<UdpSocket> socket, const asio::error_code& handler_error, std::size_t handler_bytes_received, std::shared_ptr<asio::ip::udp::endpoint> handler_endpoint_ptr ) {
auto shared_socket = socket.lock();
if( !shared_socket ) {
return;
}
auto handler_lock = shared_socket->AcquireLock();
shared_socket->m_impl->receiving = false;
shared_socket->m_impl->ReceiveHandler( handler_error, handler_bytes_received, handler_endpoint_ptr );
},
std::weak_ptr<UdpSocket>( udp_socket->shared_from_this() ), std::placeholders::_1, std::placeholders::_2, receive_endpoint_ptr
)
)
);
}
}
if( bytes_received ) {
udp_socket->OnReceived();
}
}
UdpSocket* udp_socket;
asio::ip::udp::socket asio_socket;
std::map<asio::ip::udp::endpoint, std::vector<char>> receive_buffer;
std::array<char, 2048> receive_memory;
std::size_t pending_data = 0;
bool receiving = false;
};
UdpSocket::UdpSocket() :
m_impl{ make_unique<UdpSocketImpl>( this ) }
{
}
UdpSocket::~UdpSocket() {
Close();
}
UdpSocket::Ptr UdpSocket::Create() {
return std::make_shared<UdpSocketMaker>();
}
void UdpSocket::Bind( const Endpoint& endpoint ) {
auto lock = AcquireLock();
auto asio_endpoint = asio::ip::basic_endpoint<asio::ip::udp>{ endpoint.GetAddress().m_impl->address, endpoint.GetPort() };
if( !m_impl->asio_socket.is_open() ) {
m_impl->asio_socket.open( asio_endpoint.protocol() );
}
asio::error_code error;
m_impl->asio_socket.bind( asio_endpoint, error );
if( error ) {
ErrorMessage() << "Bind() Error: " << error.message() << "\n";
return;
}
m_impl->ReceiveHandler( asio::error_code{}, 0, nullptr );
}
void UdpSocket::Close() {
auto lock = AcquireLock();
if( !m_impl->asio_socket.is_open() ) {
return;
}
m_impl->asio_socket.close();
}
void UdpSocket::SendTo( const void* data, std::size_t size, const Endpoint& endpoint ) {
auto lock = AcquireLock();
if( !data || !size ) {
return;
}
auto asio_endpoint = asio::ip::basic_endpoint<asio::ip::udp>{ endpoint.GetAddress().m_impl->address, endpoint.GetPort() };
if( !m_impl->asio_socket.is_open() ) {
m_impl->asio_socket.open( asio_endpoint.protocol() );
}
auto buffer = std::make_shared<std::vector<char>>( size );
std::memcpy( &( (*buffer)[0] ), data, size );
m_impl->SendHandler( asio::error_code{}, 0, asio_endpoint, buffer );
}
std::size_t UdpSocket::ReceiveFrom( void* data, std::size_t size, const Endpoint& endpoint ) {
auto lock = AcquireLock();
if( !data || !size ) {
return 0;
}
auto asio_endpoint = asio::ip::basic_endpoint<asio::ip::udp>{ endpoint.GetAddress().m_impl->address, endpoint.GetPort() };
if( !m_impl->asio_socket.is_open() ) {
m_impl->asio_socket.open( asio_endpoint.protocol() );
}
auto receive_size = std::min( size, m_impl->receive_buffer[asio_endpoint].size() );
for( std::size_t index = 0; index < receive_size; index++ ) {
static_cast<char*>( data )[index] = m_impl->receive_buffer[asio_endpoint][index];
}
m_impl->receive_buffer[asio_endpoint].erase( m_impl->receive_buffer[asio_endpoint].begin(), m_impl->receive_buffer[asio_endpoint].begin() + static_cast<int>( receive_size ) );
if( m_impl->receive_buffer[asio_endpoint].empty() ) {
m_impl->receive_buffer.erase( asio_endpoint );
}
auto start = false;
if( ( m_impl->pending_data >= GetMaximumBlockSize() ) && ( m_impl->pending_data - receive_size < GetMaximumBlockSize() ) ) {
start = true;
}
m_impl->pending_data -= receive_size;
if( start ) {
m_impl->ReceiveHandler( asio::error_code{}, 0, nullptr );
}
return receive_size;
}
Endpoint UdpSocket::GetLocalEndpoint() const {
auto lock = AcquireLock();
IpAddress address;
address.m_impl->address = m_impl->asio_socket.local_endpoint().address();
return { address, m_impl->asio_socket.local_endpoint().port() };
}
void UdpSocket::ClearBuffers() {
auto lock = AcquireLock();
m_impl->receive_buffer.clear();
}
std::size_t UdpSocket::BytesToReceive( const Endpoint& endpoint ) const {
auto lock = AcquireLock();
auto asio_endpoint = asio::ip::basic_endpoint<asio::ip::udp>{ endpoint.GetAddress().m_impl->address, endpoint.GetPort() };
auto iterator = m_impl->receive_buffer.find( asio_endpoint );
if( iterator == m_impl->receive_buffer.end() ) {
return 0;
}
return iterator->second.size();
}
std::deque<Endpoint> UdpSocket::PendingEndpoints() const {
auto lock = AcquireLock();
std::deque<Endpoint> endpoints;
for( auto buffer : m_impl->receive_buffer ) {
IpAddress address;
address.m_impl->address = buffer.first.address();
endpoints.emplace_back( address, buffer.first.port() );
}
return endpoints;
}
/// @cond
void UdpSocket::SetInternalSocket( void* internal_socket ) {
m_impl->asio_socket = std::move( *static_cast<asio::ip::udp::socket*>( internal_socket ) );
}
/// @endcond
}