diff options
Diffstat (limited to 'server/lobby.cpp')
-rw-r--r-- | server/lobby.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/server/lobby.cpp b/server/lobby.cpp new file mode 100644 index 0000000..f74a4f2 --- /dev/null +++ b/server/lobby.cpp @@ -0,0 +1,48 @@ +#include "lobby.h" + +#include <boost/bind.hpp> + +#include <iostream> + +void Lobby::handle_connect(Connection::p connection) { + // Send Hello. + connection->send(make_shared<Message::Hello>("aotenjoud git")); + + // Wait for Login. + connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1)); + + // Get another connection. + server.get_connection(boost::bind(&Lobby::handle_connect, this, _1)); +} + +void Lobby::handle_login(Connection::p connection, Message::p msg) { + if(msg->type != Message::Types::Login) { + return; + } + + Message::Login::p login_msg = dynamic_pointer_cast<Message::Login>(msg); + + std::cout << "Player " << login_msg->nick << " entered the lobby." << std::endl; + + // Check if nick is invalid. + if(login_msg->nick.size() == 0) { + connection->send(make_shared<Message::LoginResponse>(false)); + connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1)); + return; + } + + connection->send(make_shared<Message::LoginResponse>(true)); + + // Do something with the connection to keep it around. +} + +Lobby::Lobby() : server(io_service) { + +} + +void Lobby::run() { + // Get a connection. + server.get_connection(boost::bind(&Lobby::handle_connect, this, _1)); + + io_service.run(); +} |