00001 /* 00002 Copyright (C) 2006 by Intra2net AG - Gerd v. Egidy 00003 00004 The software in this package is distributed under the GNU General 00005 Public License version 2 (with a special exception described below). 00006 00007 A copy of GNU General Public License (GPL) is included in this distribution, 00008 in the file COPYING.GPL. 00009 00010 As a special exception, if other files instantiate templates or use macros 00011 or inline functions from this file, or you compile this file and link it 00012 with other works to produce a work based on this file, this file 00013 does not by itself cause the resulting work to be covered 00014 by the GNU General Public License. 00015 00016 However the source code for this file must still be made available 00017 in accordance with section (3) of the GNU General Public License. 00018 00019 This exception does not invalidate any other reasons why a work based 00020 on this file might be covered by the GNU General Public License. 00021 */ 00022 #ifndef __LIBT2N_CONNECTION 00023 #define __LIBT2N_CONNECTION 00024 00025 #include <string> 00026 #include <vector> 00027 #include <list> 00028 #include <iostream> 00029 00030 #include <boost/function.hpp> 00031 00032 #include <netinet/in.h> 00033 00034 #include "types.hxx" 00035 00036 namespace libt2n 00037 { 00038 00041 class connection 00042 { 00043 private: 00044 bool closed; 00045 00047 std::vector<std::list<boost::function<void ()> > > callbacks; 00048 00049 protected: 00050 connection() 00051 : closed(false), callbacks(__events_end) 00052 { } 00053 00054 std::string buffer; 00055 00056 typedef uint32_t packet_size_indicator; 00057 00058 packet_size_indicator bytes_available(); 00059 00060 virtual void real_write(const std::string& data)=0; 00061 00062 virtual std::ostream* get_logstream(log_level_values level)=0; 00063 00064 void do_callbacks(callback_event_type event); 00065 00066 void reopen(void); 00067 void remove_incomplete_packets(); 00068 00069 public: 00070 virtual ~connection(); 00071 00073 bool is_closed() 00074 { return closed; } 00075 00077 virtual void close(); 00078 00088 virtual bool fill_buffer(long long usec_timeout=-1,long long* usec_timeout_remaining=NULL)=0; 00089 00090 bool get_packet(std::string& data); 00091 00092 unsigned int peek_packet(std::string& data); 00093 00095 bool packet_available() 00096 { return bytes_available(); } 00097 00098 void write(const std::string& data); 00099 00100 void add_callback(callback_event_type event, const boost::function<void ()>& func); 00101 00102 std::list<boost::function<void ()> > get_callback_list(callback_event_type event); 00103 }; 00104 00105 } 00106 00107 #endif