summaryrefslogtreecommitdiff
path: root/server/connection.cpp
blob: b64ae119683ef828de64afdbfd5519c3e785b5f8 (plain)
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
#include "connection.h"

#include <boost/bind.hpp>

Connection::Connection(boost::asio::io_service& io_service) : socket(io_service) {
	
}

void Connection::handle_read(uint8_t* data, std::size_t bytes, const boost::system::error_code& error) {
	if(error) {
		recv_callback = NULL;
		return;
	}
	
	got_data(data, bytes);
	
	delete[] data;
}

void Connection::handle_write() {
	
}

void Connection::request_data(std::size_t bytes) {
	uint8_t* buf = new uint8_t[bytes];
	
	boost::asio::async_read(socket, boost::asio::buffer(buf, bytes),
		boost::bind(&Connection::handle_read, shared_from_this(), buf, bytes, boost::asio::placeholders::error));
	
	// boost::asio::placeholders::error
}

void Connection::got_message(const Message::p& msg) {
	recv_callback(msg);
}

void Connection::write_data(uint8_t* data, std::size_t bytes) {
	boost::asio::async_write(socket, boost::asio::buffer(data, bytes),
		boost::bind(&Connection::handle_write, shared_from_this()));
}

Connection::p Connection::create(boost::asio::io_service& io_service) {
	return Connection::p(new Connection(io_service));
}

void Connection::recv(boost::function<void (Message::p)> f) {
	recv_callback = f;
	
	start_recv();
}