summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
m---------common0
-rw-r--r--server.cpp26
-rw-r--r--server.h3
3 files changed, 26 insertions, 3 deletions
diff --git a/common b/common
-Subproject 1211f632092db824fb2c880063599f7642c34f3
+Subproject 555f65ad7171511d74513b7055552a941a6fe0f
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();
+}
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