【发布时间】:2019-04-19 18:04:27
【问题描述】:
我有一个boost::asio::ssl::stream<boost::asio::ip::tcp::socket> 类型的套接字。当 boost 第一次接受到这个套接字的连接时,我想看看一些字节。但是,偷看不是您可以正确/安全地进行的事情。所以我读取了我需要的字节并将它们放在缓冲区中。
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 个字节!
我可以做些什么来为async_handshake 提供适当的缓冲区,通知它其中已经有有效的 4 个字节?
【问题讨论】: