From 8cd1126ab17f9b0bead53c8f88f6929211bc5765 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Thu, 2 Jun 2011 00:20:16 +0200 Subject: Send a simple welcome message to the client. --- common | 2 +- server.cpp | 26 ++++++++++++++++++++++++-- server.h | 3 ++- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/common b/common index 1211f63..555f65a 160000 --- a/common +++ b/common @@ -1 +1 @@ -Subproject commit 1211f632092db824fb2c880063599f7642c34f30 +Subproject commit 555f65ad7171511d74513b7055552a941a6fe0f2 diff --git a/server.cpp b/server.cpp index 18e099c..e2fd37d 100644 --- a/server.cpp +++ b/server.cpp @@ -2,6 +2,8 @@ #include "messages.h" #include "terrain_generator.h" +#include + #include Server::Server(boost::asio::io_service& io_service) @@ -30,6 +32,10 @@ void Server::handle_connect(Connection::p connection) { uint8_t version = h.read_version(); std::cout << "version: " << (int)version << std::endl; + message::Message m((boost::format("Welcome [colour='FFFF0000']%s[colour='FFFFFFFF'], you've connected to [colour='FF0000FF']%s") % connection->socket.local_endpoint().address().to_string() + % connection->socket.remote_endpoint().address().to_string()).str()); + m.send(connection->socket); + async_read(connection); /*boost::asio::streambuf b; @@ -48,10 +54,10 @@ void Server::async_read(Connection::p connection) { uint8_t *t = new uint8_t; boost::asio::async_read(connection->socket, boost::asio::buffer(t, sizeof(uint8_t)), boost::asio::transfer_all(), - boost::bind(&Server::handle_message, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, connection, t)); + boost::bind(&Server::handle_type, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, connection, t)); } -void Server::handle_message(const boost::system::error_code& error, size_t bytes_transferred, Connection::p connection, uint8_t *_type) { +void Server::handle_type(const boost::system::error_code& error, size_t bytes_transferred, Connection::p connection, uint8_t *_type) { uint8_t type = *_type; delete _type; @@ -73,6 +79,9 @@ void Server::handle_message(const boost::system::error_code& error, size_t bytes case message::MSG_TYPE_CHUNK: handle_chunk(connection); break; + case message::MSG_TYPE_MSG: + handle_message(connection); + break; default: std::cout << "unknown type: " << (int)type << std::endl; } @@ -103,3 +112,16 @@ void Server::handle_chunk(Connection::p c) { int64_t x, y; m.get_coords(x, y); } + +void Server::handle_message(Connection::p c) { + message::Message m; + + m.read(c->socket); + // call this to tell the object we know the string length + uint16_t len = m.get_len(); + std::cout << "string length: " << len << std::endl; + + // string is fetched on next read + m.read(c->socket); + std::string s = m.get_str(); +} diff --git a/server.h b/server.h index 8b0f637..992e792 100644 --- a/server.h +++ b/server.h @@ -24,10 +24,11 @@ class Server { void handle_connect(Connection::p connection); void async_read(Connection::p connection); - void handle_message(const boost::system::error_code& error, size_t bytes_transferred, Connection::p connection, uint8_t *_type); + void handle_type(const boost::system::error_code& error, size_t bytes_transferred, Connection::p connection, uint8_t *_type); void handle_pos(Connection::p c); void handle_chunk(Connection::p c); + void handle_message(Connection::p c); }; #endif -- cgit v1.2.3