Poco Websocket 无法读取大数据
Poco Websocket cant'r read large data
我在进程连接上使用 Net::SocketReactor。当数据输入到套接字时调用如下代码:
int WebSocketWrapper::DoRecieve(void *buf) {
try{
int flags;
const auto size = m_sock.availabel();
const auto ret = m_sock.receiveFrame(buf, size, flags);
if (size != ret){
logger.warrning('Read less than available');
}
return ret;
}
catch (WebSocketException& exc){
logger.log(exc);
switch (exc.code()){
case pnet::WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
logger.debug("unsuported version");
break;
// fallthrough
case pnet::WebSocket::WS_ERR_NO_HANDSHAKE:
case pnet::WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case pnet::WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
logger.debug("Bad request");
break;
}
}
return 0;
}
当数据大小小于 1400 字节时效果很好。 TCP 包未分段。但是当我尝试发送超过 1400 字节的数据时,出现 WebSocketException: "Insufficient buffer for payload size"。我正在探索源代码 Poco::Net::Websocket,他发现了冲突。当调用 Websocket::readFrame 分析帧头的大小时,但我只有部分帧。我可以请求 return StreamSocket::availabel。
如何从websocket读取大数据?
WebSockets 在帧中运行,您将始终收到一个帧或什么也没有。话虽如此,不要费心计算可用数据量(您可能正在使用以太网 1500 字节 MTU),而是提供存储空间以容纳您希望接收的最大帧并调用 receiveFrame(). If the messages are fragmented between multiple frames, you'll have to deal with that at the application level. See documentation:
Receives a frame from the socket and stores it
in buffer. Up to length bytes are received. If
the frame's payload is larger, a WebSocketException
is thrown and the WebSocket connection must be
terminated.
即将发布的 1.7 版本将 receiveFrame() 自动调整缓冲区大小以适应框架。
要了解碎片化消息,请参阅 Receiving Data in RFC 6455. While WebSockets are conceived as messaging protocol, some musings on whether they are really messaging or streaming can be found here。
此外,您发布的代码无法编译,并且在未知大小的缓冲区中写入未知数量的字节的想法似乎很危险,委婉地说。
我在进程连接上使用 Net::SocketReactor。当数据输入到套接字时调用如下代码:
int WebSocketWrapper::DoRecieve(void *buf) {
try{
int flags;
const auto size = m_sock.availabel();
const auto ret = m_sock.receiveFrame(buf, size, flags);
if (size != ret){
logger.warrning('Read less than available');
}
return ret;
}
catch (WebSocketException& exc){
logger.log(exc);
switch (exc.code()){
case pnet::WebSocket::WS_ERR_HANDSHAKE_UNSUPPORTED_VERSION:
logger.debug("unsuported version");
break;
// fallthrough
case pnet::WebSocket::WS_ERR_NO_HANDSHAKE:
case pnet::WebSocket::WS_ERR_HANDSHAKE_NO_VERSION:
case pnet::WebSocket::WS_ERR_HANDSHAKE_NO_KEY:
logger.debug("Bad request");
break;
}
}
return 0;
}
当数据大小小于 1400 字节时效果很好。 TCP 包未分段。但是当我尝试发送超过 1400 字节的数据时,出现 WebSocketException: "Insufficient buffer for payload size"。我正在探索源代码 Poco::Net::Websocket,他发现了冲突。当调用 Websocket::readFrame 分析帧头的大小时,但我只有部分帧。我可以请求 return StreamSocket::availabel。
如何从websocket读取大数据?
WebSockets 在帧中运行,您将始终收到一个帧或什么也没有。话虽如此,不要费心计算可用数据量(您可能正在使用以太网 1500 字节 MTU),而是提供存储空间以容纳您希望接收的最大帧并调用 receiveFrame(). If the messages are fragmented between multiple frames, you'll have to deal with that at the application level. See documentation:
Receives a frame from the socket and stores it in buffer. Up to length bytes are received. If the frame's payload is larger, a WebSocketException is thrown and the WebSocket connection must be terminated.
即将发布的 1.7 版本将 receiveFrame() 自动调整缓冲区大小以适应框架。
要了解碎片化消息,请参阅 Receiving Data in RFC 6455. While WebSockets are conceived as messaging protocol, some musings on whether they are really messaging or streaming can be found here。
此外,您发布的代码无法编译,并且在未知大小的缓冲区中写入未知数量的字节的想法似乎很危险,委婉地说。