boost:asio async_receive_from 行为同步

发布于 2024-11-17 12:51:25 字数 4013 浏览 3 评论 0原文

在下面的代码中,asynch_receive_from 的行为是同步的。在收到数据之前它不会返回。查看最后的日志就表明了这一点。这可能不是那个 一开始很明显,因为数据传入的速率相当高。但是当没有数据,只有一秒的心跳消息时,这些异步调用的返回时间也约为一秒。

问题是为什么这些异步调用没有立即返回(在几微秒内),并且没有读取任何字节?

我认为运行队列中的其他项目可能会促进该过程,认为如果队列中没有任何内容可以运行,io_services 会将异步函数转换为同步函数,因为它还没有其他事情可做。但我在异步调用之前添加了 2 个帖子,我相信这会在异步调用之前将某些内容放入运行队列中。但这似乎没有什么区别。

任何帮助将不胜感激。我是 boost::asio 库的新手。

void receiver::handle_receive_from(const boost::system::error_code& error,
      size_t bytes_recvd)
{
    static char logBuf[128];    
    string dStr;
    int rVal;
    unsigned int seqNo;

    sprintf_s(logBuf, sizeof(logBuf), "BytesRead:%d", bytes_recvd);
    MyLog(logBuf);
    MyLog("1"); 
    MyLog("2");
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    socket_.async_receive_from(
                boost::asio::buffer(data_, max_length), sender_endpoint_,
                boost::bind(&receiver::handle_receive_fromTwo, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));     

    MyLog("3");

}

void receiver::handle_receive_fromTwo(const boost::system::error_code& error, size_t bytes_recvd)
{
    char logBuf[128];

    sprintf_s(logBuf, sizeof(logBuf), "Two BytesRead:%d", bytes_recvd);
    MyLog(logBuf);

    MyLog("1-Two"); 
    MyLog("2-Two");
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    socket_.async_receive_from(
                boost::asio::buffer(data_, max_length), sender_endpoint_,
                boost::bind(&receiver::handle_receive_from, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));         


    MyLog("3-Two");

}

void receiver::postTestHandler()
{   
    int count(0);
    MyLog("***postTestHandler entry***");
    printf("***postTestHandler entry***\n");
    printf("Exiting postTestHandler\n");
    MyLog("Exiting postTestHandler");
}

日志片段

[11:57:51.653647]BytesRead:16
[11:57:51.653660]1
[11:57:51.653662]2
[11:57:51.653692]3
[11:57:51.653697]***postTestHandler entry***
[11:57:51.654310]Exiting postTestHandler
[11:57:51.654315]***postTestHandler entry***
[11:57:51.654657]Exiting postTestHandler
[11:57:51.727494]Two BytesRead:67
[11:57:51.727503]1-Two
[11:57:51.727506]2-Two
[11:57:51.727524]3-Two
[11:57:51.727529]***postTestHandler entry***
[11:57:51.728060]Exiting postTestHandler
[11:57:51.728065]***postTestHandler entry***
[11:57:51.728407]Exiting postTestHandler
[11:57:52.438916]BytesRead:67
[11:57:52.438929]1
[11:57:52.438932]2
[11:57:52.438961]3
[11:57:52.438965]***postTestHandler entry***
[11:57:52.439568]Exiting postTestHandler
[11:57:52.439573]***postTestHandler entry***
[11:57:52.439914]Exiting postTestHandler
[11:57:52.581333]Two BytesRead:67
[11:57:52.581346]1-Two
[11:57:52.581349]2-Two
[11:57:52.581375]3-Two
[11:57:52.581381]***postTestHandler entry***
[11:57:52.582011]Exiting postTestHandler
[11:57:52.582016]***postTestHandler entry***
[11:57:52.582358]Exiting postTestHandler
[11:57:52.582364]BytesRead:67
[11:57:52.582367]1
[11:57:52.582370]2
[11:57:52.582377]3
[11:57:52.582381]***postTestHandler entry***
[11:57:52.582717]Exiting postTestHandler
[11:57:52.582722]***postTestHandler entry***
[11:57:52.583055]Exiting postTestHandler
[11:57:52.583061]Two BytesRead:67
[11:57:52.583064]1-Two
[11:57:52.583066]2-Two
[11:57:52.583077]3-Two
[11:57:52.583081]***postTestHandler entry***
[11:57:52.583418]Exiting postTestHandler
[11:57:52.583423]***postTestHandler entry***
[11:57:52.583755]Exiting postTestHandler
[11:57:52.616525]BytesRead:67
[11:57:52.616531]1
[11:57:52.616533]2
[11:57:52.616549]3
[11:57:52.616553]***postTestHandler entry***
[11:57:52.617015]Exiting postTestHandler
[11:57:52.617020]***postTestHandler entry***
[11:57:52.617362]Exiting postTestHandler

