1 /***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License version *
7 * 2.1 as published by the Free Software Foundation. *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU Lesser General Public License for more details. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18 ***************************************************************************/
25 #include <netinet/in.h>
27 #include "connection.hxx"
32 connection::~connection()
34 // we want the connection_closed callbacks to be called before
37 do_callbacks(connection_deleted);
40 void connection::close()
45 do_callbacks(connection_closed);
49 /// get the number of bytes being available as next complete packet
50 connection::packet_size_indicator connection::bytes_available()
52 // no size information -> no packet
53 if (buffer.size() < sizeof(packet_size_indicator))
56 packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data())));
58 // enough data for one packet in buffer?
59 if (buffer.size() < sizeof(packet_size_indicator)+psize)
62 // ok, full packet there
66 /** @brief read a complete data packet from the buffer. The packet is removed from the
68 @param[out] data the data package
69 @retval true if packet found
71 bool connection::get_packet(std::string& data)
73 packet_size_indicator psize;
75 if ((psize=bytes_available()))
77 data.assign(buffer,sizeof(packet_size_indicator),psize);
78 buffer.erase(0,sizeof(packet_size_indicator)+psize);
85 /** @brief get (maybe incomplete) data of the next packet from the buffer. Does not remove the data
86 from the connection buffer.
87 @param[out] data the data package
88 @retval full size of the packet when it will be complete
90 unsigned int connection::peek_packet(std::string& data)
92 // no size information -> no packet
93 if (buffer.size() < sizeof(packet_size_indicator))
96 packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data())));
98 // not the full data available?
99 packet_size_indicator currsize=psize;
100 if (buffer.size() < currsize+sizeof(packet_size_indicator))
101 currsize=buffer.size()-sizeof(packet_size_indicator);
103 data.assign(buffer,sizeof(packet_size_indicator),currsize);
108 /// remove all data from buffer that is not a complete packet
109 void connection::remove_incomplete_packets()
111 std::string::size_type p=0;
112 std::string::size_type end=buffer.size();
116 // not enough space for size information -> no packet
117 if (p+sizeof(packet_size_indicator) > end)
120 packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data()+p)));
122 if (p+sizeof(packet_size_indicator)+psize > end)
129 // move p to where the next packet will start
130 p+=sizeof(packet_size_indicator)+psize;
136 // incomplete packets there, remove them
141 /// send a blob to the peer
142 void connection::write(const std::string& data)
144 // prepend packet size to data
145 packet_size_indicator psize=htonl(data.size());
146 std::string send_data(data);
147 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
149 real_write(send_data);
152 /** @brief add a callback
154 @param event event the function will be called at
155 @param func functor (see boost function) that will be called
157 @note use boost::bind to bind to member functions and parameters like this:
158 17 is a fixed parameter that is always added to the call
159 c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17));
161 void connection::add_callback(callback_event_type event, const boost::function<void ()>& func)
163 callbacks[event].push_back(func);
166 /** @brief an event has occured, execute the callbacks that are registered for this event
168 @param event event type that has occured
170 void connection::do_callbacks(callback_event_type event)
172 std::list<boost::function<void ()> >::iterator i,ie=callbacks[event].end();
173 for (i=callbacks[event].begin(); i != ie; i++)
177 /** @brief get the callbacks in place for one event
179 @param event event the callbacks should be registered for
180 @return std::list of functors (boost::function) with the callbacks
182 @note if you want to get the callbacks for all events, loop from 0 to __events_end
184 std::list<boost::function<void ()> > connection::get_callback_list(callback_event_type event)
186 return callbacks[event];
189 /** @brief reopen a already closed connection, removes incomplete packets from the buffer
191 @note Only call when the connection is closed.
193 @note Justs cares about the data of connection, reconnecting has to be
194 done in a derived class.
196 void connection::reopen()
199 throw std::logic_error("connection::reopen() called with connection still open");
203 // incomplete buffer data is worthless with a new connection
204 remove_incomplete_packets();
206 do_callbacks(new_connection);