C++ Boost异步读取不';Don’别给我一个;“帧结束”;旗帜
我还在开发某种客户端,用于与IP摄像机进行通信。现在我有以下问题: 我向摄像机发送一个请求(特别是C++ Boost异步读取不';Don’别给我一个;“帧结束”;旗帜,c++,sockets,boost,asynchronous,boost-asio,C++,Sockets,Boost,Asynchronous,Boost Asio,我还在开发某种客户端,用于与IP摄像机进行通信。现在我有以下问题: 我向摄像机发送一个请求(特别是RTSP description)。现在我得到的答案是这样的: RTSP/1.0 200 OK CSeq: 2 Date: Thu, Jan 01 1970 00:31:41 GMT Content-Base: rtsp://192.168.0.42/mpeg4?mode=Live&stream=-1&buffer=0&seek=0&fps=100& m
RTSP description
)。现在我得到的答案是这样的:
RTSP/1.0 200 OK
CSeq: 2
Date: Thu, Jan 01 1970 00:31:41 GMT
Content-Base: rtsp://192.168.0.42/mpeg4?mode=Live&stream=-1&buffer=0&seek=0&fps=100& metainfo=/
Content-Type: application/sdp
Content-Length: 517
CommandReadBuffer::CallbackFromAsyncWrite()
{
boost::asio::async_read_until(*m_Socket, m_ReceiveBuffer,"\r\n\r\n",
boost::bind(&CommandReadBuffer::handle_rtsp_describe, this->shared_from_this(),
boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
}
这是答案的标题,后面是所谓的会话描述
,其大小如字段内容长度
所示。实际上,我不太喜欢会话描述
,我只对内容库
字段感兴趣。但是,由于在同一个套接字上有一些通信,所以我需要清除所有数据
对于接收,我使用来自boost::asio
的async\u read
调用。
我的代码看起来(简化)如下:
RTSP/1.0 200 OK
CSeq: 2
Date: Thu, Jan 01 1970 00:31:41 GMT
Content-Base: rtsp://192.168.0.42/mpeg4?mode=Live&stream=-1&buffer=0&seek=0&fps=100& metainfo=/
Content-Type: application/sdp
Content-Length: 517
CommandReadBuffer::CallbackFromAsyncWrite()
{
boost::asio::async_read_until(*m_Socket, m_ReceiveBuffer,"\r\n\r\n",
boost::bind(&CommandReadBuffer::handle_rtsp_describe, this->shared_from_this(),
boost::asio::placeholders::error,boost::asio::placeholders::bytes_transferred));
}
这一个至少读取头(如上所示),因为它被一个空行终止。与通常的async\u write
一样,它只读取更多的数据,而不读取。现在转到下一个回调函数:
void CommandReadBuffer::handle_rtsp_describe(const boost::system::error_code& err,size_t bytesTransferred)
{
std::istream response_stream(&m_ReceiveBuffer);
std::string header;
// Just dump the data on the console
while (std::getline(response_stream, header))
{
// Normally I would search here for the desired content-base field
std::cout << header << "\n";
}
boost::asio::async_read(*m_Socket, m_ReceiveBuffer, boost::asio::transfer_at_least(1),
boost::bind(&CommandReadBuffer::handle_rtsp_setup, this->shared_from_this(),
boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
这意味着该功能将持续到
因此,如果没有更多的数据可读取,它将继续。
但是我也尝试了没有CompletionCondition
参数的重载函数,当发生错误时(EOF
!!!)应该返回该参数-但是这也不会回调
有什么建议吗?我只是不明白我做错了什么…我已经使用boost asio编写了一个RTSP客户端和服务器库,可以提供以下建议: RTSP消息语法是通用的:不需要不同的描述和设置处理程序。大体上
- 编写一个RTSP请求
- 要读取响应,请执行一个
boost::asio::async\u read\u,直到(“\r\n\r\n”)
- 然后检查
标题内容长度
- 如果内容长度>0,则至少执行一次
boost::asio::transfer\u(内容长度)
如果在应用程序中已完成与RTSP服务器的交互,请在读取响应后关闭连接。EOF的优点是。。。我可能脑子里有点不对劲!其实我是朝着你说的方向走的。但在这一点上有一个问题:当我收到对设置或播放请求的回复时,我从来没有在标题中得到“content length”字段……它从来没有出现过,还是只是在这种特殊情况下?设置和播放请求没有消息体。根据RFC2326,只有descripe和GET_参数请求在响应中返回消息体:“……所有返回消息体的方法。在本规范中,descripe和GET_参数属于此类。”好吧,我想我现在都明白了!谢谢你的帮助!!
The supplied buffer is full (that is, it has reached maximum size).
The completion_condition function object returns 0.