In the code below, the asynch_receive_from behaves synchronously. It doesn't come back until data is received. A look at the log at the end indicates this. This may not be that
obvious at first because there's a fairly high rate of data coming in. But when there's not data, only one second heartbeat messages, the return out of these asynch calls is on the order of one second as well.

The question is why aren't these asynch calls returning immediately (within a few micros), and with no bytes read?

I thought that maybe having other items in the run queue would facilitate the process, thinking that if there's nothing on the queue to run that io_services would turn the asynch function into a synch function since there's nothing else for it to do yet. But I added the 2 posts ahead of the asynch call which, I would believe, put something into the run queue ahead of the asynch call. But that didn't seem to make a difference.

Any help would be appreciated. I am new to the boost::asio library.

void receiver::handle_receive_from(const boost::system::error_code& error,
      size_t bytes_recvd)
{
    static char logBuf[128];    
    string dStr;
    int rVal;
    unsigned int seqNo;

    sprintf_s(logBuf, sizeof(logBuf), "BytesRead:%d", bytes_recvd);
    MyLog(logBuf);
    MyLog("1"); 
    MyLog("2");
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    socket_.async_receive_from(
                boost::asio::buffer(data_, max_length), sender_endpoint_,
                boost::bind(&receiver::handle_receive_fromTwo, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));     

    MyLog("3");

}

void receiver::handle_receive_fromTwo(const boost::system::error_code& error, size_t bytes_recvd)
{
    char logBuf[128];

    sprintf_s(logBuf, sizeof(logBuf), "Two BytesRead:%d", bytes_recvd);
    MyLog(logBuf);

    MyLog("1-Two"); 
    MyLog("2-Two");
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    iosP->post(boost::bind(&receiver::postTestHandler, this));
    socket_.async_receive_from(
                boost::asio::buffer(data_, max_length), sender_endpoint_,
                boost::bind(&receiver::handle_receive_from, this,
                boost::asio::placeholders::error,
                boost::asio::placeholders::bytes_transferred));         


    MyLog("3-Two");

}

void receiver::postTestHandler()
{   
    int count(0);
    MyLog("***postTestHandler entry***");
    printf("***postTestHandler entry***\n");
    printf("Exiting postTestHandler\n");
    MyLog("Exiting postTestHandler");
}

Log snippet

