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