From 18e73025a15b98aae008ffe5a19570a0e5ea19ef Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Tue, 7 Dec 2010 02:02:42 +0100 Subject: Completed framework for reconnection. Still missing client-fallback and resync upon reconnect. --- server/client.cpp | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) (limited to 'server/client.cpp') diff --git a/server/client.cpp b/server/client.cpp index 07a59f4..af2494e 100644 --- a/server/client.cpp +++ b/server/client.cpp @@ -2,32 +2,53 @@ #include -Client::p Client::create(Connection::p c, boost::function f) { - Client::p p(new Client(c)); - p->start(f); +std::map > Client::client_list; + +Client::p Client::create(Connection::p c, std::string n) { + Client::p p(new Client(c, n)); + p->start(); return p; } -Client::Client(Connection::p c) : connection(c), timer(c->socket.get_io_service()) { +Client::p Client::exists(Message::Login::p login_msg) { + if(client_list.count(login_msg->cookie)) { + // Obtain shared pointer to existing client. + Client::p client = client_list[login_msg->cookie].lock(); + + // TODO: Check if game still is ongoing? + // A client should only be reconnected to if still participating in an active game. + // It may however be proven that a client is always destructed if the game has ended, so this test might not be neccessary. + + return client; + + } else { + // No or invalid existing client found. + return Client::p(); + } +} + +Client::Client(Connection::p c, std::string n) : connection(c), timer(c->socket.get_io_service()), nick_(n) { } -void Client::disconnect() { - //delete connection; - //connection = NULL; - //We need to make this player dumb? +Client::~Client() { + client_list.erase(cookie); } void Client::reconnect(Connection::p c) { connection = c; + // TODO: Resync client. } -void Client::start(boost::function f) { - // Send Hello. - connection->send(make_shared("aotenjoud git")); +void Client::start() { + // Create a unique cookie for this client. + cookie = uint64_t(this); // TODO: Use a better cookie source. + + // Store a weak pointer to this client. + client_list[cookie] = shared_from_this(); - // Wait for Login. - connection->recv(boost::bind(&Client::handle_login, shared_from_this(), _1, f)); + // Logged in to lobby. + connection->send(make_shared(Message::LoginResponse::Lobby)); } void Client::handle_login(Message::p msg, boost::function login_callback) { @@ -94,10 +115,6 @@ void Client::lobby_status(const std::vector& game_modes, boost::fun connection->recv(boost::bind(&Client::handle_lobby, shared_from_this(), _1, callback)); } -unsigned int Client::id() { - return id_; -} - std::string Client::nick() { return nick_; } -- cgit v1.2.3