]> git.mxchange.org Git - simgear.git/blob - simgear/io/sg_socket.hxx
Bernie Bright:
[simgear.git] / simgear / io / sg_socket.hxx
1 /**
2  * \file sg_socket.hxx
3  * Socket I/O routines.
4  */
5
6 // Written by Curtis Olson, started November 1999.
7 //
8 // Copyright (C) 1999  Curtis L. Olson - curt@flightgear.org
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_HXX
28 #define _SG_SOCKET_HXX
29
30
31 #ifndef __cplusplus
32 # error This library requires C++
33 #endif
34
35 #include <simgear/compiler.h>
36
37 #include STL_STRING
38
39 #include <simgear/math/sg_types.hxx>
40 #include <simgear/io/iochannel.hxx>
41
42 #include <plib/netSocket.h>
43
44 SG_USING_STD(string);
45
46 #define SG_MAX_SOCKET_QUEUE 32
47
48
49 /**
50  * A socket I/O class based on SGIOChannel.
51  */
52 class SGSocket : public SGIOChannel {
53 public:
54 private:
55     string hostname;
56     string port_str;
57
58     char save_buf[ 2 * SG_IO_MAX_MSG_SIZE ];
59     int save_len;
60
61     netSocket sock;
62     netSocket* client;
63     unsigned short port;
64     bool is_tcp;
65     bool is_server;
66     bool first_read;
67
68     static bool init;
69
70     // make a server (master listening) socket
71     bool make_server_socket();
72
73     // make a client socket
74     bool  make_client_socket();
75
76     // Poll for new connections or data to read.
77     int poll();
78
79 public:
80
81     /**
82      * Create an instance of SGSocket.
83      *
84      * When calling the constructor you need to provide a host name, a
85      * port number, and a socket style. The convention used by the
86      * SGSocket class is that the server side listens and the client
87      * side sends. For a server socket, the host name should be
88      * empty. For a server, the port number is optional, if you do not
89      * specify a port, the system will assign one. For a client
90      * socket, you need to specify both a destination host and
91      * destination port. For both client and server sockets you must
92      * specify the socket type. Type must be either udp or tcp. Here's
93      * a quick breakdown of the major differences between UDP and TCP
94      * type sockets.
95      *
96      * TCP sockets are the type where you establish a connection and
97      * then can read and write to the socket from both ends. If one
98      * end of TCP socket connect quits, the other end will likely
99      * segfault if it doesn't take special precautions.  But, the nice
100      * thing about TCP connections is that the underlying protocol
101      * guarantees that your message will get through. This imposes a
102      * certain performance overhead though on the communication
103      * because the protocol must resend failed messages. TCP sockets
104      * are good for sending periodic command/response type messages
105      * where performance isn't a big issues, but reliability is.
106      *
107      * UDP sockets on the other hand are a lower level protocol and
108      * don't have the same sort of connection as TCP sockets. With UDP
109      * sockets, the server end just sits and listens for incoming
110      * packets from anywhere. The client end sends it's message and
111      * forgets about it. It doesn't care if there isn't even a server
112      * out there listening and all the packets are getting
113      * lost. Although systems/networks usually do a pretty good job
114      * (statistically) of getting your UDP packets to their
115      * destination, there is no guarantee that any particular packet
116      * will make it. But, because of this low level implementation and
117      * lack of error checking, UDP packets are much faster and
118      * efficient. UDP packets are good for sending positional
119      * information to synchronize two applications. In this case, you
120      * want the information to arrive as quickly as possible, and if
121      * you lose a packet, you'd rather get new updated information
122      * rather than have the system waste time resending a packet that
123      * is becoming older and older with every retry.
124      * @param host name of host if direction is SG_IO_OUT or SG_IO_BI
125      * @param port port number if we care to choose one.
126      * @param style specify "udp" or "tcp"
127      */
128     SGSocket( const string& host, const string& port, const string& style );
129
130     /** Destructor */
131     ~SGSocket();
132
133     // If specified as a server (in direction for now) open the master
134     // listening socket.  If specified as a client (out direction),
135     // open a connection to a server.
136     bool open( const SGProtocolDir d );
137
138     // read data from socket
139     int read( char *buf, int length );
140
141     // read data from socket
142     int readline( char *buf, int length );
143
144     // write data to a socket
145     int write( const char *buf, const int length );
146
147     // write null terminated string to a socket
148     int writestring( const char *str );
149
150     // close file
151     bool close();
152
153     /**
154      * Enable non-blocking mode.
155      * @return success/failure
156      */
157     bool nonblock();
158
159     /** @return the remote host name */
160     inline string get_hostname() const { return hostname; }
161
162     /** @return the port number (in string form) */
163     inline string get_port_str() const { return port_str; }
164 };
165
166
167 #endif // _SG_SOCKET_HXX