summaryrefslogtreecommitdiff
path: root/server.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-06-02 00:20:16 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-06-02 00:20:16 +0200
commit8cd1126ab17f9b0bead53c8f88f6929211bc5765 (patch)
tree02c84948b06ba305895dc9289f3187e05d6dcc80 /server.cpp
parentd4b3c37642bb9be28ef34885cb33e60ae0f1fa79 (diff)
Send a simple welcome message to the client.
Diffstat (limited to 'server.cpp')
-rw-r--r--server.cpp26
1 files changed, 24 insertions, 2 deletions
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 <boost/format.hpp>
+
#include <iostream>
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();
+}