在使用Boost :: asio读取套接字之前,可以做async_handshake吗? [英] Is it possible to do async_handshake after reading from socket prior using Boost::asio?

查看:326
本文介绍了在使用Boost :: asio读取套接字之前,可以做async_handshake吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个boost::asio::ssl::stream<boost::asio::ip::tcp::socket>类型的套接字.当boost首先接受与此套接字的连接时,我想窥视一些字节.但是,偷看并不是您可以正确/安全地执行的操作.因此,我读取了我需要的字节并将其放在缓冲区中.

I have a boost::asio::ssl::stream<boost::asio::ip::tcp::socket> typed socket. When boost first accepts a connection to this socket, I want to peek at some bytes. However, peeking is not something you can do properly/safely. So I read the bytes I need and have them in a buffer.

typedef socket_type boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
void OnAccept(std::shared_ptr<socket_type> socket)
{
    boost::asio::mutable_buffers_1 sslBuffer(m_Buffer.data(), m_Buffer.size());
    // I'm going to read 4 bytes from the socket.
    boost::system::error_code ec;
    std::size_t readBytes = boost::asio::read(socket->next_layer(), boost::asio::buffer(sslBuffer, 4), ec);
    if(ec) { Stop(); return; } // pseudo

    // Check the bytes I read in the buffer

    socket->async_handshake(boost::asio::ssl::stream_base::server, sslBuffer, &handler);
}

这时,将调用async_handshake的处理程序,但它会告诉我它从ssl得到了unexpected message错误代码.这是有道理的:正在与之进行握手的消息可能丢失了前4个字节!

At this point, async_handshake's handler will be called but it will tell me it got an unexpected message error code from ssl. This makes sense: the message it's doing the handshake with is missing the first 4 bytes likely!

我该怎么做才能为async_handshake提供适当的缓冲区,通知它已经存在有效的4个字节?

What can I do to provide async_handshake with the proper buffer that informs it that there is already valid 4 bytes in it?

推荐答案

在研究了async_handshake的缓冲区重载方法的实现之后,看来缓冲区必须已经读取了握手.

After investigating the implementation of async_handshake's buffer overload method, it appears that the buffer must already have the handshake read in.

我试图这样做,但仍然遇到问题,但不断收到错误代码,指出SSL版本不正确.我知道这不是问题所在,因为不使用async_handshake的缓冲重载就可以了!

I attempted that but still encountered issues, I kept getting an error code saying that the SSL version was incorrect. I knew this was not the issue because not using the buffered overload of async_handshake worked fine!

然后解决方案是还限制缓冲区参数的大小.

The solution was then to also limit the buffer parameter's size.

typedef socket_type boost::asio::ssl::stream<boost::asio::ip::tcp::socket>;
void OnAccept(std::shared_ptr<socket_type> socket)
{
    const uint bytesToPeek = 4;
    boost::asio::mutable_buffers_1 sslBuffer(m_Buffer.data(), m_Buffer.size());
    // I'm going to read 4 bytes from the socket.
    boost::system::error_code ec;
    std::size_t readBytes = boost::asio::read(socket->next_layer(), boost::asio::buffer(sslBuffer, bytesToPeek), ec);
    if(ec) { Stop(); return; } // pseudo

    // Check the bytes I read in the buffer

    // Read in the rest of the handshake.
    std::size_t bytesOfHandshake = socket->next_layer().read_some(boost::asio::buffer(sslBuffer+bytesToPeek, 4000));
    bytesOfHandshake += bytesToPeek;

    // Finish the handshake.
    socket->async_handshake(boost::asio::ssl::stream_base::server, boost::asio::buffer(sslBuffer, bytesOfHandshake), &handler);
}

请注意,在此的readread_some调用也都应设为async.我只是想在没有处理程序的情况下演示答案.

Note that both the read and read_some calls in this should also be made async. I just wanted to demonstrate the answer without handlers.

这篇关于在使用Boost :: asio读取套接字之前,可以做async_handshake吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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