是否可以在使用 Boost::asio 之前从套接字读取后执行async_handshake
Is it possible to do async_handshake after reading from socket prior using Boost::asio?
我有一个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 个字节?
在调查了async_handshake缓冲区重载方法的实现之后,缓冲区似乎必须已经读入了握手。
我尝试过,但仍然遇到问题,我不断收到一个错误代码,说SSL版本不正确。我知道这不是问题,因为不使用async_handshake
的缓冲重载工作正常!
然后,解决方案是限制缓冲区参数的大小。
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);
}
请注意,其中的read
和read_some
调用也应async
进行。我只是想在没有处理程序的情况下演示答案。
相关文章:
- 套接字读取后,我在缓冲区中看到意外输入
- 将"uint8_t"(从套接字读取)隐式转换为"char"安全吗
- TCP 服务器的异步读取使用 boost::asio 打印客户端套接字发送的数据
- 从Qt上的UNIX域套接字(QLocalSocket)读取
- 不要从输入队列套接字读取数据
- 如何读取套接字编程(c ++)中的所有数据?
- 使用 Broadcast 发出的从节点服务器发送的数据不能被 C++ 套接字 IO 客户端读取
- boost::asio-async_read_some示例代码没有读取套接字中的所有数据
- 使用RAW套接字读取ICMP响应
- 为什么我在蓝牙连接()上收到"java.io.IOException:读取失败,套接字可能关闭或超时,读取re
- UDP 套接字读取最后一个传入字节
- CAN套接字读取帧较晚
- Java 套接字读取在第二次读取时返回奇怪字符
- 如何使用 recv 或 C++ 中的读取功能从 tcp 套接字读取大请求
- 如何在读取时获取套接字流的位置
- 无法在java(客户端)的套接字上正确写入数据(类),也无法在c ++(服务器)上读取数据(类)
- C/C++:写入和读取套接字
- 如何正确读取套接字消息(C++)
- 如何读取套接字消息
- 连续两次读取套接字,第二次读取不起作用