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

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

#include "message.h"

class ConnectionBase {
	private:
		std::queue<Message::p> recv_queue;
		
		Message::p next_message;
		
		//! Create a new message and initiate reception.
		void prepare_next_message();
		
	protected:
		//! Signal that connection is established and ready to transfer data.
		void connected();
		
		//! 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;
		
	public:
		//! Send a message.
		void send(const Message::p& msg);
		
		//! Get received message or null if queue empty.
		Message::p recv();
		
		virtual ~ConnectionBase();
};

#endif