3

データの送受信を同時に行えるTCPクライアントを作成しています。別々のスレッドasync_sendで、どのように呼び出すべきか教えていただけますか?async_receive

言い換えれば、どのように呼び出すか

m_Socket.async_send(boost::asio::buffer(txBuf.c_str(), txBuf.length()+1),
    boost::bind(&TCPClient::sendingHandler, this, boost::asio::placeholders::error));

m_Socket.async_receive(boost::asio::buffer(rxBuf, maxBufLen),
    boost::bind(&TCPClient::sendingHandler, this, boost::asio::placeholders::error));

boost::thread receivingThread(boost::bind(...));
boost::thread sendingThread(boost::bind(...));

また、ハンドラー内で呼び出したり、再度呼び出しasync_send たりすると、正しく機能しますか?async_receiveデータを送受信するための不定詞ループが必要です。

4

1 に答える 1

2

TCPClient::sendingHandler/receivingHandler主なアイデアは、2の内部で再帰的に送受信することio_service'sです。これio_service'sは2つのスレッド内で呼び出されます-

boost::thread receivingThread(boost::bind(...));
boost::thread sendingThread(boost::bind(...));

このアイデアは、このチュートリアルではっきりとわかります。2つの別々のを呼び出して使用する必要がある唯一の違いio_service's

もう1つのオプションは、1io_serviceを呼び出してスレッドを乗算することio_service::runです。ただし、boost::asio::strandファースレッドセーフを使用する必要があります。

boost::asio::strand* _strand = new boost::asio::strand(io);
//then use it in handlers
boost::asio::async_read(*socket, 
                    boost::asio::buffer(msg.data(), result.size),
                    (*_strand).wrap(
                    boost::bind(&ConnectionInterface::parsePacket, shared_from_this(), boost::asio::placeholders::error)));
于 2012-10-25T03:57:26.690 に答える