[11:57:51.653647]BytesRead:16
[11:57:51.653660]1
[11:57:51.653662]2
[11:57:51.653692]3
[11:57:51.653697]***postTestHandler entry***
[11:57:51.654310]Exiting postTestHandler
[11:57:51.654315]***postTestHandler entry***
[11:57:51.654657]Exiting postTestHandler
[11:57:51.727494]Two BytesRead:67
[11:57:51.727503]1-Two
[11:57:51.727506]2-Two
[11:57:51.727524]3-Two
[11:57:51.727529]***postTestHandler entry***
[11:57:51.728060]Exiting postTestHandler
[11:57:51.728065]***postTestHandler entry***
[11:57:51.728407]Exiting postTestHandler
[11:57:52.438916]BytesRead:67
[11:57:52.438929]1
[11:57:52.438932]2
[11:57:52.438961]3
[11:57:52.438965]***postTestHandler entry***
[11:57:52.439568]Exiting postTestHandler
[11:57:52.439573]***postTestHandler entry***
[11:57:52.439914]Exiting postTestHandler
[11:57:52.581333]Two BytesRead:67
[11:57:52.581346]1-Two
[11:57:52.581349]2-Two
[11:57:52.581375]3-Two
[11:57:52.581381]***postTestHandler entry***
[11:57:52.582011]Exiting postTestHandler
[11:57:52.582016]***postTestHandler entry***
[11:57:52.582358]Exiting postTestHandler
[11:57:52.582364]BytesRead:67
[11:57:52.582367]1
[11:57:52.582370]2
[11:57:52.582377]3
[11:57:52.582381]***postTestHandler entry***
[11:57:52.582717]Exiting postTestHandler
[11:57:52.582722]***postTestHandler entry***
[11:57:52.583055]Exiting postTestHandler
[11:57:52.583061]Two BytesRead:67
[11:57:52.583064]1-Two
[11:57:52.583066]2-Two
[11:57:52.583077]3-Two
[11:57:52.583081]***postTestHandler entry***
[11:57:52.583418]Exiting postTestHandler
[11:57:52.583423]***postTestHandler entry***
[11:57:52.583755]Exiting postTestHandler
[11:57:52.616525]BytesRead:67
[11:57:52.616531]1
[11:57:52.616533]2
[11:57:52.616549]3
[11:57:52.616553]***postTestHandler entry***
[11:57:52.617015]Exiting postTestHandler
[11:57:52.617020]***postTestHandler entry***
[11:57:52.617362]Exiting postTestHandler

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懒猫 2024-11-24 12:51:25

不确定您是否仍然遇到此问题,但在您调用 io_service.run() 后它会阻塞,因为它将无限期地寻找已收到的数据包。如果您希望它检查回调,但如果没有回调则不阻止,则使用 io_service.poll()。如果您希望它只执行一个回调(如果有一个或多个请求),请使用 io_service.poll_one()。

不确定这是否是您的问题,或者您是否仍然遇到这个问题(我知道这个问题很老),但祝您好运!

编辑(回应您的评论):

如果您使用 io_service.run(),那么该线程将永远致力于处理 asio 回调。但是,如果您使用 io_service.poll(),那么您可以在需要时处理回调,然后同时执行其他操作。 io_service.poll() 绝不比 io_service.run() 快,它只是让您能够随时处理回调,而不是总是无限期地处理。

您可能会想象 io_service.run() 像这样实现:

void IOService::run()
{
    while (true)
    {
        poll();
    }
}

请注意,它不是这样实现的。但这可能会澄清运行和民意调查正在做什么的想法。

Not sure if you're still having this issue, but it will block after you call io_service.run() because it will indefinitely be looking for packets that it has received. If you'd like it to check for callbacks, but not block if there aren't any, then use io_service.poll(). If you'd like it to execute exactly one callback (if there are one or more requests) then use io_service.poll_one().

Not sure if this is your problem, or if you still have this issue (I know this question is very old), but good luck!

edit (in response to your comment):

If you use io_service.run(), then that thread will be forever dedicated to handling asio callbacks. However, if you use io_service.poll(), then you can handle callbacks when you want to and then do something else in the meantime. io_service.poll() is in no way faster than io_service.run(), it just gives you the ability to handle callbacks whenever you want to, rather than always indefinitely.

You could probably picture io_service.run() to be implemented like this:

void IOService::run()
{
    while (true)
    {
        poll();
    }
}

Note that it is not implemented like this. But this might clear up the idea of what run and poll are doing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文