summaryrefslogtreecommitdiff
path: root/common/connectionbase.h
blob: f86a9ab9472bf15958319b167d98314011f67f85 (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
#ifndef CONNECTIONBASE_H
#define CONNECTIONBASE_H

#include <stdint.h>
#include <cstdlib>
#include <string>

#include "message.h"

class ConnectionBase {
	private:
		Message::Type pending_type;
		std::size_t pending_size;
	
	protected:
		//! Initiate reception of a message.
		void start_recv();
		
		//! Deliver received data.
		//! \param data Pointer to received data. Ownership is retained by caller.
		//! \param bytes Size of data.
		void got_data(uint8_t* data, std::size_t bytes);
		
		//! Called to request data.
		//! \param bytes Amount of data requested.
		virtual void request_data(std::size_t bytes) = 0;
		
		//! Called to write data.
		//! \param data Pointer to data to send. Ownership is transferred to callee.
		//! \param bytes Size of data.
		virtual void write_data(uint8_t* data, std::size_t bytes) = 0;
		
		//! Called when a message is received.
		//! \param msg Received message.
		virtual void got_message(const Message::p& msg) = 0;
		
		//! Called upon error (lost connection or failed deserialization).
		//! \param msg Error message.
		virtual void error(const std::string& msg) = 0;
		
	public:
		ConnectionBase();
		virtual ~ConnectionBase();
		
		//! Send a message.
		void send(const Message::p& msg);
};

#endif