Como iniciar um “evento” quando meu servidor Boost :: asio tcp apenas começar a executar (AKA io_service.run ())?
Com base em um relacionamento cliente / servidor boost :: asio, preciso iniciar o programa cliente a partir do programa do servidor somente quando o encadeamento do servidor estiver em um "esperando para ser conectado" Estado.
Minha pergunta é como ter o conhecimento desse estado?
Como amostra, use oexemplo asio / serialização ligação, e substitua a função principal de server.cpp com esse código:
#include <conio.h>
#include <concrt.h> // wait function
#include <future>
#include <thread>
void server_thread( std::promise<bool>& run )
{
boost::asio::io_service io_service;
s11n_example::server server(io_service, 123);
// too early to run.set_value( true );
io_service.run();
// too late to run.set_value( true );
}
int main(int argc, char* argv[])
{
std::promise<bool> run;
std::thread thrd( server_thread, boost::ref( run ) );
thrd.detach();
bool launched = run.get_future().get();
// server is waiting for connection
// launch the client
if( launched )
{
int rc = system( "start client.exe localhost 123" );
if( rc )
std::cerr << "system failed returning " << rc << std::endl ;
}
else
std::cerr << "server_thread failure" << std::endl ;
std::cout << "hit a key to exit" ;
while( !_kbhit() )
Concurrency::wait( 100 );
return 0;
}
Obrigado,