Boost asio udp read optimization questions

Peter

The server app I am working on receives data over udp from my client app.

The server must receive udp data asynchronously. It appears there are two general ways to accomplish this.

Method 1:

boost::array<char, 65000> recv_buffer;
socket.async_receive_from(boost::asio::buffer(recv_buffer), senderEndpoint, handler);

Method 2 (from Variable-size buffer for receiving UDP packets):

socket.async_receive(boost::asio::null_buffers(), receive_handler);

// in the handler code
unsigned int available = socket.available();
buffer = resize_buffer_if_needed(available);
unsigned int packetSize = socket.receive_from(boost::asio::buffer(buffer, available), senderEndpoint, 0, ec);

Please help me evaluate the method that may be more apt for my needs. Regards.

Question 1:

From the mentioned post, it appears method 2 is inefficient as it causes asio to store the data into an internal buffer first and then copy it to my buffer. Is this correct?

Question 2:

The client will never send more than 64K of data in a single call to socket.send_to().. Given this, is method 1 always a better choice?

Question 3:

I need to provide a way to reduce attacks on my server. I was thinking of using the first two bytes as a magic key. The idea is that I would ignore a message if the first two bytes are not the expected magic key. Given this, is it better to use method 2?

Question 4:

In my design, the next four bytes were going to be the actual size of the binary data that follows. However, given that socket.available() already gives me the length, it appears I don't really have to send this information. Is it okay to simply rely on socket.available() for the length?

sehe
  1. No it doesn't store it in a temporary buffer first. It uses FIONREAD ioctl to see what is in the kernel's IP stack buffers.

  2. I'd say yes, because the code is simpler and never needs to do any dynamic allocation

  3. No, because you'll still receive whole packets at a time, which is the nature of datagram sockets. Just guard the maximum buffer size and avoid parsing untrusted data. A "magic number" is hardly any protection though. It's excruciatingly simple for an attacker to adapt (there's only 65536 possible combinations). Much better to have a HMAC-sgin (or similar) to authenticate the packet.

  4. It's still UDP, so there's hardly any gain receiving in batches (the kernel doesn't so it's just more back-and-forth adding more CPU load for the same latency).

    Also:

    Q. In my design, the next four bytes were going to be the actual size of the binary data that follows

    Beware of authentication first. Don't parse untrusted data. See Encrypt-then-MAC by Colin Percival

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

boost::asio UDP "gather" operation

From Dev

boost asio async udp server - poor performance

From Dev

Boost::asio UDP Broadcast with ephemeral port

From Dev

Get UDP datagram length via Boost Asio?

From Dev

boost asio async udp server - poor performance

From Dev

boost::asio::read() blocks forever

From Dev

Boost asio bind read handler

From Dev

boost::asio read/write trouble

From Dev

Boost asio asynchronous read and then write

From Dev

Boost::Asio - read or read_until?

From Dev

Boost::Asio - read or read_until?

From Dev

boost asio cancelling read without cancelling write

From Dev

c++, boost asio, slow read

From Dev

Read Binary Data over TCP with Boost::Asio

From Dev

Boost asio trouble with async_read_until

From Dev

boost asio cancelling read without cancelling write

From Dev

boost asio multiple async_send on udp socket

From Dev

How to increase throughput of Boost ASIO, UDP client application

From Dev

How to receive a UDP broadcast sent to 255.255.255.255 using boost asio?

From Dev

Write directly to cout using boost's boost::asio::read

From Dev

Boost Asio: Some questions about the tutorial (A synchronous daytime server/client)

From Dev

Concurrent read and async_read_some in boost asio

From Dev

Boost asio async_read_until followed by async_read

From Dev

boost::asio::read prevents boost:asio::write from sending data to Java Socket

From Dev

boost::asio::read throws compilation error 'read_some' is not a member of 'boost::shared_ptr<boost::asio::ip::tcp::socket>'

From Dev

boost::asio how to read full buffer in right way?

From Dev

boost::asio::async_read 100% CPU usage on simple example

From Dev

Boost asio async_read_some returning small amount of data

From Dev

boost::asio::ip::tcp::socket doesn't read anything

Related Related

  1. 1

    boost::asio UDP "gather" operation

  2. 2

    boost asio async udp server - poor performance

  3. 3

    Boost::asio UDP Broadcast with ephemeral port

  4. 4

    Get UDP datagram length via Boost Asio?

  5. 5

    boost asio async udp server - poor performance

  6. 6

    boost::asio::read() blocks forever

  7. 7

    Boost asio bind read handler

  8. 8

    boost::asio read/write trouble

  9. 9

    Boost asio asynchronous read and then write

  10. 10

    Boost::Asio - read or read_until?

  11. 11

    Boost::Asio - read or read_until?

  12. 12

    boost asio cancelling read without cancelling write

  13. 13

    c++, boost asio, slow read

  14. 14

    Read Binary Data over TCP with Boost::Asio

  15. 15

    Boost asio trouble with async_read_until

  16. 16

    boost asio cancelling read without cancelling write

  17. 17

    boost asio multiple async_send on udp socket

  18. 18

    How to increase throughput of Boost ASIO, UDP client application

  19. 19

    How to receive a UDP broadcast sent to 255.255.255.255 using boost asio?

  20. 20

    Write directly to cout using boost's boost::asio::read

  21. 21

    Boost Asio: Some questions about the tutorial (A synchronous daytime server/client)

  22. 22

    Concurrent read and async_read_some in boost asio

  23. 23

    Boost asio async_read_until followed by async_read

  24. 24

    boost::asio::read prevents boost:asio::write from sending data to Java Socket

  25. 25

    boost::asio::read throws compilation error 'read_some' is not a member of 'boost::shared_ptr<boost::asio::ip::tcp::socket>'

  26. 26

    boost::asio how to read full buffer in right way?

  27. 27

    boost::asio::async_read 100% CPU usage on simple example

  28. 28

    Boost asio async_read_some returning small amount of data

  29. 29

    boost::asio::ip::tcp::socket doesn't read anything

HotTag

Archive