boost :: asio :: streambuf :: consume-注入垃圾字符 [英] boost::asio::streambuf::consume - Injects garbage character

查看:135
本文介绍了boost :: asio :: streambuf :: consume-注入垃圾字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我失去连接时,在我的服务器代码中,我尝试永远以循环的方式重新连接.重新连接后,我会将登录消息发送到所连接的组件.然后,该组件会发回类似"MyResponse"的登录响应

When I lose connection, in my server code, I try to reconnect in a loop forever. Once I reconnect, I send a login message to the component I am connected to. That component then sends back a login response that looks like "MyResponse"

初始连接正常.但是,重新连接后,在出现预期的消息之前会出现垃圾,例如:ýMyResponse"

The initial connection works fine. After I reconnect, however, I get garbage before the expected message, that looks like: "ýMyResponse"

在谷歌搜索之后.我在Stack Overflow上看到许多关于boost :: asio :: streambuf的问题,该问题用于boost :: asio中的异步套接字.特别是关于重用他的缓冲区.我遵循了那里的建议,并在断开连接时调用了消耗.换句话说,在调用shutdown并在套接字上关闭之后,在调用recv_until以响应recv时返回错误之后,我调用boost :: asio :: streambuf :: consume.

After googling this. I see many questions on Stack Overflow about the boost::asio::streambuf that is used for async sockets in boost::asio. Particularly about reusing he buffer. I have followed the advice there and called consume upon disconnecting. In other words, I call boost::asio::streambuf::consume after I call shutdown and close on my socket, after being called back with an error on recv in response to a call to recv_until.

我还使用wireshark来确保不会发送垃圾字符,也不会发送垃圾字符.

I have also used wireshark to make sure that the garbage character is not being sent and it is not.

经过大量调试后,好像好像要消费的调用是注入一个字符而不是清除所有字符.

After much debugging, it appears as though the calls to consume are injecting a character rather than clearing out all characters.

这是一个最小的示例:

#include <boost/asio.hpp>

#include <iostream>
#include <sstream>
#include <string>

int main()
{
    boost::asio::streambuf buffer;

    std::cout << "buffer size " << buffer.size() << std::endl;

    buffer.consume(std::numeric_limits<size_t>::max());

    std::cout << "buffer size " << buffer.size() << std::endl;

    std::istream is(&buffer);
    std::string contents;
    is >> contents;

    std::cout << "Contents: "  << contents << std::endl;

    std::cout << "buffer size " << buffer.size() << std::endl;

    return 0;
}

输出:

buffer size 0
buffer size 1
Contents: ²
buffer size 0

预期输出:

buffer size 0
buffer size 0
Contents:
buffer size 0

如果不使用消耗,则会在服务器代码中收到一些消息,该消息之前是断开连接的,重新连接之后是第一条消息之前的.

If I do not use consume, I get some of the message, previous to disconnect, before the first message after reconnection, in my server code.

如果我确实使用了消耗,则会得到一个垃圾字符.

If I do use consume, I get a garbage character.

请参阅:

使用boost :: asio :: streambuf

读取直到boost :: asio :: streambuf中的字符串定界符

boost asio async_read:已读消息添加到本身

推荐答案

boost :: asio :: streambuf ::消耗溢出.

boost::asio::streambuf::consume overflows.

使用

buffer.consume(buffer.consume(buffer.size());

而不是

buffer.consume(std::numeric_limits<size_t>::max());

报告错误以增强邮件列表.

Reporting bug to boost mailing list.

这篇关于boost :: asio :: streambuf :: consume-注入垃圾字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