| 347 | } |
| 348 | |
| 349 | bool UDPServer::SendAsync(const asio::ip::udp::endpoint& endpoint, const void* buffer, size_t size) |
| 350 | { |
| 351 | if (_sending) |
| 352 | return false; |
| 353 | |
| 354 | if (!IsStarted()) |
| 355 | return false; |
| 356 | |
| 357 | if (size == 0) |
| 358 | return true; |
| 359 | |
| 360 | assert((buffer != nullptr) && "Pointer to the buffer should not be null!"); |
| 361 | if (buffer == nullptr) |
| 362 | return false; |
| 363 | |
| 364 | // Check the send buffer limit |
| 365 | if ((size > _send_buffer_limit) && (_send_buffer_limit > 0)) |
| 366 | { |
| 367 | SendError(asio::error::no_buffer_space); |
| 368 | |
| 369 | // Call the buffer sent zero handler |
| 370 | onSent(_send_endpoint, 0); |
| 371 | |
| 372 | return false; |
| 373 | } |
| 374 | |
| 375 | // Fill the main send buffer |
| 376 | const uint8_t* bytes = (const uint8_t*)buffer; |
| 377 | _send_buffer.assign(bytes, bytes + size); |
| 378 | |
| 379 | // Update statistic |
| 380 | _bytes_sending = _send_buffer.size(); |
| 381 | |
| 382 | // Update send endpoint |
| 383 | _send_endpoint = endpoint; |
| 384 | |
| 385 | // Async send-to with the send-to handler |
| 386 | _sending = true; |
| 387 | auto self(this->shared_from_this()); |
| 388 | auto async_send_to_handler = make_alloc_handler(_send_storage, [this, self](std::error_code ec, size_t sent) |
| 389 | { |
| 390 | _sending = false; |
| 391 | |
| 392 | if (!IsStarted()) |
| 393 | return; |
| 394 | |
| 395 | // Check for error |
| 396 | if (ec) |
| 397 | { |
| 398 | SendError(ec); |
| 399 | |
| 400 | // Call the buffer sent zero handler |
| 401 | onSent(_send_endpoint, 0); |
| 402 | |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | // Send some data to the client |