summaryrefslogtreecommitdiff
path: root/src/connection.cpp
blob: 1e38f7ba3cf5d4a70c7694f4f1fd95f7e89364b6 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#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_code) {
	if(error_code) {
		error("Read error.");
		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));
}

void Connection::got_message(const Message::p& msg) {
	// TODO: Implement message queueing. For now, unexpected messages will cause an error.
	
	boost::function<void (Message::p)> f = recv_callback;
	recv_callback.clear();
	error_callback.clear();
	
	f(msg);
}

void Connection::error(const std::string& msg) {
	if(error_callback) {
		boost::function<void (const std::string&)> f = error_callback;
		recv_callback.clear();
		error_callback.clear();
		
		f(msg);
	} else {
		recv_callback.clear();
	}
}

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)> callback, boost::function<void (const std::string&)> error) {
	recv_callback = callback;
	error_callback = error;
	
	start_recv();
}