summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/lobby.cpp48
-rw-r--r--server/lobby.h25
-rw-r--r--server/main.cpp16
-rw-r--r--server/tcpserver.cpp16
-rw-r--r--server/tcpserver.h5
5 files changed, 90 insertions, 20 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();
+}
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 <boost/asio.hpp>
+
+#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 <iostream>
-#include <boost/asio.hpp>
+#include "lobby.h"
-#include "tcpserver.h"
+#include <iostream>
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<Message::Hello>("aotenjoud git");
-
- connection->send(m);
+ connect_callback(connection);
+}
+
+void TCPServer::get_connection(boost::function<void (Connection::p)> 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 <boost/asio.hpp>
+#include <boost/function.hpp>
#include "connection.h"
@@ -9,6 +10,8 @@ class TCPServer {
private:
boost::asio::ip::tcp::acceptor acceptor_;
+ boost::function<void (Connection::p)> 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<void (Connection::p)> f);
};
#endif