From 6f51fbfb06767000173647b57d0b755f4d02c247 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Mon, 15 Nov 2010 07:02:48 +0100 Subject: Added Lobby class to server, serving as the master application class. --- server/lobby.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ server/lobby.h | 25 +++++++++++++++++++++++++ server/main.cpp | 16 +++++----------- server/tcpserver.cpp | 16 +++++++--------- server/tcpserver.h | 5 +++++ 5 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 server/lobby.cpp create mode 100644 server/lobby.h (limited to 'server') 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 + +#include + +void Lobby::handle_connect(Connection::p connection) { + // Send Hello. + connection->send(make_shared("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(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(false)); + connection->recv(boost::bind(&Lobby::handle_login, this, connection, _1)); + return; + } + + connection->send(make_shared(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(); +} diff --git a/server/lobby.h b/server/lobby.h new file mode 100644 index 0000000..07ed13d --- /dev/null +++ b/server/lobby.h @@ -0,0 +1,25 @@ +#ifndef LOBBY_H +#define LOBBY_H + +#include + +#include "tcpserver.h" + +class Lobby { + private: + boost::asio::io_service io_service; + TCPServer server; + + //! Handle new connection. + void handle_connect(Connection::p connection); + + //! Handle login. + void handle_login(Connection::p connection, Message::p msg); + + public: + Lobby(); + + void run(); +}; + +#endif diff --git a/server/main.cpp b/server/main.cpp index 3e311e5..207d6d6 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -1,18 +1,12 @@ -#include -#include +#include "lobby.h" -#include "tcpserver.h" +#include int main() { try { - boost::asio::io_service io_service; - - TCPServer server(io_service); - - std::cout << "Listening to port 12345" << std::endl; - - io_service.run(); - + Lobby lobby; + lobby.run(); + } catch(std::exception& e) { std::cerr << "Exception caught: " << e.what() << std::endl; } diff --git a/server/tcpserver.cpp b/server/tcpserver.cpp index 545e666..3f34997 100644 --- a/server/tcpserver.cpp +++ b/server/tcpserver.cpp @@ -6,7 +6,7 @@ TCPServer::TCPServer(boost::asio::io_service& io_service) : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), 12345)) { // Start listening for first connection attempt. - listen(); + //listen(); } void TCPServer::listen() { @@ -16,19 +16,17 @@ void TCPServer::listen() { boost::bind(&TCPServer::handle_connection, this, new_connection, boost::asio::placeholders::error)); } - -//! Incoming connection, handle void TCPServer::handle_connection(Connection::p connection, const boost::system::error_code& error) { if(error) { return; } - connection->connected(); - - Message::p m = make_shared("aotenjoud git"); - - connection->send(m); + connect_callback(connection); +} + +void TCPServer::get_connection(boost::function f) { + connect_callback = f; - // Start listening for another connection attempt. + // Start listening for a connection attempt. listen(); } diff --git a/server/tcpserver.h b/server/tcpserver.h index 5d0cbba..47c5056 100644 --- a/server/tcpserver.h +++ b/server/tcpserver.h @@ -2,6 +2,7 @@ #define TCPSERVER_H #include +#include #include "connection.h" @@ -9,6 +10,8 @@ class TCPServer { private: boost::asio::ip::tcp::acceptor acceptor_; + boost::function connect_callback; + //! Listen for incoming connection. void listen(); @@ -17,6 +20,8 @@ class TCPServer { public: TCPServer(boost::asio::io_service& io_service); + + void get_connection(boost::function f); }; #endif -- cgit v1.2.3