summaryrefslogtreecommitdiff
path: root/server/client.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'server/client.cpp')
-rw-r--r--server/client.cpp51
1 files changed, 34 insertions, 17 deletions
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 <boost/bind.hpp>
-Client::p Client::create(Connection::p c, boost::function<void (Client::p)> f) {
- Client::p p(new Client(c));
- p->start(f);
+std::map<uint64_t, boost::weak_ptr<Client> > 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<void (Client::p)> f) {
- // Send Hello.
- connection->send(make_shared<Message::Hello>("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>(Message::LoginResponse::Lobby));
}
void Client::handle_login(Message::p msg, boost::function<void (Client::p)> login_callback) {
@@ -94,10 +115,6 @@ void Client::lobby_status(const std::vector<std::string>& 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_;
}