Hello. I'm writing a little game which keeps four players connected.
At first, my protocol does something like the following:
-Server waits for 3 incoming connections. io_service::run is running
in another thread (and the code I'll show you server side is executed
after that).
-Clients connect and send its data (name and team).
When everything's done, the server continues and shows the received data
from the clients.
I have a loop like this on the Server side (with the great coroutine
implementation from the examples):
for (; num_players_connected_ !=3; ++num_players_connected_) {
//Create context
connection_contexts_.at(num_players_connected_) =
std::make_shared<ConnectionContext>(service_);
yield acceptor_.async_accept(*connection_contexts_.at(num_players_connected_)->socket(),
std::ref(*this));
std::cout << "Servidor: Incoming connection" << std::endl;
connection_contexts_.at(num_players_connected_)->start(); //This
calls ClientContext::operator()
//Start connection processing
std::cout << "Servidor: Players connected: " <<
num_players_connected_ << std::endl;
}
//Here every connection is already done but must wait for data
available in every context.
std::cout << "Server: All " << num_players_connected_ << " players are
connected" << std::endl;
for (std::size_t i = 0; i < 3; i++) {
//Wait for each player to have its data available server-side
connection_contexts_.at(i)->playerDataReady().get();
std::cout << "Player info:\n";
std::cout << "\tPlayer name " <<
connection_contexts_.at(i)->playerName() << std::endl;
std::cout << "\tPlayer team " <<
connection_contexts_.at(i)->playerTeam() << std::endl;
}
///////////////////////////////////
The Context for each client in the server side looks something like this:
void ClientContext::operator()() {
using namespace boost;
const MsjDatosJugador * player_data = 0;
MensajeRed msg;
reenter (coro_opcall_state_) {
yield asyncReadMessageFromSocketIntoQueue(std::ref(*this));
msg = getNextMessage();
std::cout << "Message has been read\n" << std::endl;
if (msg.getTipo() == typeid(MsjDatosJugador)) {
player_data = &(msg.getInfo<MsjDatosJugador>());
player_name_ = std::move(player_data->name_);
player_team_ = player_data->team_;
player_ready_ = true;
}
else {
message_received_handler_(getNextMessage());
}
}
}
void ClientContext::asyncReadMessageFromSocketIntoQueue(
std::function