2 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy
4 The software in this package is distributed under the GNU General
5 Public License version 2 (with a special exception described below).
7 A copy of GNU General Public License (GPL) is included in this distribution,
8 in the file COPYING.GPL.
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.
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.
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.
22 #ifndef __LIBT2N_CONNECTION
23 #define __LIBT2N_CONNECTION
30 #include <boost/function.hpp>
32 #include <netinet/in.h>
39 /** @brief a connection between client and server. abstact.
46 /// vector initialized for all callback-types, all elements in each list will be called
47 std::vector<std::list<boost::function<void ()> > > callbacks;
51 : callbacks(__events_end), closed(false)
56 typedef uint32_t packet_size_indicator;
58 packet_size_indicator bytes_available();
60 virtual void real_write(const std::string& data)=0;
62 virtual std::ostream* get_logstream(log_level_values level)=0;
64 void do_callbacks(callback_event_type event);
67 void remove_incomplete_packets();
70 virtual ~connection();
72 /// is this connection closed or not
76 /// close this connection
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.
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)
88 virtual bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL)=0;
90 bool get_packet(std::string& data);
92 unsigned int peek_packet(std::string& data);
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(); }
98 void write(const std::string& data);
100 void add_callback(callback_event_type event, const boost::function<void ()>& func);
102 std::list<boost::function<void ()> > get_callback_list(callback_event_type event);