]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_netBuffer.cxx
Migrate relevant PLIB netXXX classes into SimGear.
[simgear.git] / simgear / io / sg_netBuffer.cxx
1 /*
2     Copied from PLIB into SimGear
3     
4      PLIB - A Suite of Portable Game Libraries
5      Copyright (C) 1998,2002  Steve Baker
6  
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.
11  
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.
16  
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
20  
21      For further information visit http://plib.sourceforge.net
22
23      $Id: netBuffer.cxx 1568 2002-09-02 06:05:49Z sjbaker $
24 */
25
26 #include "sg_netBuffer.hxx"
27
28 #include <simgear/debug/logstream.hxx>
29
30 namespace simgear {
31   
32 NetBuffer::NetBuffer( int _max_length )
33 {
34   length = 0 ;
35   max_length = _max_length ;
36   data = new char [ max_length+1 ] ;  //for null terminator
37 }
38
39 NetBuffer::~NetBuffer ()
40 {
41   delete[] data ;
42 }
43
44 void NetBuffer::remove ()
45 {
46   length = 0 ;
47 }
48
49 void NetBuffer::remove (int pos, int n)
50 {
51   assert (pos>=0 && pos<length && (pos+n)<=length) ;
52   //if (pos>=0 && pos<length && (pos+n)<=length)
53   {
54     memmove(&data[pos],&data[pos+n],length-(pos+n)) ;
55     length -= n ;
56   }
57 }
58
59 bool NetBuffer::append (const char* s, int n)
60 {
61   if ((length+n)<=max_length)
62   {
63     memcpy(&data[length],s,n) ;
64     length += n ;
65     return true ;
66   }
67   return false ;
68 }
69
70 bool NetBuffer::append (int n)
71 {
72   if ((length+n)<=max_length)
73   {
74     length += n ;
75     return true ;
76   }
77   return false ;
78 }
79
80 NetBufferChannel::NetBufferChannel (int in_buffer_size, int out_buffer_size) :
81     in_buffer (in_buffer_size),
82     out_buffer (out_buffer_size),
83     should_close (0)
84 { /* empty */
85 }
86
87 void NetBufferChannel::handleClose ( void )
88 {
89   in_buffer.remove () ;
90   out_buffer.remove () ;
91   should_close = 0 ;
92   NetChannel::handleClose () ;
93 }
94
95
96 bool NetBufferChannel::bufferSend (const char* msg, int msg_len)
97 {
98   if ( out_buffer.append(msg,msg_len) )
99     return true ;
100     
101   SG_LOG(SG_IO, SG_WARN, "NetBufferChannel: output buffer overflow!" ) ;
102   return false ;
103 }
104
105 void NetBufferChannel::handleBufferRead (NetBuffer& buffer)
106 {
107   /* do something here */
108   buffer.remove();
109 }
110   
111 void
112 NetBufferChannel::handleRead (void)
113 {
114   int max_read = in_buffer.getMaxLength() - in_buffer.getLength() ;
115   if (max_read)
116   {
117     char* data = in_buffer.getData() + in_buffer.getLength() ;
118     int num_read = recv (data, max_read) ;
119     if (num_read > 0)
120     {
121       in_buffer.append (num_read) ;
122       //ulSetError ( UL_DEBUG, "netBufferChannel: %d read", num_read ) ;
123     }
124   }
125   if (in_buffer.getLength())
126   {
127     handleBufferRead (in_buffer);
128   }
129 }
130
131 void
132 NetBufferChannel::handleWrite (void)
133 {
134   if (out_buffer.getLength())
135   {
136     if (isConnected())
137     {
138       int length = out_buffer.getLength() ;
139       if (length>512)
140         length=512;
141       int num_sent = NetChannel::send (
142         out_buffer.getData(), length);
143       if (num_sent > 0)
144       {
145         out_buffer.remove (0, num_sent);
146         //ulSetError ( UL_DEBUG, "netBufferChannel: %d sent", num_sent ) ;
147       }
148     }
149   }
150   else if (should_close)
151   {
152     close();
153   }
154 }
155
156 } // of namespace simgear