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