简体   繁体   English

C ++异步网络编程

[英]C++ asynchronous network programming

I have some simple questions, I have a client-server application and data sent on the wire. 我有一些简单的问题,我有一个客户端服务器应用程序和在线发送的数据。

I'd like to be able to recover the data and handle it properly. 我希望能够恢复数据并正确处理它。

struct T1
{
   int id;
   int foo;
};

struct T2
{
  int id;
  char foo;
  int bar;
};

Let's take these structures, they are sent on the network preceded by an int which will tell if either T1 or T2 follows. 让我们采用这些结构,它们是在网络上发送的,前面是一个整数,该整数将指示是否跟随T1或T2。 If the client sends me T1, then sends me T2, do I have the assurance that I can read a full structure with asio::ip::tcp::socket.async_read() ? 如果客户端向我发送了T1,然后向我发送了T2,我是否可以保证可以使用asio :: ip :: tcp :: socket.async_read()读取完整的结构? I'd like to set up a handler which will handle a single struct, however what will happen if I'm unable to read everything in a single async_read() ? 我想设置一个处理单个结构的处理程序,但是如果我无法在单个async_read()中读取所有内容,将会发生什么?

The asynchronous operation will continue until one of the following conditions is true: 异步操作将继续,直到满足以下条件之一:

  • The supplied buffers are full. 提供的缓冲区已满。 That is, the bytes transferred is equal to the sum of the buffer sizes. 即,传输的字节等于缓冲区大小的总和。
  • An error occurred. 发生错误。

Will it discard the data that cannot be read ? 它会丢弃无法读取的数据吗? will it trigger another async_read ? 它会触发另一个async_read吗? And, am I assured that an async_read will only get one ID+structure if my client sends me sequentially the ID+structure ? 而且,如果我的客户按顺序向我发送ID +结构,我是否可以确保async_read仅会获得一个ID +结构? Or may the OS optimize things and put them both in the same paquet ? 还是操作系统可以优化事情,然后将它们放在同一块地板上? As you may have seen, I'm a little confused, I'd like to make the right decisions when designing a server/client application, any help will be appreciated. 如您所见,我有些困惑,我想在设计服务器/客户端应用程序时做出正确的决定,我们将不胜感激。

Thank you. 谢谢。

EDIT: Thanks to @timo for pointing out a mistake. 编辑:感谢@timo指出一个错误。 async_read won't complete until an entire struct is read, so you don't need to loop. 在读取整个结构之前,async_read不会完成,因此您无需循环。 Otherwise, my answer is the same. 否则,我的答案是相同的。 Nothing will be lost, regardless of how the TCP protocol breaks up or coalesces the data. 无论TCP协议如何分解或合并数据,都不会丢失任何内容。

If there isn't enough data in the TCP buffers to fill your input buffer, the read will simply fetch whatever is available, and report the number of bytes fetched. 如果TCP缓冲区中的数据不足以填充您的输入缓冲区,则读取操作将简单地获取所有可用数据,并报告获取的字节数。 What happens next is up to you. 接下来会发生什么取决于您。 You might have enough data in the fetched bytes to decide you don't want to continue, so asio makes no assumptions. 您可能在所提取的字节中有足够的数据来确定您不想继续,因此asio不作任何假设。 If you haven't read enough bytes to comprise a complete structure, then initiate a further async_read, repeating the process until you have enough bytes or something dies. 如果您没有读取足够的字节来构成一个完整的结构,请启动另一个async_read,重复此过程,直到您有足够的字节或死了。

I'm not sure what you mean by, "unable to read everything." 我不确定“无法阅读所有内容”的意思。 two possible meanings come to mind: 我想到了两种可能的含义:

  1. The amount of data that's available to read doesn't fill a struct. 可读取的数据量未填充结构。 In this case, you simply do another read_async to wait for more data to arrive. 在这种情况下,您只需执行另一个read_async即可等待更多数据到达。
  2. The struct doesn't absorb all that data that has arrived. 该结构不会吸收所有已到达的数据。 The TCP stack will simply buffer unread data until you get around to reading it. TCP堆栈将仅缓冲未读的数据,直到您开始阅读为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM