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. --- server.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'server.cpp') 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(); +} -- cgit v1.2.3