From 1c4c2c7abd9bd8ed7a32455749082056079ae85c Mon Sep 17 00:00:00 2001 From: Ole Daniel Evensen Date: Wed, 15 Dec 2010 20:29:08 +0100 Subject: Some tests in client to fall back to dumb behaviour if connection is gone. --- server/client.cpp | 58 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/server/client.cpp b/server/client.cpp index 3573128..afec605 100644 --- a/server/client.cpp +++ b/server/client.cpp @@ -122,41 +122,61 @@ std::string Client::nick() { } void Client::game_start(boost::function callback, std::vector players) { - Message::GameStart::p msg = make_shared(); - - msg->players = players; - - connection->send(msg); - - connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); + if(connection) { + Message::GameStart::p msg = make_shared(); + + msg->players = players; + + connection->send(msg); + + connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); + } else { + callback(); + } } void Client::round_start() { - connection->send(make_shared()); + if(connection) { + connection->send(make_shared()); + } } void Client::round_state(const PlayerState& pl_d, const PlayerState& pl_r, const PlayerState& pl_u, const PlayerState& pl_l, const GameState& g, const Actions& a) { - connection->send(make_shared(pl_d, pl_r, pl_u, pl_l, g, a)); + if(connection) { + connection->send(make_shared(pl_d, pl_r, pl_u, pl_l, g, a)); + } } void Client::round_end(Message::RoundEnd::p msg, boost::function callback) { - connection->send(msg); - - // Check if we're waiting for ready. - if(callback) { - connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); + if(connection) { + connection->send(msg); + + // Check if we're waiting for ready. + if(callback) { + connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); + } + } else { + if(callback){callback();} } } void Client::get_action(boost::function callback, Actions expected_actions) { - connection->recv(boost::bind(&Client::handle_action, shared_from_this(), _1, callback, expected_actions)); + if(connection) { + connection->recv(boost::bind(&Client::handle_action, shared_from_this(), _1, callback, expected_actions)); + } else { + callback(expected_actions.back()); + } } void Client::game_end(Message::GameEnd::p msg, boost::function callback) { - connection->send(msg); - - if(callback) { - connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); + if(connection) { + connection->send(msg); + + if(callback) { + connection->recv(boost::bind(&Client::handle_ready, shared_from_this(), _1, callback)); + } + } else { + callback(); } } -- cgit v1.2.3