]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket.cxx
Further HTTP improvements, wget-alike test program to check it all behaves!
[simgear.git] / simgear / io / sg_socket.cxx
1 // sg_socket.cxx -- Socket I/O routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 // Modified by Bernie Bright <bbright@bigpond.net.au>, May 2002.
5 //
6 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24 #ifdef HAVE_CONFIG_H
25 #  include <simgear_config.h>
26 #endif
27
28 #include <simgear/compiler.h>
29
30 #include <iostream>
31 #include <cstring>
32 #include <cstdlib> // for atoi
33
34 #include <simgear/debug/logstream.hxx>
35
36 #include "sg_socket.hxx"
37
38 bool SGSocket::init = false;
39 using std::string;
40
41 SGSocket::SGSocket( const string& host, const string& port_, 
42                     const string& style ) :
43     hostname(host),
44     port_str(port_),
45     save_len(0),
46     client(0),
47     is_tcp(false),
48     is_server(false),
49     first_read(false),
50     timeout(0)
51 {
52     if (!init)
53     {
54       simgear::Socket::initSockets();
55       init = true;
56     }
57
58     if ( style == "tcp" )
59     {
60         is_tcp = true;
61     }
62     else if ( style != "udp" )
63     {
64         SG_LOG( SG_IO, SG_ALERT,
65                 "Error: SGSocket() unknown style = " << style );
66     }
67
68     set_type( sgSocketType );
69 }
70
71
72 SGSocket::~SGSocket()
73 {
74     this->close();
75 }
76
77
78 bool
79 SGSocket::make_server_socket()
80 {
81     if (!sock.open( is_tcp ))
82     {
83         SG_LOG( SG_IO, SG_ALERT, 
84                 "Error: socket() failed in make_server_socket()" );
85         return false;
86     }
87
88     if (sock.bind( hostname.c_str(), port ) < 0)
89     {
90         SG_LOG( SG_IO, SG_ALERT,
91                 "Error: bind() failed in make_server_socket()" );
92         sock.close();
93         return false;
94     }
95
96     return true;
97 }
98
99
100 bool
101 SGSocket::make_client_socket()
102 {
103     if (!sock.open( is_tcp ))
104     {
105         SG_LOG( SG_IO, SG_ALERT, 
106                 "Error: socket() failed in make_client_socket()" );
107         return false;
108     }
109
110     if (sock.connect( hostname.c_str(), port ) < 0)
111     {
112         SG_LOG( SG_IO, SG_ALERT, 
113                 "Error: connect() failed in make_client_socket()" );
114         sock.close();
115         return false;
116     }
117
118     return true;
119 }
120
121 // If specified as a server (in direction for now) open the master
122 // listening socket.  If specified as a client (out direction), open a
123 // connection to a server.
124 bool
125 SGSocket::open( SGProtocolDir direction )
126 {
127     set_dir( direction );
128
129     is_server = is_tcp &&
130         (direction == SG_IO_IN || direction == SG_IO_BI);
131
132     if ( port_str == "" || port_str == "any" ) {
133         port = 0; 
134     } else {
135         port = atoi( port_str.c_str() );
136     }
137     
138     if (direction == SG_IO_IN)
139     {
140         // this means server for now
141
142         // Setup socket to listen on.  Set "port" before making this
143         // call.  A port of "0" indicates that we want to let the os
144         // pick any available port.
145         if (!make_server_socket())
146         {
147             SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
148             return false;
149         }
150
151         if ( !is_tcp )
152         {
153             // Non-blocking UDP
154             nonblock();
155         }
156         else
157         {
158             // Blocking TCP
159             // Specify the maximum length of the connection queue
160             sock.listen( SG_MAX_SOCKET_QUEUE );
161         }
162
163     }
164     else if (direction == SG_IO_OUT)
165     {
166         // this means client for now
167
168         if (!make_client_socket())
169         {
170             SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
171             return false;
172         }
173
174         if ( !is_tcp )
175         {
176             // Non-blocking UDP
177             nonblock();
178         }
179     }
180     else if (direction == SG_IO_BI && is_tcp)
181     {
182         // this means server for TCP sockets
183
184         // Setup socket to listen on.  Set "port" before making this
185         // call.  A port of "0" indicates that we want to let the os
186         // pick any available port.
187         if (!make_server_socket())
188         {
189             SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
190             return false;
191         }
192         // Blocking TCP
193         // Specify the maximum length of the connection queue
194         sock.listen( SG_MAX_SOCKET_QUEUE );
195     }
196     else
197     {
198         SG_LOG( SG_IO, SG_ALERT, 
199                 "Error:  bidirection mode not available for UDP sockets." );
200         return false;
201     }
202
203     first_read = false;
204
205     return true;
206 }
207
208
209 // read data from socket (server)
210 // read a block of data of specified size
211 int
212 SGSocket::read( char *buf, int length )
213 {
214     if (sock.getHandle() == -1 &&
215         (client == 0 || client->getHandle() == -1))
216     {
217         return 0;
218     }
219
220     // test for any input available on sock (returning immediately, even if
221     // nothing)
222     int result = poll();
223
224     if (result > 0)
225     {
226        if (is_tcp && is_server)
227         {
228             result = client->recv( buf, length );
229         }
230         else
231         {
232             result = sock.recv( buf, length );
233         }
234
235         if ( result != length )
236         {
237             SG_LOG( SG_IO, SG_INFO, 
238                     "Warning: read() not enough bytes." );
239         }
240     }
241
242     return result;
243 }
244
245
246 // read a line of data, length is max size of input buffer
247 int
248 SGSocket::readline( char *buf, int length )
249 {
250     if (sock.getHandle() == -1 &&
251         (client == 0 || client->getHandle() == -1))
252     {
253         return 0;
254     }
255
256     // test for any input read on sock (returning immediately, even if
257     // nothing)
258     int result = this->poll();
259
260     if (result > 0)
261     {
262         // read a chunk, keep in the save buffer until we have the
263         // requested amount read
264
265        if (is_tcp && is_server)
266         {
267             char *buf_ptr = save_buf + save_len;
268             result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
269                 
270             if ( result > 0 )
271             {
272                 first_read = true;
273             }
274
275             save_len += result;
276
277             // Try and detect that the remote end died.  This
278             // could cause problems so if you see connections
279             // dropping for unexplained reasons, LOOK HERE!
280             if (result == 0 && save_len == 0 && first_read == true)
281             {
282                 SG_LOG( SG_IO, SG_ALERT, 
283                         "Connection closed by foreign host." );
284                 delete client;
285                 client = 0;
286             }
287         }
288         else
289         {
290             char *buf_ptr = save_buf + save_len;
291             result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
292             save_len += result;
293         }
294     }
295
296     // look for the end of line in save_buf
297     int i;
298     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
299         ;
300     if (( i < save_len ) && ( save_buf[i] == '\n' )) {
301         result = i + 1;
302     } else {
303         // no end of line yet
304         return 0;
305     }
306
307     // we found an end of line
308
309     // check buffer size
310     int copy_length = result;
311     if (copy_length >= length) {
312         SG_LOG( SG_IO, SG_ALERT, 
313                 "Alert: readline() has line exceeding the buffer size." );
314         copy_length = length-1;
315     }
316     // copy to external buffer
317     strncpy( buf, save_buf, copy_length );
318     buf[copy_length] = '\0';
319
320     // shift save buffer
321     //memmove( save_buf+, save_buf+, ? );
322     for ( i = result; i < save_len; ++i ) {
323         save_buf[ i - result ] = save_buf[i];
324     }
325     save_len -= result;
326
327     return result;
328 }
329
330
331 // write data to socket (client)
332 int
333 SGSocket::write( const char *buf, const int length )
334 {
335     simgear::Socket* s = client == 0 ? &sock : client;
336     if (s->getHandle() == -1)
337     {
338         return 0;
339     }
340
341     bool error_condition = false;
342
343     if ( s->send( buf, length ) < 0 )
344     {
345         SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
346         error_condition = true;
347     }
348
349     if ( error_condition ) {
350         return 0;
351     }
352
353     return length;
354 }
355
356
357 // write null terminated string to socket (server)
358 int
359 SGSocket::writestring( const char *str )
360 {
361     int length = strlen( str );
362     return this->write( str, length );
363 }
364
365
366 // close the port
367 bool
368 SGSocket::close()
369 {
370     delete client;
371     client = 0;
372
373     sock.close();
374     return true;
375 }
376
377
378 // configure the socket as non-blocking
379 bool
380 SGSocket::nonblock()
381 {
382     if (sock.getHandle() == -1) {
383         return false;
384     }
385
386     sock.setBlocking( false );
387     return true;
388 }
389
390 int
391 SGSocket::poll()
392 {
393     simgear::Socket* readers[2];
394
395     readers[0] = client != 0 ? client : &sock;
396     readers[1] = 0;
397
398     simgear::Socket* writers[1];
399     writers[0] = 0;
400
401     int result = simgear::Socket::select( readers, writers, timeout );
402
403     if (result > 0 && is_server && client == 0)
404     {
405         // Accept a new client connection
406         simgear::IPAddress addr;
407         int new_fd = sock.accept( &addr );
408         SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
409                 << addr.getHost() << ":" << addr.getPort() );
410         client = new simgear::Socket();
411         client->setHandle( new_fd );
412         return 0;
413     }
414
415     return result;
416 }