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