]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket.cxx
Merge branch 'next' of git@gitorious.org:fg/simgear into next
[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
40 SGSocket::SGSocket( const string& host, const string& port_, 
41                     const string& style ) :
42     hostname(host),
43     port_str(port_),
44     save_len(0),
45     client(0),
46     is_tcp(false),
47     is_server(false),
48     first_read(false),
49     timeout(0)
50 {
51     if (!init)
52     {
53       simgear::Socket::initSockets();
54       init = true;
55     }
56
57     if ( style == "tcp" )
58     {
59         is_tcp = true;
60     }
61     else if ( style != "udp" )
62     {
63         SG_LOG( SG_IO, SG_ALERT,
64                 "Error: SGSocket() unknown style = " << style );
65     }
66
67     set_type( sgSocketType );
68 }
69
70
71 SGSocket::~SGSocket()
72 {
73     this->close();
74 }
75
76
77 bool
78 SGSocket::make_server_socket()
79 {
80     if (!sock.open( is_tcp ))
81     {
82         SG_LOG( SG_IO, SG_ALERT, 
83                 "Error: socket() failed in make_server_socket()" );
84         return false;
85     }
86
87     if (sock.bind( "", port ) < 0)
88     {
89         SG_LOG( SG_IO, SG_ALERT,
90                 "Error: bind() failed in make_server_socket()" );
91         sock.close();
92         return false;
93     }
94
95     return true;
96 }
97
98
99 bool
100 SGSocket::make_client_socket()
101 {
102     if (!sock.open( is_tcp ))
103     {
104         SG_LOG( SG_IO, SG_ALERT, 
105                 "Error: socket() failed in make_client_socket()" );
106         return false;
107     }
108
109     if (sock.connect( hostname.c_str(), port ) < 0)
110     {
111         SG_LOG( SG_IO, SG_ALERT, 
112                 "Error: connect() failed in make_client_socket()" );
113         sock.close();
114         return false;
115     }
116
117     return true;
118 }
119
120 // If specified as a server (in direction for now) open the master
121 // listening socket.  If specified as a client (out direction), open a
122 // connection to a server.
123 bool
124 SGSocket::open( SGProtocolDir direction )
125 {
126     set_dir( direction );
127
128     is_server = is_tcp &&
129         (direction == SG_IO_IN || direction == SG_IO_BI);
130
131     if ( port_str == "" || port_str == "any" ) {
132         port = 0; 
133     } else {
134         port = atoi( port_str.c_str() );
135     }
136     
137     if (direction == SG_IO_IN)
138     {
139         // this means server for now
140
141         // Setup socket to listen on.  Set "port" before making this
142         // call.  A port of "0" indicates that we want to let the os
143         // pick any available port.
144         if (!make_server_socket())
145         {
146             SG_LOG( SG_IO, SG_ALERT, "SG_IO_IN socket creation failed" );
147             return false;
148         }
149
150         if ( !is_tcp )
151         {
152             // Non-blocking UDP
153             nonblock();
154         }
155         else
156         {
157             // Blocking TCP
158             // Specify the maximum length of the connection queue
159             sock.listen( SG_MAX_SOCKET_QUEUE );
160         }
161
162     }
163     else if (direction == SG_IO_OUT)
164     {
165         // this means client for now
166
167         if (!make_client_socket())
168         {
169             SG_LOG( SG_IO, SG_ALERT, "SG_IO_OUT socket creation failed" );
170             return false;
171         }
172
173         if ( !is_tcp )
174         {
175             // Non-blocking UDP
176             nonblock();
177         }
178     }
179     else if (direction == SG_IO_BI && is_tcp)
180     {
181         // this means server for TCP sockets
182
183         // Setup socket to listen on.  Set "port" before making this
184         // call.  A port of "0" indicates that we want to let the os
185         // pick any available port.
186         if (!make_server_socket())
187         {
188             SG_LOG( SG_IO, SG_ALERT, "SG_IO_BI socket creation failed" );
189             return false;
190         }
191         // Blocking TCP
192         // Specify the maximum length of the connection queue
193         sock.listen( SG_MAX_SOCKET_QUEUE );
194     }
195     else
196     {
197         SG_LOG( SG_IO, SG_ALERT, 
198                 "Error:  bidirection mode not available for UDP sockets." );
199         return false;
200     }
201
202     first_read = false;
203
204     return true;
205 }
206
207
208 // read data from socket (server)
209 // read a block of data of specified size
210 int
211 SGSocket::read( char *buf, int length )
212 {
213     if (sock.getHandle() == -1 &&
214         (client == 0 || client->getHandle() == -1))
215     {
216         return 0;
217     }
218
219     // test for any input available on sock (returning immediately, even if
220     // nothing)
221     int result = poll();
222
223     if (result > 0)
224     {
225        if (is_tcp && is_server)
226         {
227             result = client->recv( buf, length );
228         }
229         else
230         {
231             result = sock.recv( buf, length );
232         }
233
234         if ( result != length )
235         {
236             SG_LOG( SG_IO, SG_INFO, 
237                     "Warning: read() not enough bytes." );
238         }
239     }
240
241     return result;
242 }
243
244
245 // read a line of data, length is max size of input buffer
246 int
247 SGSocket::readline( char *buf, int length )
248 {
249     if (sock.getHandle() == -1 &&
250         (client == 0 || client->getHandle() == -1))
251     {
252         return 0;
253     }
254
255     // test for any input read on sock (returning immediately, even if
256     // nothing)
257     int result = this->poll();
258
259     if (result > 0)
260     {
261         // read a chunk, keep in the save buffer until we have the
262         // requested amount read
263
264        if (is_tcp && is_server)
265         {
266             char *buf_ptr = save_buf + save_len;
267             result = client->recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
268                 
269             if ( result > 0 )
270             {
271                 first_read = true;
272             }
273
274             save_len += result;
275
276             // Try and detect that the remote end died.  This
277             // could cause problems so if you see connections
278             // dropping for unexplained reasons, LOOK HERE!
279             if (result == 0 && save_len == 0 && first_read == true)
280             {
281                 SG_LOG( SG_IO, SG_ALERT, 
282                         "Connection closed by foreign host." );
283                 delete client;
284                 client = 0;
285             }
286         }
287         else
288         {
289             char *buf_ptr = save_buf + save_len;
290             result = sock.recv( buf_ptr, SG_IO_MAX_MSG_SIZE - save_len );
291             save_len += result;
292         }
293     }
294
295     // look for the end of line in save_buf
296     int i;
297     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i )
298         ;
299     if ( save_buf[i] == '\n' ) {
300         result = i + 1;
301     } else {
302         // no end of line yet
303         return 0;
304     }
305
306     // we found an end of line
307
308     // copy to external buffer
309     strncpy( buf, save_buf, result );
310     buf[result] = '\0';
311
312     // shift save buffer
313     //memmove( save_buf+, save_buf+, ? );
314     for ( i = result; i < save_len; ++i ) {
315         save_buf[ i - result ] = save_buf[i];
316     }
317     save_len -= result;
318
319     return result;
320 }
321
322
323 // write data to socket (client)
324 int
325 SGSocket::write( const char *buf, const int length )
326 {
327     simgear::Socket* s = client == 0 ? &sock : client;
328     if (s->getHandle() == -1)
329     {
330         return 0;
331     }
332
333     bool error_condition = false;
334
335     if ( s->send( buf, length ) < 0 )
336     {
337         SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
338         error_condition = true;
339     }
340
341     if ( error_condition ) {
342         return 0;
343     }
344
345     return length;
346 }
347
348
349 // write null terminated string to socket (server)
350 int
351 SGSocket::writestring( const char *str )
352 {
353     int length = strlen( str );
354     return this->write( str, length );
355 }
356
357
358 // close the port
359 bool
360 SGSocket::close()
361 {
362     delete client;
363     client = 0;
364
365     sock.close();
366     return true;
367 }
368
369
370 // configure the socket as non-blocking
371 bool
372 SGSocket::nonblock()
373 {
374     if (sock.getHandle() == -1) {
375         return false;
376     }
377
378     sock.setBlocking( false );
379     return true;
380 }
381
382 int
383 SGSocket::poll()
384 {
385     simgear::Socket* readers[2];
386
387     readers[0] = client != 0 ? client : &sock;
388     readers[1] = 0;
389
390     simgear::Socket* writers[1];
391     writers[0] = 0;
392
393     int result = simgear::Socket::select( readers, writers, timeout );
394
395     if (result > 0 && is_server && client == 0)
396     {
397         // Accept a new client connection
398         simgear::IPAddress addr;
399         int new_fd = sock.accept( &addr );
400         SG_LOG( SG_IO, SG_INFO, "Accepted connection from "
401                 << addr.getHost() << ":" << addr.getPort() );
402         client = new simgear::Socket();
403         client->setHandle( new_fd );
404         return 0;
405     }
406
407     return result;
408 }