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: netBuffer.cxx 1568 2002-09-02 06:05:49Z sjbaker $
26 #include "sg_netBuffer.hxx"
31 #include <simgear/debug/logstream.hxx>
35 NetBuffer::NetBuffer( int _max_length )
38 max_length = _max_length ;
39 data = new char [ max_length+1 ] ; //for null terminator
42 NetBuffer::~NetBuffer ()
47 void NetBuffer::remove ()
52 void NetBuffer::remove (int pos, int n)
54 assert (pos>=0 && pos<length && (pos+n)<=length) ;
55 //if (pos>=0 && pos<length && (pos+n)<=length)
57 memmove(&data[pos],&data[pos+n],length-(pos+n)) ;
62 bool NetBuffer::append (const char* s, int n)
64 if ((length+n)<=max_length)
66 memcpy(&data[length],s,n) ;
73 bool NetBuffer::append (int n)
75 if ((length+n)<=max_length)
83 NetBufferChannel::NetBufferChannel (int in_buffer_size, int out_buffer_size) :
84 in_buffer (in_buffer_size),
85 out_buffer (out_buffer_size),
90 void NetBufferChannel::handleClose ( void )
93 out_buffer.remove () ;
95 NetChannel::handleClose () ;
99 bool NetBufferChannel::bufferSend (const char* msg, int msg_len)
101 if ( out_buffer.append(msg,msg_len) )
104 SG_LOG(SG_IO, SG_WARN, "NetBufferChannel: output buffer overflow!" ) ;
108 void NetBufferChannel::handleBufferRead (NetBuffer& buffer)
110 /* do something here */
115 NetBufferChannel::handleRead (void)
117 int max_read = in_buffer.getMaxLength() - in_buffer.getLength() ;
120 char* data = in_buffer.getData() + in_buffer.getLength() ;
121 int num_read = recv (data, max_read) ;
124 in_buffer.append (num_read) ;
125 //ulSetError ( UL_DEBUG, "netBufferChannel: %d read", num_read ) ;
128 if (in_buffer.getLength())
130 handleBufferRead (in_buffer);
135 NetBufferChannel::handleWrite (void)
137 if (out_buffer.getLength())
141 int length = out_buffer.getLength() ;
144 int num_sent = NetChannel::send (
145 out_buffer.getData(), length);
148 out_buffer.remove (0, num_sent);
149 //ulSetError ( UL_DEBUG, "netBufferChannel: %d sent", num_sent ) ;
153 else if (should_close)
159 } // of namespace simgear