Fix 'occurred' typo
[libt2n] / src / connection.hxx
CommitLineData
19facd85
TJ
1/*
2Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
3
4The software in this package is distributed under the GNU General
5Public License version 2 (with a special exception described below).
6
7A copy of GNU General Public License (GPL) is included in this distribution,
8in the file COPYING.GPL.
9
10As a special exception, if other files instantiate templates or use macros
11or inline functions from this file, or you compile this file and link it
12with other works to produce a work based on this file, this file
13does not by itself cause the resulting work to be covered
14by the GNU General Public License.
15
16However the source code for this file must still be made available
17in accordance with section (3) of the GNU General Public License.
18
19This exception does not invalidate any other reasons why a work based
20on this file might be covered by the GNU General Public License.
21*/
a11e19b7
GE
22#ifndef __LIBT2N_CONNECTION
23#define __LIBT2N_CONNECTION
24
25#include <string>
a64066eb
GE
26#include <vector>
27#include <list>
28#include <iostream>
29
30#include <boost/function.hpp>
a11e19b7 31
8104c8f7 32#include <netinet/in.h>
a7170401 33
8104c8f7 34#include "types.hxx"
a7170401 35
a11e19b7
GE
36namespace libt2n
37{
38
94247295
GE
39/** @brief a connection between client and server. abstact.
40*/
a11e19b7
GE
41class connection
42{
43 private:
44 bool closed;
45
a64066eb
GE
46 /// vector initialized for all callback-types, all elements in each list will be called
47 std::vector<std::list<boost::function<void ()> > > callbacks;
48
a11e19b7
GE
49 protected:
50 connection()
1eed3f4c 51 : closed(false), callbacks(__events_end)
a64066eb 52 { }
a11e19b7
GE
53
54 std::string buffer;
55
8104c8f7 56 typedef uint32_t packet_size_indicator;
a11e19b7
GE
57
58 packet_size_indicator bytes_available();
59
60 virtual void real_write(const std::string& data)=0;
61
a7170401
GE
62 virtual std::ostream* get_logstream(log_level_values level)=0;
63
a64066eb
GE
64 void do_callbacks(callback_event_type event);
65
af84dfb5
GE
66 void reopen(void);
67 void remove_incomplete_packets();
68
a11e19b7 69 public:
a64066eb 70 virtual ~connection();
a11e19b7 71
94247295 72 /// is this connection closed or not
a11e19b7
GE
73 bool is_closed()
74 { return closed; }
75
94247295 76 /// close this connection
a64066eb 77 virtual void close();
a11e19b7 78
94247295
GE
79 /** @brief look for new data and store it in the local buffer
80 @param usec_timeout wait until new data is found, max timeout usecs.
81 -1: wait endless
45a2ebc9
GE
82 0: return instantly
83 @param usec_timeout_remaining if non-NULL the function will write the
84 not used time to the given target
94247295
GE
85 @retval true if new data was found (does not mean that the received data
86 is a complete packet though)
87 */
45a2ebc9
GE
88 virtual bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL)=0;
89
a11e19b7 90 bool get_packet(std::string& data);
94247295 91
b2ba0928
GE
92 unsigned int peek_packet(std::string& data);
93
94247295 94 /// returns true if a complete data packet is in the buffer. retrieve it with get_packet().
a11e19b7
GE
95 bool packet_available()
96 { return bytes_available(); }
97
98 void write(const std::string& data);
a64066eb
GE
99
100 void add_callback(callback_event_type event, const boost::function<void ()>& func);
101
102 std::list<boost::function<void ()> > get_callback_list(callback_event_type event);
a11e19b7
GE
103};
104
105}
106
107#endif