libt2n: (gerd) bugfixes, better logging, unit tests for wrapper, ignore handler still...
[libt2n] / src / connection.cpp
CommitLineData
a11e19b7
GE
1/***************************************************************************
2 * Copyright (C) 2006 by Gerd v. Egidy *
3 * gve@intra2net.com *
4 * *
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. *
8 * *
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. *
13 * *
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 ***************************************************************************/
19
20#include <string>
21#include <sstream>
a7170401 22#include <iostream>
af84dfb5 23#include <stdexcept>
a11e19b7 24
8104c8f7
GE
25#include <netinet/in.h>
26
a11e19b7
GE
27#include "connection.hxx"
28
29namespace libt2n
30{
31
a64066eb
GE
32connection::~connection()
33{
34 // we want the connection_closed callbacks to be called before
35 close();
36
37 do_callbacks(connection_deleted);
38}
39
40void connection::close()
41{
42 if (!is_closed())
43 {
44 closed=true;
45 do_callbacks(connection_closed);
46 }
47}
48
a11e19b7
GE
49connection::packet_size_indicator connection::bytes_available()
50{
a11e19b7 51 // no size information -> no packet
8104c8f7 52 if (buffer.size() < sizeof(packet_size_indicator))
644c4d26 53 return 0;
a11e19b7 54
8104c8f7 55 packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data())));
a11e19b7
GE
56
57 // enough data for one packet in buffer?
8104c8f7 58 if (buffer.size() < sizeof(packet_size_indicator)+psize)
644c4d26 59 return 0;
a11e19b7
GE
60
61 // ok, full packet there
644c4d26 62 return psize;
a11e19b7
GE
63}
64
94247295
GE
65/** @brief read a complete data packet from the buffer. The packet is removed from the
66 connection buffer.
67 @param[out] data the data package
68 @retval true if packet found
69*/
a11e19b7
GE
70bool connection::get_packet(std::string& data)
71{
72 packet_size_indicator psize;
73
74 if ((psize=bytes_available()))
75 {
8104c8f7
GE
76 data.assign(buffer,sizeof(packet_size_indicator),psize);
77 buffer.erase(0,sizeof(packet_size_indicator)+psize);
a11e19b7
GE
78 return true;
79 }
80 else
81 return false;
82}
83
b2ba0928
GE
84/** @brief get (maybe incomplete) data of the next packet from the buffer. Does not remove the data
85 from the connection buffer.
86 @param[out] data the data package
87 @retval full size of the packet when it will be complete
88*/
89unsigned int connection::peek_packet(std::string& data)
90{
91 // no size information -> no packet
92 if (buffer.size() < sizeof(packet_size_indicator))
93 return 0;
94
95 packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data())));
96
97 // not the full data available?
98 packet_size_indicator currsize=psize;
99 if (buffer.size() < currsize+sizeof(packet_size_indicator))
100 currsize=buffer.size()-sizeof(packet_size_indicator);
101
102 data.assign(buffer,sizeof(packet_size_indicator),currsize);
103
104 return psize;
105}
106
af84dfb5
GE
107/// remove all data from buffer that is not a complete packet
108void connection::remove_incomplete_packets()
109{
110 std::string::size_type p=0;
111 std::string::size_type end=buffer.size();
112
113 while (p < end)
114 {
115 // not enough space for size information -> no packet
116 if (p+sizeof(packet_size_indicator) > end)
117 break;
118
119 packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data()+p)));
120
121 if (p+sizeof(packet_size_indicator)+psize > end)
122 {
123 // incomplete packet
124 break;
125 }
126 else
127 {
128 // move p to where the next packet will start
129 p+=sizeof(packet_size_indicator)+psize;
130 }
131 }
132
133 if (p < end)
134 {
135 // incomplete packets there, remove them
136 buffer.erase(p);
137 }
138}
139
94247295 140/// send a blob to the peer
a11e19b7
GE
141void connection::write(const std::string& data)
142{
143 // prepend packet size to data
8104c8f7 144 packet_size_indicator psize=htonl(data.size());
a11e19b7 145 std::string send_data(data);
644c4d26 146 send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
a11e19b7
GE
147
148 real_write(send_data);
149}
150
a64066eb
GE
151/** @brief add a callback
152
153 @param event event the function will be called at
154 @param func functor (see boost function) that will be called
155
156 @note use boost::bind to bind to member functions and parameters like this:
157 17 is a fixed parameter that is always added to the call
158 c.add_callback(connection_closed,bind(&my_class::func_to_call_back, boost::ref(*this), 17));
159*/
160void connection::add_callback(callback_event_type event, const boost::function<void ()>& func)
161{
a64066eb
GE
162 callbacks[event].push_back(func);
163}
164
165/** @brief an event has occured, execute the callbacks that are registered for this event
166
167 @param event event type that has occured
168*/
169void connection::do_callbacks(callback_event_type event)
170{
171 std::list<boost::function<void ()> >::iterator i,ie=callbacks[event].end();
172 for (i=callbacks[event].begin(); i != ie; i++)
173 (*i)();
174}
175
176/** @brief get the callbacks in place for one event
177
178 @param event event the callbacks should be registered for
179 @return std::list of functors (boost::function) with the callbacks
180
181 @note if you want to get the callbacks for all events, loop from 0 to __events_end
182*/
183std::list<boost::function<void ()> > connection::get_callback_list(callback_event_type event)
184{
185 return callbacks[event];
186}
187
af84dfb5
GE
188/** @brief reopen a already closed connection, removes incomplete packets from the buffer
189
190 @note Only call when the connection is closed.
191
192 @note Justs cares about the data of connection, reconnecting has to be
193 done in a derived class.
194*/
195void connection::reopen()
196{
197 if (!is_closed())
198 throw std::logic_error("connection::reopen() called with connection still open");
199
200 closed=false;
201
202 // incomplete buffer data is worthless with a new connection
203 remove_incomplete_packets();
204
205 do_callbacks(new_connection);
206}
a64066eb 207
a11e19b7 208}