我正在構建客戶端服務器通信。服務器發送Header + Data(使用async_write和一個單獨的IO線程),客戶端接收固定大小的Header並知道需要讀取多少數據。Boost Asio - 郵件內容傳輸錯誤
問題:有時客戶端收到錯誤的數據。這似乎是服務器發送錯誤的數據。
void Session::do_write(std::shared_ptr<DataItem> data)
{
std::lock_guard<std::mutex> lk(doWrite_mutex);
std::vector<boost::asio::const_buffer> buffers;
buffers.push_back(boost::asio::buffer(&data->length, sizeof(uint32_t)));
buffers.push_back(boost::asio::buffer(&data->callbackID, sizeof(uint8_t)));
buffers.push_back(boost::asio::buffer(&data->isString, sizeof(bool)));
//Get the data to send into the buffer and make sure the given shared ptr to the data item keeps living until this function is finished.
buffers.push_back(boost::asio::buffer(data->getData(), data->length));
boost::asio::async_write(*socket_, buffers, boost::bind(&Session::onSend, this, data, _1,_2));
}
void Session::onSend(std::shared_ptr<DataItem> data,const boost::system::error_code ec, std::size_t length)
{ //Some logging, nothing special here
}
該數據項是一個多態類,用於處理不同種類的數據(向量,字符串...)。 getData()
方法將const void*
返回到實際數據(例如在矢量的情況下爲myData->data()
)。數據在DataItem中保存爲shared_ptr
(以防止其被銷燬)。
在大多數情況下,數據傳輸是正確的。
我不知道在哪裏調試或我做錯了什麼。
」好像服務器發送了錯誤的數據。「你應該驗證一下。 – PlasmaHH
我在'onSend'中驗證了'data'中的數據仍然正確,正如預期的那樣。 – Till
我更像是在電線上思考。 – PlasmaHH