[英]Having a hard time understanding a few concepts with Boost ASIO TCP with async_read and async_write
I'm having a hard time understand the correct way I should structure a tcp client when using async_read and async_write. 我很难理解使用async_read和async_write时应该构造tcp客户端的正确方法。 The examples seem to do a async_read after connecting and then have async_write in the handler.
这些示例似乎在连接后执行async_read,然后在处理程序中具有async_write。
In the case of my client and sever, when the client connects it needs to check a queue of messages to write and check to see if anything needs to be read. 对于我的客户端和服务器,当客户端连接时,它需要检查要写入的消息队列,并检查是否需要读取任何内容。 One of the things I'm having a hard time with is understanding how this would work asynchronously.
我遇到的麻烦之一是了解它如何异步工作。
What I envision is in the async_connect handler, the thread would call async_write if anything is in the sendQueue and call async_read over and over. 我设想的是在async_connect处理程序中,如果sendQueue中有任何内容,线程将调用async_write,并反复调用async_read。 Or should it check if anything is available to be read before it does an async_read?
还是应该在进行async_read之前检查是否有任何可读取的内容? Below is an example of what I'm talking about.
以下是我正在谈论的示例。
void BoostTCPConnection::connectHandler()
{
setRunning(true);
while (isRunning())
{
//If send Queue has messages
if ( sendSize > 0)
{
//Calls to async_write
send();
}
boost::shared_ptr<std::vector<char> > sizeBuffer(new std::vector<char>(4));
boost::asio::async_read(socket_, boost::asio::buffer(data, size), boost::bind(&BoostTCPConnection::handleReceive, shared_from_this(), boost::asio::placeholders::error, sizeBuffer));
}
}
void BoostTCPConnection::handleReceive(const boost::system::error_code& error, boost::shared_ptr<std::vector<char> > sizeBuffer)
{
if (error)
{
//Handle Error
return;
}
size_t messageSize(0);
memcpy((void*)(&messageSize),(void*)sizeBuffer.data(),4);
boost::shared_ptr<std::vector<char> > message(new std::vector<char>(messageSize) );
//Will this create a race condition with other reads?
//Should a regular read happen here
boost::asio::async_read(socket_, boost::asio::buffer(data, size),
boost::bind(&BoostTCPConnection::handleReceiveMessage, shared_from_this(),
boost::asio::placeholders::error, message));
}
void BoostTCPConnection::handleReceiveMessage(const boost::system::error_code& error, boost::shared_ptr<std::vector<char> > rcvBuffer)
{
if (error)
{
//Handle Error
return;
}
boost::shared_ptr<std::string> message(new std::string(rcvBuffer.begin(),rcvBuffer.end()));
receivedMsgs_.push_back(message);
}
void BoostTCPConnection::handleWrite(const boost::system::error_code& error,size_t bytes_transferred)
{
//Success
if (error.value() == 0)
return;
//else handleError
}
Conceptually, async_read
waits for data to be received. 从概念上讲,
async_read
等待接收数据。 You should call it any time you want something to happen after data is received and a read isn't already pending. 在收到数据并且尚未进行读取之后,只要您希望发生任何事情,就应该调用它。 Similarly,
async_write
waits for data to be written. 同样,
async_write
等待写入数据。 You should call it any time you need to write data and a write isn't already pending. 您应该在需要写入数据的任何时间调用它,而写入尚未挂起。
You should call async_read
when you complete the connection. 完成连接后,应调用
async_read
。 Before your async_read
handler returns, it should probably call async_read
again. 在您的
async_read
处理函数返回之前,它可能应该再次调用async_read
。
When you need to write to the connection, you should call async_write
(if a write isn't already pending). 当需要写入连接时,应调用
async_write
(如果尚未完成写入操作)。 In your async_write
handler, if you still need to write more, you should call async_write
again. 在
async_write
处理程序中,如果仍然需要编写更多内容,则应再次调用async_write
。
If no read is already pending, you can call async_read
in your write handler, if you wish to resume reading after you finish writing. 如果尚未完成任何读取,则希望在完成写入后继续读取,则可以在写入处理程序中调用
async_read
。 You can also just keep a read always pending. 您也可以始终保持读取状态。 That's up to you.
随你(由你决定。
You should not check if there's anything to read before calling async_read
. 在调用
async_read
之前,您不应该检查是否有任何要读取的内容。 The point of async_read
is for it to complete when there's something to read. async_read
是在需要读取内容时完成它。 It's a smart way of waiting and doing other things in the meantime. 这是同时等待和执行其他操作的明智方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.