]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket_udp.cxx
SGPath: fix creating paths with permission checker.
[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
36 using std::string;
37
38 SGSocketUDP::SGSocketUDP( const string& host, const string& port ) :
39     hostname(host),
40     port_str(port),
41     save_len(0)
42 {
43     set_valid( false );
44 }
45
46
47 SGSocketUDP::~SGSocketUDP() {
48 }
49
50
51 // If specified as a server (in direction for now) open the master
52 // listening socket.  If specified as a client (out direction), open a
53 // connection to a server.
54 bool SGSocketUDP::open( const SGProtocolDir d ) {
55     set_dir( d );
56
57     if ( ! sock.open( false ) ) {       // open a UDP socket
58         SG_LOG( SG_IO, SG_ALERT, "error opening socket" );
59         return false;
60     }
61
62     if ( port_str == "" || port_str == "any" ) {
63         port = 0; 
64     } else {
65         port = atoi( port_str.c_str() );
66     }
67     
68     // client_connections.clear();
69
70     if ( get_dir() == SG_IO_IN ) {
71         // this means server
72
73         // bind ...
74         if ( sock.bind( hostname.c_str(), port ) == -1 ) {
75             SG_LOG( SG_IO, SG_ALERT, "error binding to port" << port_str );
76             return false;
77         }
78     } else if ( get_dir() == SG_IO_OUT ) {
79         // this means client
80
81         // connect ...
82         if ( sock.connect( hostname.c_str(), port ) == -1 ) {
83             SG_LOG( SG_IO, SG_ALERT,
84                     "error connecting to " << hostname << port_str );
85             return false;
86         }
87     } else {
88         SG_LOG( SG_IO, SG_ALERT, 
89                 "Error:  bidirection mode not available for UDP sockets." );
90         return false;
91     }
92
93     set_valid( true );
94
95     return true;
96 }
97
98
99 // read data from socket (server)
100 // read a block of data of specified size
101 int SGSocketUDP::read( char *buf, int length ) {
102     if ( ! isvalid() ) {
103         return 0;
104     }
105
106     if (length <= 0) {
107         return 0;
108     }
109     int result;
110     // prevent buffer overflow
111     int maxsize = std::min(length - 1, SG_IO_MAX_MSG_SIZE);
112
113     if ( (result = sock.recv(buf, maxsize, 0)) >= 0 ) {
114         buf[result] = '\0';
115         // printf("msg received = %s\n", buf);
116     }
117
118     return result;
119 }
120
121
122 // read a line of data, length is max size of input buffer
123 int SGSocketUDP::readline( char *buf, int length ) {
124     if ( ! isvalid() ) {
125         return 0;
126     }
127
128     if (length <= 0) {
129         return 0;
130     }
131     // cout << "sock = " << sock << endl;
132
133     char *buf_ptr = save_buf + save_len;
134     // prevent buffer overflow (size of save_buf is 2 * SG_IO_MAX_MSG_SIZE)
135     int maxsize = save_len < SG_IO_MAX_MSG_SIZE ?
136     SG_IO_MAX_MSG_SIZE : 2 * SG_IO_MAX_MSG_SIZE - save_len;
137     int result = sock.recv(buf_ptr, maxsize, 0);
138     // printf("msg received = %s\n", buf);
139     save_len += result;
140
141     // look for the end of line in save_buf
142     int i;
143     for ( i = 0; i < save_len && save_buf[i] != '\n'; ++i );
144     if ( save_buf[i] == '\n' ) {
145         result = i + 1;
146     } else {
147         // no end of line yet
148         // cout << "no eol found" << endl;
149         return 0;
150     }
151     // cout << "line length = " << result << endl;
152
153     // we found an end of line
154
155     // copy to external buffer
156     // prevent buffer overflow
157     result = std::min(result,length - 1);
158     strncpy( buf, save_buf, result );
159     buf[result] = '\0';
160     // cout << "sg_socket line = " << buf << endl;
161     
162     // shift save buffer
163     for ( i = result; i < save_len; ++i ) {
164         save_buf[ i - result ] = save_buf[i];
165     }
166     save_len -= result;
167
168     return result;
169 }
170
171
172 // write data to socket (client)
173 int SGSocketUDP::write( const char *buf, const int length ) {
174     if ( ! isvalid() ) {
175         return 0;
176     }
177
178     if ( sock.send( buf, length, 0 ) < 0 ) {
179         SG_LOG( SG_IO, SG_WARN, "Error writing to socket: " << port );
180         return 0;
181     }
182
183     return length;
184 }
185
186
187 // write null terminated string to socket (server)
188 int SGSocketUDP::writestring( const char *str ) {
189     if ( !isvalid() ) {
190         return 0;
191     }
192
193     int length = strlen( str );
194     return write( str, length );
195 }
196
197
198 // close the port
199 bool SGSocketUDP::close() {
200     if ( !isvalid() ) {
201         return 0;
202     }
203
204     sock.close();
205
206     return true;
207 }
208
209
210 // configure the socket as non-blocking
211 bool SGSocketUDP::setBlocking( bool value ) {
212     sock.setBlocking( value );
213
214     return true;
215 }