Commit | Line | Data |
---|---|---|
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> |
a11e19b7 | 23 | |
8104c8f7 GE |
24 | #include <netinet/in.h> |
25 | ||
a11e19b7 GE |
26 | #include "connection.hxx" |
27 | ||
28 | namespace libt2n | |
29 | { | |
30 | ||
31 | connection::packet_size_indicator connection::bytes_available() | |
32 | { | |
a11e19b7 | 33 | // no size information -> no packet |
8104c8f7 | 34 | if (buffer.size() < sizeof(packet_size_indicator)) |
644c4d26 | 35 | return 0; |
a11e19b7 | 36 | |
8104c8f7 | 37 | packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data()))); |
a11e19b7 GE |
38 | |
39 | // enough data for one packet in buffer? | |
8104c8f7 | 40 | if (buffer.size() < sizeof(packet_size_indicator)+psize) |
644c4d26 | 41 | return 0; |
a11e19b7 GE |
42 | |
43 | // ok, full packet there | |
644c4d26 | 44 | return psize; |
a11e19b7 GE |
45 | } |
46 | ||
94247295 GE |
47 | /** @brief read a complete data packet from the buffer. The packet is removed from the |
48 | connection buffer. | |
49 | @param[out] data the data package | |
50 | @retval true if packet found | |
51 | */ | |
a11e19b7 GE |
52 | bool connection::get_packet(std::string& data) |
53 | { | |
54 | packet_size_indicator psize; | |
55 | ||
56 | if ((psize=bytes_available())) | |
57 | { | |
8104c8f7 GE |
58 | data.assign(buffer,sizeof(packet_size_indicator),psize); |
59 | buffer.erase(0,sizeof(packet_size_indicator)+psize); | |
a11e19b7 GE |
60 | return true; |
61 | } | |
62 | else | |
63 | return false; | |
64 | } | |
65 | ||
b2ba0928 GE |
66 | /** @brief get (maybe incomplete) data of the next packet from the buffer. Does not remove the data |
67 | from the connection buffer. | |
68 | @param[out] data the data package | |
69 | @retval full size of the packet when it will be complete | |
70 | */ | |
71 | unsigned int connection::peek_packet(std::string& data) | |
72 | { | |
73 | // no size information -> no packet | |
74 | if (buffer.size() < sizeof(packet_size_indicator)) | |
75 | return 0; | |
76 | ||
77 | packet_size_indicator psize=ntohl(*((packet_size_indicator*)(buffer.data()))); | |
78 | ||
79 | // not the full data available? | |
80 | packet_size_indicator currsize=psize; | |
81 | if (buffer.size() < currsize+sizeof(packet_size_indicator)) | |
82 | currsize=buffer.size()-sizeof(packet_size_indicator); | |
83 | ||
84 | data.assign(buffer,sizeof(packet_size_indicator),currsize); | |
85 | ||
86 | return psize; | |
87 | } | |
88 | ||
94247295 | 89 | /// send a blob to the peer |
a11e19b7 GE |
90 | void connection::write(const std::string& data) |
91 | { | |
92 | // prepend packet size to data | |
8104c8f7 | 93 | packet_size_indicator psize=htonl(data.size()); |
a11e19b7 | 94 | std::string send_data(data); |
644c4d26 | 95 | send_data.insert(0,(char*)&psize,sizeof(packet_size_indicator)); |
a11e19b7 GE |
96 | |
97 | real_write(send_data); | |
98 | } | |
99 | ||
a11e19b7 | 100 | } |