]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket_udp.cxx
Drop explicit SDK setting on Mac
[simgear.git] / simgear / io / sg_socket_udp.cxx
1 // sg_socket.cxx -- Socket I/O routines
2 //
3 // Written by Curtis Olson, started November 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include <simgear_config.h>
25 #endif
26
27 #include <simgear/compiler.h>
28
29 #include <simgear/debug/logstream.hxx>
30
31 #include "sg_socket_udp.hxx"
32
33 #include <cstring>
34 #include <cstdlib> // for atoi
35 #include <algorithm>
36
37 using std::string;
38
39 SGSocketUDP::SGSocketUDP( const string& host, const string& port ) :
40     hostname(host),
41     port_str(port),
42     save_len(0)
43 {
44     set_valid( false );
45 }
46
47
48 SGSocketUDP::~SGSocketUDP() {
49 }
50
51
52 // If specified as a server (in direction for now) open the master
53 // listening socket.  If specified as a client (out direction), open a
54 // connection to a server.
55 bool SGSocketUDP::open( const SGProtocolDir d ) {
56     set_dir( d );
57
58     if ( ! sock.open( false ) ) {       // open a UDP socket
59         SG_LOG( SG_IO, SG_ALERT, "error opening socket" );
60         return false;
61     }
62
63     if ( port_str == "" || port_str == "any" ) {
64         port = 0; 
65     } else {
66         port = atoi( port_str.c_str() );
67     }
68     
69     // client_connections.clear();
70
71     if ( get_dir() == SG_IO_IN ) {
72         // this means server
73
74         // bind ...
75         if ( sock.bind( hostname.c_str(), port ) == -1 ) {
76             SG_LOG( SG_IO, SG_ALERT, "error binding to port" << port_str );
77             return false;
78         }
79     } else if ( get_dir() == SG_IO_OUT ) {
80         // this means client
81
82         // connect ...
83         if ( sock.connect( hostname.c_str(), port ) == -1 ) {
84             SG_LOG( SG_IO, SG_ALERT,
85                     "error connecting to " << hostname << port_str );
86             return false;
87         }
88     } else {
89         SG_LOG( SG_IO, SG_ALERT, 
90                 "Error:  bidirection mode not available for UDP sockets." );
91         return false;
92     }
93
94     set_valid( true );
95
96     return true;
97 }
98
99
100 // read data from socket (server)
101 // read a block of data of specified size
102 int SGSocketUDP::read( char *buf, int length ) {
103     if ( ! isvalid() ) {
104         return 0;
105     }
106
107     if (length <= 0) {
108         return 0;
109     }
110     int result;
111     // prevent buffer overflow
112     int maxsize = std::min(length - 1, SG_IO_MAX_MSG_SIZE);
113
114     if ( (result = sock.recv(buf, maxsize, 0)) >= 0 ) {
115         buf[result] = '\0';
116         // printf("msg received = %s\n", buf);
117     }
118
119     return result;
120 }
121
122
123 // read a line of data, length is max size of input buffer
124 int SGSocketUDP::readline( char *buf, int length ) {
125     if ( ! isvalid() ) {
126         return 0;
127     }
128
129     if (length <= 0) {
130         return 0;
131     }
132     // cout << "sock = " << sock << endl;
133
134     char *buf_ptr = save_buf + save_len;
135     // prevent buffer overflow (size of save_buf is 2 * SG_IO_MAX_MSG_SIZE)
136     int maxsize = save_len < SG_IO_MAX_MSG_SIZE ?
137     SG_IO_MAX_MSG_SIZE : 2 * SG_IO_MAX_MSG_SIZE - save_len;
138     int result = sock.recv(buf_ptr, maxsize, 0);
139     // printf("msg received = %s\n", buf);
140     save_len += result;
141
142     // look for the end of line in save_buf
143     int i;
144     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
145     if ( save_buf[i] == '\n' ) {
146         result = i + 1;
147     } else {
148         // no end of line yet
149         // cout << "no eol found" << endl;
150         return 0;
151     }
152     // cout << "line length = " << result << endl;
153
154     // we found an end of line
155
156     // copy to external buffer
157     // prevent buffer overflow
158     result = std::min(result,length - 1);
159     strncpy( buf, save_buf, result );
160     buf[result] = '\0';
161     // cout << "sg_socket line = " << buf << endl;
162     
163     // shift save buffer
164     for ( i = result; i < save_len; ++i ) {
165         save_buf[ i - result ] = save_buf[i];
166     }
167     save_len -= result;
168
169     return result;
170 }
171
172
173 // write data to socket (client)
174 int SGSocketUDP::write( const char *buf, const int length ) {
175     if ( ! isvalid() ) {
176         return 0;
177     }
178
179     if ( sock.send( buf, length, 0 ) < 0 ) {
180         SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
181         return 0;
182     }
183
184     return length;
185 }
186
187
188 // write null terminated string to socket (server)
189 int SGSocketUDP::writestring( const char *str ) {
190     if ( !isvalid() ) {
191         return 0;
192     }
193
194     int length = strlen( str );
195     return write( str, length );
196 }
197
198
199 // close the port
200 bool SGSocketUDP::close() {
201     if ( !isvalid() ) {
202         return 0;
203     }
204
205     sock.close();
206
207     return true;
208 }
209
210
211 // configure the socket as non-blocking
212 bool SGSocketUDP::setBlocking( bool value ) {
213     sock.setBlocking( value );
214
215     return true;
216 }