libt2n: (gerd) small fixes, hello unit tests
[libt2n] / src / connection.cpp
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>
22 #include <iostream>
23
24 #include <netinet/in.h>
25
26 #include "connection.hxx"
27
28 namespace libt2n
29 {
30
31 connection::packet_size_indicator connection::bytes_available()
32 {
33     // max packet size is unsigned int
34
35     // no size information -> no packet
36     if (buffer.size() < sizeof(packet_size_indicator))
37         return 0;
38
39     packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data())));
40
41     // enough data for one packet in buffer?
42     if (buffer.size() < sizeof(packet_size_indicator)+psize)
43         return 0;
44
45     // ok, full packet there
46     return psize;
47 }
48
49 /** @brief read a complete data packet from the buffer. The packet is removed from the
50             connection buffer.
51     @param[out] data the data package
52     @retval true if packet found
53 */
54 bool connection::get_packet(std::string& data)
55 {
56     packet_size_indicator psize;
57
58     if ((psize=bytes_available()))
59     {
60         data.assign(buffer,sizeof(packet_size_indicator),psize);
61         buffer.erase(0,sizeof(packet_size_indicator)+psize);
62         return true;
63     }
64     else
65         return false;
66 }
67
68 /// send a blob to the peer
69 void connection::write(const std::string& data)
70 {
71     // prepend packet size to data
72     packet_size_indicator psize=htonl(data.size());
73     std::string send_data(data);
74     send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator));
75
76     real_write(send_data);
77 }
78
79 }