]> icculus.org git repositories - taylor/freespace2.git/blob - include/psnet2.h
embed standalone web in executable
[taylor/freespace2.git] / include / psnet2.h
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Network/Psnet2.h $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Header file for the application level network-interface.
16  *
17  * $Log$
18  * Revision 1.7  2005/10/01 21:38:32  taylor
19  * some reorg of includes to not requires the same headers in many different files
20  * handle FS1 specific language changes
21  * some OS X changes for paths and to handle socklen_t better
22  *
23  * Revision 1.6  2002/06/16 05:20:01  relnev
24  * work around some gcc 3.1 weirdness
25  *
26  * Revision 1.5  2002/06/09 04:41:14  relnev
27  * added copyright header
28  *
29  * Revision 1.4  2002/05/27 00:40:47  theoddone33
30  * Fix net_addr vs net_addr_t
31  *
32  * Revision 1.3  2002/05/26 21:27:53  theoddone33
33  * More progress (I hate psnet2)
34  *
35  * Revision 1.2  2002/05/26 20:22:48  theoddone33
36  * Most of network/ works
37  *
38  * Revision 1.1.1.1  2002/05/03 03:28:12  root
39  * Initial import.
40  *
41  * 
42  * 9     8/16/99 4:06p Dave
43  * Big honking checkin.
44  * 
45  * 8     7/28/99 11:46a Dave
46  * Put in FS2_DEMO defines for port stuff.
47  * 
48  * 7     6/25/99 11:59a Dave
49  * Multi options screen.
50  * 
51  * 6     6/07/99 9:51p Dave
52  * Consolidated all multiplayer ports into one.
53  * 
54  * 5     3/09/99 6:24p Dave
55  * More work on object update revamping. Identified several sources of
56  * unnecessary bandwidth.
57  * 
58  * 4     11/19/98 4:19p Dave
59  * Put IPX sockets back in psnet. Consolidated all multiplayer config
60  * files into one.
61  * 
62  * 3     11/19/98 8:04a Dave
63  * Full support for D3-style reliable sockets. Revamped packet lag/loss
64  * system, made it receiver side and at the lowest possible level.
65  * 
66  * $NoKeywords: $
67  */
68
69
70 #ifndef _PSNET2_H
71 #define _PSNET2_H
72
73 /* sigh */
74 #ifdef PLAT_UNIX
75 #include <sys/select.h>
76 #include <sys/time.h>
77 #include <sys/types.h>
78 #include <sys/socket.h>
79 #include <unistd.h>
80 #else
81 #define WIN32_LEAN_AND_MEAN
82 #include <winsock.h>
83 #endif
84
85 #include "pstypes.h"
86
87
88 // -------------------------------------------------------------------------------------------------------
89 // PSNET 2 DEFINES/VARS
90 //
91
92 #define NET_NONE                0               // if no protocol is active or none are selected
93 #define NET_TCP         1
94 #define NET_IPX         2                       // ** no longer supported !!!! **
95 #define NET_VMT         3
96
97 #define MAX_PACKET_SIZE         512
98
99 #ifdef FS2_DEMO
100         #define DEFAULT_GAME_PORT 7802
101 #else
102         #define DEFAULT_GAME_PORT 7808
103 #endif
104
105 typedef struct net_addr {
106         uint    type;                   // See NET_ defines above
107         ubyte addr[4];          // address
108         short port;
109         short _pad;                     // alignment padding
110 } net_addr_t;
111
112 // define these in such a manner that a call to psnet_send_reliable is exactly the same and the new code in unobtrusive
113 typedef uint PSNET_SOCKET;
114 typedef uint PSNET_SOCKET_RELIABLE;
115 #undef INVALID_SOCKET
116 #define INVALID_SOCKET (PSNET_SOCKET)(~0)
117
118 // defines for protocol overheads
119 #define UDP_HEADER_SIZE                                         34
120 #define TCP_HEADER_SIZE                                         40
121 #define TCP_HEADER_SIZE_COMPRESSED              6
122
123 // define values for network errors when trying to enter the ready room
124 #define NETWORK_ERROR_NONE                                      0
125 #define NETWORK_ERROR_NO_TYPE                           -1
126 #define NETWORK_ERROR_NO_WINSOCK                        -2
127 #define NETWORK_ERROR_NO_PROTOCOL               -3
128 #define NETWORK_ERROR_RELIABLE                  -4
129 #define NETWORK_ERROR_CONNECT_TO_ISP    -5
130 #define NETWORK_ERROR_LAN_AND_RAS               -6
131
132 // psnet packet types
133 #define PSNET_NUM_TYPES                                         5
134 #define PSNET_TYPE_UNRELIABLE                           0
135 #define PSNET_TYPE_RELIABLE                             1
136 #define PSNET_TYPE_USER_TRACKER                 2
137 #define PSNET_TYPE_GAME_TRACKER                 3
138 #define PSNET_TYPE_VALIDATION                           4
139
140 extern net_addr_t Psnet_my_addr;                                                        // address information of this machine
141 extern uint Psnet_my_ip;
142 extern int Psnet_my_addr_valid;
143
144 extern int Network_status;
145 extern int Tcp_failure_code;
146
147 extern int Tcp_active;
148
149 extern int Socket_type;                                                                         // protocol type in use (see NET_* defines above)
150
151 // specified their internet connnection type
152 #define NETWORK_CONNECTION_NONE                 1
153 #define NETWORK_CONNECTION_DIALUP               2
154 #define NETWORK_CONNECTION_LAN                  3
155
156 extern int Psnet_connection;
157
158 extern ushort Psnet_default_port;
159
160 // Reliable socket states
161 #define RNF_UNUSED                      0               // Completely clean socket..
162 #define RNF_CONNECTED           1               // Connected and running fine
163 #define RNF_BROKEN                      2               // Broken - disconnected abnormally
164 #define RNF_DISCONNECTED        3               // Disconnected cleanly
165 #define RNF_CONNECTING          4               // We received the connecting message, but haven't told the game yet.
166 #define RNF_LIMBO                               5               // between connecting and connected
167
168 extern SOCKET Unreliable_socket;        // all PXO API modules should use this to send and receive on
169
170 // -------------------------------------------------------------------------------------------------------
171 // PSNET 2 TOP LAYER FUNCTIONS - these functions simply buffer and store packets based upon type (see PSNET_TYPE_* defines)
172 //
173
174 #ifndef PLAT_UNIX
175 struct sockaddr;
176 struct fd_set;
177 struct timeval;
178 #endif
179
180 // wrappers around select() and recvfrom() for lagging/losing data, and for sorting through different packet types
181 int RECVFROM(SOCKET s, char * buf, int len, int flags, sockaddr *from, int *fromlen, int psnet_type);
182 int SELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set*exceptfds, struct timeval* timeout, int psnet_type);
183
184 // wrappers around sendto to sorting through different packet types
185 int SENDTO(SOCKET s, char * buf, int len, int flags, sockaddr * to, int tolen, int psnet_type);
186
187 // call this once per frame to read everything off of our socket
188 void PSNET_TOP_LAYER_PROCESS();
189
190
191 // -------------------------------------------------------------------------------------------------------
192 // PSNET 2 FUNCTIONS
193 //
194
195 // initialize psnet to use the specified port
196 void psnet_init(int protocol, int default_port);
197
198 // shutdown psnet
199 void psnet_close();
200
201 // set the protocol to use
202 int psnet_use_protocol(int type);
203
204 // get the status of the network
205 int psnet_get_network_status();
206
207 // convert a net_addr to a string
208 char *psnet_addr_to_string(char * text, const int max_textlen, net_addr_t * address );
209
210 // convert a string to a net addr
211 void psnet_string_to_addr(net_addr_t * address, char * text , const int max_textlen);
212
213 // compare 2 addresses
214 int psnet_same( net_addr_t * a1, net_addr_t * a2 );
215
216 // send data unreliably
217 int psnet_send( net_addr_t * who_to, void * data, int len, int np_index = -1 );
218
219 // get data from the unreliable socket
220 int psnet_get( void * data, net_addr_t * from_addr );
221
222 // broadcast data on unreliable socket
223 int psnet_broadcast( net_addr_t * who_to, void * data, int len );
224
225 // flush all sockets
226 void psnet_flush();
227
228 // if the passed string is a valid IP string
229 int psnet_is_valid_ip_string( char *ip_string, int allow_port=1 );
230
231 // mark a socket as having received data
232 void psnet_mark_received(PSNET_SOCKET_RELIABLE socket);
233
234
235 // -------------------------------------------------------------------------------------------------------
236 // PSNET 2 RELIABLE SOCKET FUNCTIONS
237 //
238
239 // shutdown a reliable socket
240 void psnet_rel_close_socket(PSNET_SOCKET_RELIABLE *sockp);
241
242 // obsolete function - left in for compatibility sake
243 int psnet_rel_check();
244
245 // send data on the reliable socket
246 int psnet_rel_send(PSNET_SOCKET_RELIABLE socket, ubyte *data, int length, int np_index = -1);
247
248 // Return codes:
249 // -1 socket not connected
250 // 0 No packet ready to receive
251 // >0 Buffer filled with the number of bytes recieved
252 int psnet_rel_get(PSNET_SOCKET_RELIABLE socket, ubyte *buffer, int max_length);
253
254 // process all active reliable sockets
255 void psnet_rel_work();
256
257 // get the status of a reliable socket, see RNF_* defines above
258 int psnet_rel_get_status(PSNET_SOCKET_RELIABLE sock);
259
260 // check the listen socket for pending reliable connections
261 int psnet_rel_check_for_listen(net_addr_t *addr);
262
263 // perform a reliable socket connect to the specified server
264 void psnet_rel_connect_to_server(PSNET_SOCKET_RELIABLE *s, net_addr_t *server_addr);
265
266 #endif
267