私の問題は次のとおりです。非同期TCPサーバーとそれぞれの非同期クライアントがあります。私が必要とするのは、クライアントからコマンドを受け取ることができると同時に、リアルタイム変数をクライアントに(連続的に)書き込むことができる方法です。async_readを実行している間、boost :: asio :: io_service async_writeループ中に
クライアントがこの操作をトリガするコマンドを送信すると、テストメッセージが送信されますが、クライアントが待っている間にサーバーがハングアップするため、1つのメッセージを送信する方法しか見つけません。コマンド。
この関数は、クライアントから送信されたコマンドを処理し、受信した後、関数h_readに渡すものです:
ここvoid conn::h_write() {
memset(data_, '\0', sizeof(char)*max_length);
async_read_until(sock_ , input_buffer_, '\n',
boost::bind(&conn::h_read, shared_from_this()));
}
コマンドはの書き込み連続トリガするものであれば、私はチェックリアルタイムバッファをクライアントに送信します。この場合、コマンドは "c"です。
void conn::h_read(){
std::string line;
std::istream is(&input_buffer_);
std::getline(is, line);
std::string output = "";
output.reserve(5000);
if (line == "exit"){
return;
}
if (line.empty()){
memset(data_, '\0', sizeof(char)*max_length);
async_read_until(sock_ , input_buffer_, '\n', boost::bind(&conn::h_read, shared_from_this()));
return;
}
clientMessage_ = line;
clientMessage_ += '\n';
if (clientMessage_.substr(0,1) == "c"){
std::stringstream toSend;
streamON = true;
toSend.str("c l 1 ");
toSend << std::fixed << std::setprecision(2) << luxID1[0];
toSend.str(" ");
// Here I sent the real time value for the first time
boost::asio::async_write(sock_, boost::asio::buffer(toSend.str()), boost::bind(&conn::sendRealTime, shared_from_this()));
}
else{ // Does't really matter to this example
// Do some stuff here and send to client
boost::asio::async_write(sock_, boost::asio::buffer(I2CrxBuf_), boost::bind(&conn::h_write, shared_from_this()));
}
}
は今、これは変数の連続送信を処理する機能であるが、同時にコマンドクライアントを読むことができる:
void conn::sendRealTime(){
if (streamON){
boost::asio::async_write(sock_, boost::asio::buffer("This is a test\n"), boost::bind(&conn::h_write, shared_from_this()));
memset(data_, '\0', sizeof(char)*max_length);
async_read_until(sock_ , input_buffer_, '\n', boost::bind(&conn::h_read, shared_from_this()));
}
else{
memset(data_, '\0', sizeof(char)*max_length);
async_read_until(sock_ , input_buffer_, '\n', boost::bind(&conn::h_read, shared_from_this()));
}
}
問題は、最初の後をブロック"async_read_util"関数を呼び出します。
私が望むものが可能であれば分かりませんが、それがあれば誰かがそれをどうやって手伝ってくれますか?私は必要なもの