6 // Written by Curtis Olson, started November 1999.
8 // Copyright (C) 1999 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
27 #ifndef _SG_SOCKET_HXX
28 #define _SG_SOCKET_HXX
32 # error This library requires C++
35 #include <simgear/compiler.h>
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/io/iochannel.hxx>
42 #include <plib/netSocket.h>
46 #define SG_MAX_SOCKET_QUEUE 32
50 * A socket I/O class based on SGIOChannel.
52 class SGSocket : public SGIOChannel {
58 char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
71 // make a server (master listening) socket
72 bool make_server_socket();
74 // make a client socket
75 bool make_client_socket();
77 // Poll for new connections or data to read.
83 * Create an instance of SGSocket.
85 * When calling the constructor you need to provide a host name, a
86 * port number, and a socket style. The convention used by the
87 * SGSocket class is that the server side listens and the client
88 * side sends. For a server socket, the host name should be
89 * empty. For a server, the port number is optional, if you do not
90 * specify a port, the system will assign one. For a client
91 * socket, you need to specify both a destination host and
92 * destination port. For both client and server sockets you must
93 * specify the socket type. Type must be either udp or tcp. Here's
94 * a quick breakdown of the major differences between UDP and TCP
97 * TCP sockets are the type where you establish a connection and
98 * then can read and write to the socket from both ends. If one
99 * end of TCP socket connect quits, the other end will likely
100 * segfault if it doesn't take special precautions. But, the nice
101 * thing about TCP connections is that the underlying protocol
102 * guarantees that your message will get through. This imposes a
103 * certain performance overhead though on the communication
104 * because the protocol must resend failed messages. TCP sockets
105 * are good for sending periodic command/response type messages
106 * where performance isn't a big issues, but reliability is.
108 * UDP sockets on the other hand are a lower level protocol and
109 * don't have the same sort of connection as TCP sockets. With UDP
110 * sockets, the server end just sits and listens for incoming
111 * packets from anywhere. The client end sends it's message and
112 * forgets about it. It doesn't care if there isn't even a server
113 * out there listening and all the packets are getting
114 * lost. Although systems/networks usually do a pretty good job
115 * (statistically) of getting your UDP packets to their
116 * destination, there is no guarantee that any particular packet
117 * will make it. But, because of this low level implementation and
118 * lack of error checking, UDP packets are much faster and
119 * efficient. UDP packets are good for sending positional
120 * information to synchronize two applications. In this case, you
121 * want the information to arrive as quickly as possible, and if
122 * you lose a packet, you'd rather get new updated information
123 * rather than have the system waste time resending a packet that
124 * is becoming older and older with every retry.
125 * @param host name of host if direction is SG_IO_OUT or SG_IO_BI
126 * @param port port number if we care to choose one.
127 * @param style specify "udp" or "tcp"
129 SGSocket( const string& host, const string& port, const string& style );
134 // If specified as a server (in direction for now) open the master
135 // listening socket. If specified as a client (out direction),
136 // open a connection to a server.
137 bool open( const SGProtocolDir d );
139 // read data from socket
140 int read( char *buf, int length );
142 // read data from socket
143 int readline( char *buf, int length );
145 // write data to a socket
146 int write( const char *buf, const int length );
148 // write null terminated string to a socket
149 int writestring( const char *str );
155 * Enable non-blocking mode.
156 * @return success/failure
160 // set timeout (default: 0)
161 inline void set_timeout(int i) { timeout = i; }
163 /** @return the remote host name */
164 inline string get_hostname() const { return hostname; }
166 /** @return the port number (in string form) */
167 inline string get_port_str() const { return port_str; }
171 #endif // _SG_SOCKET_HXX