]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_netBuffer.cxx
Terrasync: implement HTTP service lookup via DNS
[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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, 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 <cassert>
29 #include <cstring>
30
31 #include <simgear/debug/logstream.hxx>
32
33 namespace simgear {
34   
35 NetBuffer::NetBuffer( int _max_length )
36 {
37   length = 0 ;
38   max_length = _max_length ;
39   data = new char [ max_length+1 ] ;  //for null terminator
40 }
41
42 NetBuffer::~NetBuffer ()
43 {
44   delete[] data ;
45 }
46
47 void NetBuffer::remove ()
48 {
49   length = 0 ;
50 }
51
52 void NetBuffer::remove (int pos, int n)
53 {
54   assert (pos>=0 && pos<length && (pos+n)<=length) ;
55   //if (pos>=0 && pos<length && (pos+n)<=length)
56   {
57     memmove(&data[pos],&data[pos+n],length-(pos+n)) ;
58     length -= n ;
59   }
60 }
61
62 bool NetBuffer::append (const char* s, int n)
63 {
64   if ((length+n)<=max_length)
65   {
66     memcpy(&data[length],s,n) ;
67     length += n ;
68     return true ;
69   }
70   return false ;
71 }
72
73 bool NetBuffer::append (int n)
74 {
75   if ((length+n)<=max_length)
76   {
77     length += n ;
78     return true ;
79   }
80   return false ;
81 }
82
83 NetBufferChannel::NetBufferChannel (int in_buffer_size, int out_buffer_size) :
84     in_buffer (in_buffer_size),
85     out_buffer (out_buffer_size),
86     should_close (0)
87 { /* empty */
88 }
89
90 void NetBufferChannel::handleClose ( void )
91 {
92   in_buffer.remove () ;
93   out_buffer.remove () ;
94   should_close = 0 ;
95   NetChannel::handleClose () ;
96 }
97
98
99 bool NetBufferChannel::bufferSend (const char* msg, int msg_len)
100 {
101   if ( out_buffer.append(msg,msg_len) )
102     return true ;
103     
104   SG_LOG(SG_IO, SG_WARN, "NetBufferChannel: output buffer overflow!" ) ;
105   return false ;
106 }
107
108 void NetBufferChannel::handleBufferRead (NetBuffer& buffer)
109 {
110   /* do something here */
111   buffer.remove();
112 }
113   
114 void
115 NetBufferChannel::handleRead (void)
116 {
117   int max_read = in_buffer.getMaxLength() - in_buffer.getLength() ;
118   if (max_read)
119   {
120     char* data = in_buffer.getData() + in_buffer.getLength() ;
121     int num_read = recv (data, max_read) ;
122     if (num_read > 0)
123     {
124       in_buffer.append (num_read) ;
125       //ulSetError ( UL_DEBUG, "netBufferChannel: %d read", num_read ) ;
126     }
127   }
128   if (in_buffer.getLength())
129   {
130     handleBufferRead (in_buffer);
131   }
132 }
133
134 void
135 NetBufferChannel::handleWrite (void)
136 {
137   if (out_buffer.getLength())
138   {
139     if (isConnected())
140     {
141       int length = out_buffer.getLength() ;
142       if (length>512)
143         length=512;
144       int num_sent = NetChannel::send (
145         out_buffer.getData(), length);
146       if (num_sent > 0)
147       {
148         out_buffer.remove (0, num_sent);
149         //ulSetError ( UL_DEBUG, "netBufferChannel: %d sent", num_sent ) ;
150       }
151     }
152   }
153   else if (should_close)
154   {
155     close();
156   }
157 }
158
159 } // of namespace simgear