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