summaryrefslogtreecommitdiff
path: root/server/connection.h
blob: 3428b883ec664fabc136cb83906ef709808ac2c3 (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
#ifndef CONNECTION_H
#define CONNECTION_H

#include <boost/asio.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/function.hpp>

#include "../common/connectionbase.h"

class Connection : public ConnectionBase, public boost::enable_shared_from_this<Connection> {
	private:
		friend class TCPServer;
		
		boost::asio::ip::tcp::socket socket;
		
		boost::function<void (Message::p)> recv_callback;
		
		Connection(boost::asio::io_service& io_service);
		
		//! Callback for when data is read.
		void handle_read(uint8_t* data, std::size_t bytes, const boost::system::error_code& error);
		
		//! Callback for when data is written.
		void handle_write();
	
	protected:
		//! Implements request_data().
		virtual void request_data(std::size_t bytes);
		
		//! Implements write_data().
		virtual void write_data(uint8_t* data, std::size_t bytes);
		
		//! Implements got_message().
		virtual void got_message(const Message::p& msg);
	
	public:
		typedef boost::shared_ptr<Connection> p;
		
		//! Constructs a new instance and returns a shared pointer.
		static p create(boost::asio::io_service& io_service);
		
		//! Initiates an asynchronous message receive.
		void recv(boost::function<void (Message::p)> f);
};

#endif