summaryrefslogtreecommitdiff
path: root/server.cpp
diff options
context:
space:
mode:
authorJon Bergli Heier <snakebite@jvnv.net>2011-06-01 19:28:24 +0200
committerJon Bergli Heier <snakebite@jvnv.net>2011-06-01 19:28:24 +0200
commit9fa6460f22be5482239d0b903af985a519f77166 (patch)
tree7f4be0d51977ad90b184cd01aa9300f8efd2eb4d /server.cpp
Initial commit.
Diffstat (limited to 'server.cpp')
-rw-r--r--server.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/server.cpp b/server.cpp
new file mode 100644
index 0000000..18e099c
--- /dev/null
+++ b/server.cpp
@@ -0,0 +1,105 @@
+#include "server.h"
+#include "messages.h"
+#include "terrain_generator.h"
+
+#include <iostream>
+
+Server::Server(boost::asio::io_service& io_service)
+ : acceptor(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v6(), 54321)) {
+ TerrainGenerator::p gen(new TerrainGenerator(0, "map"));
+ cache = TerrainCache::p(new TerrainCache(gen, 500));
+ start_accept();
+}
+
+void Server::start_accept() {
+ Connection::p c(new Connection(acceptor.io_service()));
+ acceptor.async_accept(c->socket, boost::bind(&Server::handle_connect, this, c));
+}
+
+void Server::handle_connect(Connection::p connection) {
+ start_accept();
+
+ uint8_t type = message::MessageBase::read_type(connection->socket);
+ std::cout << "type: " << (int)type << std::endl;
+
+ message::Hello h;
+ //h.b = boost::asio::buffer(&version, sizeof(version));
+ std::cout << "reading payload" << std::endl;
+ h.read(connection->socket);
+ std::cout << "fetching version" << std::endl;
+ uint8_t version = h.read_version();
+ std::cout << "version: " << (int)version << std::endl;
+
+ async_read(connection);
+
+ /*boost::asio::streambuf b;
+ boost::asio::read_until(connection->get_socket(), b, '\n');
+
+ std::istream is(&b);
+ std::string s;
+ is >> s;
+ std::cout << s << std::endl;
+ for(std::list<Connection::p>::iterator it = clients.begin(); it != clients.end(); it++) {
+ boost::asio::write((*it)->get_socket(), boost::asio::buffer(s + "\n"));
+ }*/
+}
+
+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));
+}
+
+void Server::handle_message(const boost::system::error_code& error, size_t bytes_transferred, Connection::p connection, uint8_t *_type) {
+ uint8_t type = *_type;
+ delete _type;
+
+ if(error) {
+ std::cerr << "error: " << error.message() << std::endl;
+ for(std::list<Connection::p>::iterator it = clients.begin(); it != clients.end(); it++) {
+ if(*it == connection) {
+ clients.erase(it);
+ break;
+ }
+ }
+ return;
+ }
+
+ switch(type) {
+ case message::MSG_TYPE_POS:
+ handle_pos(connection);
+ break;
+ case message::MSG_TYPE_CHUNK:
+ handle_chunk(connection);
+ break;
+ default:
+ std::cout << "unknown type: " << (int)type << std::endl;
+ }
+
+ async_read(connection);
+}
+
+void Server::handle_pos(Connection::p c) {
+ message::Pos m;
+
+ m.read(c->socket);
+ m.get_pos(c->pos.x, c->pos.y, c->pos.z);
+
+ std::set<std::pair<int64_t, int64_t> > chunks = c->check_chunks();
+ for(std::set<std::pair<int64_t, int64_t> >::iterator it = chunks.begin(); it != chunks.end(); it++) {
+ // TODO: fix sizes
+ TerrainCacheObject::p obj = cache->get_chunk(it->first, it->second, 35, 35);
+ message::Chunk chunk(it->first, it->second);
+ chunk.set_data(obj->heights);
+ chunk.send(c->socket);
+ }
+}
+
+void Server::handle_chunk(Connection::p c) {
+ message::Chunk m;
+
+ m.read(c->socket);
+ int64_t x, y;
+ m.get_coords(x, y);
+}