]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket_udp.hxx
Back out the previous patch.
[simgear.git] / simgear / io / sg_socket_udp.hxx
1 /**
2  * \file sg_socket_udp.hxx
3  * UDP Socket I/O routines.
4  */
5
6 // Written by Curtis Olson, started November 2001.
7 //
8 // Copyright (C) 2001  Curtis L. Olson - http://www.flightgear.org/~curt
9 //
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
14 //
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 // General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_SOCKET_UDP_HXX
28 #define _SG_SOCKET_UDP_HXX
29
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <plib/netSocket.h>
36
37 #include <simgear/compiler.h>
38
39 #include STL_STRING
40
41 #include <simgear/math/sg_types.hxx>
42 #include <simgear/io/iochannel.hxx>
43
44 SG_USING_STD(string);
45
46 /**
47  * A UDP socket I/O class based on SGIOChannel and plib/net.
48  */
49 class SGSocketUDP : public SGIOChannel {
50
51 private:
52
53     netSocket sock;
54
55     string hostname;
56     string port_str;
57
58     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
59     int save_len;
60
61     short unsigned int port;
62
63 public:
64
65     /**
66      * Create an instance of SGSocketUDP.
67      *
68      * When calling the constructor you need to provide a host name, and a
69      * port number. The convention used by the
70      * SGSocketUDP class is that the server side listens and the client
71      * side sends. For a server socket, the host name should be
72      * empty. For a server, the port number is optional, if you do not
73      * specify a port, the system will assign one. For a client
74      * socket, you need to specify both a destination host and
75      * destination port.
76      *
77      * UDP sockets are a lower level protocol than TCP sockets and are
78      * "connectionless" in the sense that either client or server can
79      * exist, or not exist, startup, quit, etc. in any order and
80      * whenever both ends are alive, the communication succeeds. With
81      * UDP sockets, the server end just sits and listens for incoming
82      * packets from anywhere. The client end sends it's message and
83      * forgets about it. It doesn't care if there isn't even a server
84      * out there listening and all the packets are getting
85      * lost. Although systems/networks usually do a pretty good job
86      * (statistically) of getting your UDP packets to their
87      * destination, there is no guarantee that any particular packet
88      * will make it. But, because of this low level implementation and
89      * lack of error checking, UDP packets are much faster and
90      * efficient. UDP packets are good for sending positional
91      * information to synchronize two applications. In this case, you
92      * want the information to arrive as quickly as possible, and if
93      * you lose a packet, you'd rather get new updated information
94      * rather than have the system waste time resending a packet that
95      * is becoming older and older with every retry.
96      * @param host name of host if direction is SG_IO_OUT or SG_IO_BI
97      * @param port port number if we care to choose one.
98      * @param style specify "udp" or "tcp" */
99     SGSocketUDP( const string& host, const string& port );
100
101     /** Destructor */
102     ~SGSocketUDP();
103
104     // If specified as a server (in direction for now) open the master
105     // listening socket.  If specified as a client (out direction),
106     // open a connection to a server.
107     bool open( const SGProtocolDir d );
108
109     // read data from socket
110     int read( char *buf, int length );
111
112     // read data from socket
113     int readline( char *buf, int length );
114
115     // write data to a socket
116     int write( const char *buf, const int length );
117
118     // write null terminated string to a socket
119     int writestring( const char *str );
120
121     // close file
122     bool close();
123
124     /**
125      * Set blocking true or false
126      * @return success/failure
127      */
128     bool setBlocking( bool value );
129
130     /** @return the remote host name */
131     inline string get_hostname() const { return hostname; }
132
133     /** @return the port number (in string form) */
134     inline string get_port_str() const { return port_str; }
135 };
136
137
138 #endif // _SG_SOCKET_UDP_HXX