1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#include "server.h"
#include "messages.h"
#include "terrain_generator.h"
#include <boost/format.hpp>
#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.recv(connection->socket);
std::cout << "fetching version" << std::endl;
uint8_t version = h.get_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);
message::Player player(connection->get_id(), connection->pos, connection->socket.local_endpoint().address().to_string());
for(std::list<Connection::p>::iterator it = clients.begin(); it != clients.end(); it++) {
player.send((*it)->socket);
Connection::p c = *it;
message::Player other(c->get_id(), c->pos, c->socket.local_endpoint().address().to_string());
other.send(connection->socket);
}
clients.push_back(connection);
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_type, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, connection, t));
}
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;
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;
case message::MSG_TYPE_MSG:
handle_message(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.recv(c->socket);
c->pos = m.get_pos();
TerrainGenerator::p tg = boost::static_pointer_cast<TerrainGenerator>(cache->tl);
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);
std::list<Vector3> trees = tg->get_objects(it->first, it->second, 35, 35);
for(std::list<Vector3>::iterator it = trees.begin(); it != trees.end(); it++) {
message::Object tree(0, *it);
tree.send(c->socket);
}
}
for(std::list<Connection::p>::iterator it = clients.begin(); it != clients.end(); it++) {
if(*it == c)
continue;
message::Pos p(c->get_id(), c->pos);
p.send((*it)->socket);
}
}
void Server::handle_chunk(Connection::p c) {
message::Chunk m;
m.recv(c->socket);
int64_t x, y;
m.get_coords(x, y);
}
void Server::handle_message(Connection::p c) {
message::Message m;
m.recv(c->socket);
std::string s = m.get_str();
for(std::list<Connection::p>::iterator it = clients.begin(); it != clients.end(); it++) {
message::Message msg(s);
msg.send((*it)->socket);
}
}
|