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