2 Copied from PLIB into SimGear
4 PLIB - A Suite of Portable Game Libraries
5 Copyright (C) 1998,2002 Steve Baker
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 For further information visit http://plib.sourceforge.net
23 $Id: NetChat.cxx 2117 2007-09-13 23:21:09Z fayjf $
26 #include <simgear/io/sg_netChat.hxx>
28 #include <cstring> // for strdup
33 NetChat::setTerminator (const char* t)
35 if (terminator) free(terminator);
36 terminator = strdup(t);
41 NetChat::getTerminator (void)
48 NetChat::setByteCount(int count)
50 if (terminator) free(terminator);
52 bytesToCollect = count;
55 // return the size of the largest prefix of needle at the end
58 #define MAX(a,b) (((a)>(b))?(a):(b))
61 find_prefix_at_end (const NetBuffer& haystack, const char* needle)
63 const char* hd = haystack.getData();
64 int hl = haystack.getLength();
65 int nl = strlen(needle);
67 for (int i = MAX (nl-hl, 0); i < nl; i++) {
68 //if (haystack.compare (needle, hl-(nl-i), nl-i) == 0) {
69 if (memcmp(needle, &hd[hl-(nl-i)], nl-i) == 0) {
77 find_terminator (const NetBuffer& haystack, const char* needle)
79 if (needle && *needle)
81 const char* data = haystack.getData();
82 const char* ptr = strstr(data,needle);
89 bool NetChat::push (const char* s)
91 return bufferSend ( s, strlen(s) ) ;
95 NetChat::handleBufferRead (NetBuffer& in_buffer)
97 // Continue to search for terminator in in_buffer,
98 // while calling collect_incoming_data. The while loop is
99 // necessary because we might read several data+terminator combos
100 // with a single recv().
102 while (in_buffer.getLength()) {
103 // special case where we're not using a terminator
104 if (terminator == 0 || *terminator == 0) {
105 if ( bytesToCollect > 0) {
106 const int toRead = std::min(in_buffer.getLength(), bytesToCollect);
107 collectIncomingData(in_buffer.getData(), toRead);
108 in_buffer.remove(0, toRead);
109 bytesToCollect -= toRead;
110 if (bytesToCollect == 0) { // read all requested bytes
113 } else { // read the whole lot
114 collectIncomingData (in_buffer.getData(),in_buffer.getLength());
121 int terminator_len = strlen(terminator);
123 int index = find_terminator ( in_buffer, terminator ) ;
126 // 1) end of buffer matches terminator exactly:
127 // collect data, transition
128 // 2) end of buffer matches some prefix:
129 // collect data to the prefix
130 // 3) end of buffer does not match any prefix:
134 // we found the terminator
135 collectIncomingData ( in_buffer.getData(), index ) ;
136 in_buffer.remove (0, index + terminator_len);
139 // check for a prefix of the terminator
140 int num = find_prefix_at_end (in_buffer, terminator);
142 int bl = in_buffer.getLength();
143 // we found a prefix, collect up to the prefix
144 collectIncomingData ( in_buffer.getData(), bl - num ) ;
145 in_buffer.remove (0, bl - num);
148 // no prefix, collect it all
149 collectIncomingData (in_buffer.getData(), in_buffer.getLength());
156 } // of namespace simgear