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"
28 #include <simgear/debug/logstream.hxx>
32 NetBuffer::NetBuffer( int _max_length )
35 max_length = _max_length ;
36 data = new char [ max_length+1 ] ; //for null terminator
39 NetBuffer::~NetBuffer ()
44 void NetBuffer::remove ()
49 void NetBuffer::remove (int pos, int n)
51 assert (pos>=0 && pos<length && (pos+n)<=length) ;
52 //if (pos>=0 && pos<length && (pos+n)<=length)
54 memmove(&data[pos],&data[pos+n],length-(pos+n)) ;
59 bool NetBuffer::append (const char* s, int n)
61 if ((length+n)<=max_length)
63 memcpy(&data[length],s,n) ;
70 bool NetBuffer::append (int n)
72 if ((length+n)<=max_length)
80 NetBufferChannel::NetBufferChannel (int in_buffer_size, int out_buffer_size) :
81 in_buffer (in_buffer_size),
82 out_buffer (out_buffer_size),
87 void NetBufferChannel::handleClose ( void )
90 out_buffer.remove () ;
92 NetChannel::handleClose () ;
96 bool NetBufferChannel::bufferSend (const char* msg, int msg_len)
98 if ( out_buffer.append(msg,msg_len) )
101 SG_LOG(SG_IO, SG_WARN, "NetBufferChannel: output buffer overflow!" ) ;
105 void NetBufferChannel::handleBufferRead (NetBuffer& buffer)
107 /* do something here */
112 NetBufferChannel::handleRead (void)
114 int max_read = in_buffer.getMaxLength() - in_buffer.getLength() ;
117 char* data = in_buffer.getData() + in_buffer.getLength() ;
118 int num_read = recv (data, max_read) ;
121 in_buffer.append (num_read) ;
122 //ulSetError ( UL_DEBUG, "netBufferChannel: %d read", num_read ) ;
125 if (in_buffer.getLength())
127 handleBufferRead (in_buffer);
132 NetBufferChannel::handleWrite (void)
134 if (out_buffer.getLength())
138 int length = out_buffer.getLength() ;
141 int num_sent = NetChannel::send (
142 out_buffer.getData(), length);
145 out_buffer.remove (0, num_sent);
146 //ulSetError ( UL_DEBUG, "netBufferChannel: %d sent", num_sent ) ;
150 else if (should_close)
156 } // of namespace simgear