]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket_udp.hxx
Introduce SGBinaryFile
[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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23 //
24 // $Id$
25
26
27 #ifndef _SG_SOCKET_UDP_HXX
28 #define _SG_SOCKET_UDP_HXX
29
30
31 #include <simgear/compiler.h>
32
33 #include <string>
34
35 #include <simgear/math/sg_types.hxx>
36 #include <simgear/io/iochannel.hxx>
37 #include <simgear/io/raw_socket.hxx>
38
39 /**
40  * A UDP socket I/O class based on SGIOChannel and plib/net.
41  */
42 class SGSocketUDP : public SGIOChannel {
43
44 private:
45
46     simgear::Socket sock;
47
48     std::string hostname;
49     std::string port_str;
50
51     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
52     int save_len;
53
54     short unsigned int port;
55
56 public:
57
58     /**
59      * Create an instance of SGSocketUDP.
60      *
61      * When calling the constructor you need to provide a host name, and a
62      * port number. The convention used by the
63      * SGSocketUDP class is that the server side listens and the client
64      * side sends. For a server socket, the host name should be
65      * empty. For a server, the port number is optional, if you do not
66      * specify a port, the system will assign one. For a client
67      * socket, you need to specify both a destination host and
68      * destination port.
69      *
70      * UDP sockets are a lower level protocol than TCP sockets and are
71      * "connectionless" in the sense that either client or server can
72      * exist, or not exist, startup, quit, etc. in any order and
73      * whenever both ends are alive, the communication succeeds. With
74      * UDP sockets, the server end just sits and listens for incoming
75      * packets from anywhere. The client end sends it's message and
76      * forgets about it. It doesn't care if there isn't even a server
77      * out there listening and all the packets are getting
78      * lost. Although systems/networks usually do a pretty good job
79      * (statistically) of getting your UDP packets to their
80      * destination, there is no guarantee that any particular packet
81      * will make it. But, because of this low level implementation and
82      * lack of error checking, UDP packets are much faster and
83      * efficient. UDP packets are good for sending positional
84      * information to synchronize two applications. In this case, you
85      * want the information to arrive as quickly as possible, and if
86      * you lose a packet, you'd rather get new updated information
87      * rather than have the system waste time resending a packet that
88      * is becoming older and older with every retry.
89      * @param host name of host if direction is SG_IO_OUT or SG_IO_BI
90      * @param port port number if we care to choose one.
91      * @param style specify "udp" or "tcp" */
92     SGSocketUDP( const std::string& host, const std::string& port );
93
94     /** Destructor */
95     ~SGSocketUDP();
96
97     // If specified as a server (in direction for now) open the master
98     // listening socket.  If specified as a client (out direction),
99     // open a connection to a server.
100     bool open( const SGProtocolDir d );
101
102     // read data from socket
103     int read( char *buf, int length );
104
105     // read data from socket
106     int readline( char *buf, int length );
107
108     // write data to a socket
109     int write( const char *buf, const int length );
110
111     // write null terminated string to a socket
112     int writestring( const char *str );
113
114     // close file
115     bool close();
116
117     /**
118      * Set blocking true or false
119      * @return success/failure
120      */
121     bool setBlocking( bool value );
122
123     /** @return the remote host name */
124     inline std::string get_hostname() const { return hostname; }
125
126     /** @return the port number (in string form) */
127     inline std::string get_port_str() const { return port_str; }
128 };
129
130
131 #endif // _SG_SOCKET_UDP_HXX