]> icculus.org git repositories - taylor/freespace2.git/blob - include/psnet.h
re-add PXO sources to project files
[taylor/freespace2.git] / include / psnet.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/PsNet.h $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Header file for the application level network-interface.
16  *
17  * $Log$
18  * Revision 1.2  2002/06/09 04:41:14  relnev
19  * added copyright header
20  *
21  * Revision 1.1.1.1  2002/05/03 03:28:12  root
22  * Initial import.
23  *
24  * 
25  * 5     11/19/98 4:19p Dave
26  * Put IPX sockets back in psnet. Consolidated all multiplayer config
27  * files into one.
28  * 
29  * 4     11/19/98 8:04a Dave
30  * Full support for D3-style reliable sockets. Revamped packet lag/loss
31  * system, made it receiver side and at the lowest possible level.
32  * 
33  * 3     11/17/98 11:12a Dave
34  * Removed player identification by address. Now assign explicit id #'s.
35  * 
36  * 2     10/07/98 10:53a Dave
37  * Initial checkin.
38  * 
39  * 1     10/07/98 10:50a Dave
40  * 
41  * 41    10/02/98 3:22p Allender
42  * fix up the -connect option and fix the -port option
43  * 
44  * 40    9/14/98 11:28a Allender
45  * support for server bashing of address when received from client.  Added
46  * a cmdline.cfg file to process command line arguments from a file
47  * 
48  * 39    8/07/98 10:40a Allender
49  * new command line flags for starting netgames.  Only starting currently
50  * works, and PXO isn't implemented yet
51  * 
52  * 38    6/30/98 2:17p Dave
53  * Revised object update system. Removed updates for all weapons. Put
54  * button info back into control info packet.
55  * 
56  * 37    6/04/98 11:04a Allender
57  * object update level stuff.  Don't reset to high when becoming an
58  * observer of any type.  default to low when guy is a dialup customer
59  * 
60  * 36    5/22/98 10:54a Allender
61  * new dialog information for networking to tell user if connection type
62  * doesn't match the setup specified type
63  * 
64  * 35    5/21/98 3:31p Allender
65  * more RAS stuff -- fix possible problem when unable to read data on
66  * reliable socket
67  * 
68  * 34    5/18/98 4:10p Allender
69  * ditch any reference to IPX initialziation.  put in (butt comment out)
70  * the RAS code
71  * 
72  * 33    5/01/98 3:15a Dave
73  * Tweaked object update system. Put in new packet buffering system.
74  * 
75  * 32    4/04/98 4:22p Dave
76  * First rev of UDP reliable sockets is done. Seems to work well if not
77  * overly burdened.
78  * 
79  * 31    4/03/98 1:03a Dave
80  * First pass at unreliable guaranteed delivery packets.
81  * 
82  * 30    3/11/98 11:42p Allender
83  * more ingame join stuff.  Fix to networking code to possibly
84  * reinitialize reliable socket when entering join screen
85  * 
86  * 29    2/05/98 11:44a Allender
87  * enhcanced network statistic collection.  Made callback in debug console
88  * to do networking if player is in the console
89  * 
90  * 28    2/04/98 6:35p Dave
91  * Changed psnet to use raw data with no headers. Started putting in
92  * support for master tracker security measures.
93  * 
94  * 27    1/11/98 10:03p Allender
95  * removed <winsock.h> from headers which included it.  Made psnet_socket
96  * type which is defined just as SOCKET type is.
97  * 
98  * 26    12/16/97 6:19p Dave
99  * Put in primary weapon support for multiplayer weapon select.
100  * 
101  * 25    12/16/97 5:24p Allender
102  * changed to options menu to allow FQDN's.  changes to player booting
103  * code (due to timeout).  more work still needs to be done though.
104  * 
105  * 24    12/11/97 8:15p Dave
106  * Put in network options screen. Xed out olf protocol selection screen.
107  * 
108  * 23    12/03/97 11:48a Allender
109  * overhaul on reliable socket code.  Made reliable sockets non-blocking
110  * (listen socket still blocks).  Made player timeouts work correctly with
111  * certain winsock errors on the reliable sockets
112  * 
113  * 22    11/11/97 11:55a Allender
114  * initialize network at beginning of application.  create new call to set
115  * which network protocol to use
116  * 
117  * 21    11/04/97 3:50p Allender
118  * more reliable socket stuff.  Removed admin port.  Cleaner sequencing,
119  * etc.
120  * 
121  * 20    11/03/97 8:25p Dave
122  * Got client side reliable sockets working. Got reliable/unreliable
123  * pakcet interleaving done.
124  * 
125  * 19    11/03/97 5:09p Allender
126  * added reliable transport system -- still in infancy.  Each netplayer
127  * has reliable socket to server (and server to clients).
128  * 
129  * 18    10/24/97 6:22p Sandeep
130  * added checknet
131  * 
132  * 17    9/17/97 9:09a Dave
133  * Observer mode work, put in standalone controls. Fixed up some stuff for
134  * ingame join broken by recent code checkins.
135  * 
136  * 16    8/26/97 5:02p Dave
137  * Put in admin socket handling thread. Modified all functions to support
138  * this.
139  * 
140  * 15    8/20/97 4:34p Dave
141  * Added admin socket init to psnet_init()
142  * 
143  * 14    8/13/97 4:55p Dave
144  * Moved PSNET_FLAG_CHECKSUM bit #define into psnet.h
145  * 
146  * 13    8/11/97 3:18p Dave
147  * Added administration client socket.
148  * 
149  * 12    8/04/97 9:41p Allender
150  * revamped packet structure for netgames.  No more magic number.  psnet*
151  * functions can now do checksumming at it's level.
152  * 
153  * 11    7/30/97 9:39a Allender
154  * network debug stuff -- showing net read/write stats on hud
155  * 
156  * 10    6/11/97 1:38p Allender
157  * added basic sequencing through ship selection.  Put My_addr structure
158  * into psnet.cpp where it should be
159  * 
160  * 9     1/03/97 12:01p Lawrance
161  * Now getting return address and port from the network layer header.
162  * 
163  * 8     1/02/97 2:07p Lawrance
164  * fixed problem with reading socket data
165  *
166  * 7     1/01/97 6:45p Lawrance
167  * added support for IPX
168  *
169  * $NoKeywords: $
170  */
171
172
173 #ifndef _PSNET_H
174 #define _PSNET_H
175
176 // use PSNET 2
177 #define PSNET2
178
179 #ifdef PSNET2
180         #include "psnet2.h"
181 #else 
182
183 // use Berkeley reliable sockets if defined - otherwise use Volition reliable sockets
184 #define PSNET_RELIABLE_OLD_SCHOOL
185
186 #define NET_NONE                0               // if no protocol is active or none are selected
187 #define NET_TCP         1
188 #define NET_IPX         2
189 #define NET_VMT         3
190
191 #define MAX_PACKET_SIZE         512
192
193 #define PSNET_FLAG_CHECKSUM     (1<<0)          // this packet is checksummed
194 #define PSNET_FLAG_RAW                  (1<<1)          // send or receive raw data. don't do any checksumming, sequencing, etc
195
196 #define DEFAULT_GAME_PORT 7802
197
198 typedef struct net_addr {
199         uint    type;                   // See NET_ defines above
200         ubyte   net_id[4];      // used for IPX only
201         ubyte addr[6];          // address (first 4 used when IP, all 6 used when IPX)
202         short port;                     
203 } net_addr;
204
205 // define these in such a manner that a call to psnet_send_reliable is exactly the same and the new code in unobtrusive
206 #ifdef PSNET_RELIABLE_OLD_SCHOOL
207         typedef uint PSNET_SOCKET;
208         typedef uint PSNET_SOCKET_RELIABLE;
209
210         #undef INVALID_SOCKET
211         #define INVALID_SOCKET (PSNET_SOCKET)(~0)
212 #else
213         typedef net_addr* PSNET_SOCKET;
214         typedef net_addr* PSNET_SOCKET_RELIABLE
215
216         #undef INVALID_SOCKET
217         #define INVALID_SOCKET NULL
218
219 #endif
220
221 // defines for protocol overheads
222 #define UDP_HEADER_SIZE                                         34
223 #define TCP_HEADER_SIZE                                         40
224 #define TCP_HEADER_SIZE_COMPRESSED              6
225
226 // define values for network errors when trying to enter the ready room
227 #define NETWORK_ERROR_NONE                                      0
228 #define NETWORK_ERROR_NO_TYPE                           -1
229 #define NETWORK_ERROR_NO_WINSOCK                        -2
230 #define NETWORK_ERROR_NO_PROTOCOL               -3
231 #define NETWORK_ERROR_RELIABLE                  -4
232 #define NETWORK_ERROR_CONNECT_TO_ISP    -5
233 #define NETWORK_ERROR_LAN_AND_RAS               -6
234
235 extern net_addr Psnet_my_addr;                                                  // address information of this machine
236 extern uint Psnet_my_ip;
237 extern int Psnet_my_addr_valid;
238
239 extern int Network_status;
240 extern int Tcp_failure_code;
241 extern int Ipx_failure_code;
242
243 extern int Tcp_active;
244
245 // specified their internet connnection type
246 #define NETWORK_CONNECTION_NONE                 1
247 #define NETWORK_CONNECTION_DIALUP               2
248 #define NETWORK_CONNECTION_LAN                  3
249
250 extern int Psnet_connection;
251
252 extern ushort Psnet_default_port;
253
254
255 #ifndef NDEBUG
256
257 extern int      Psnet_bytes_read;                                       // globally available numbers for printing on the hud
258 extern int      Psnet_bytes_written;
259 extern void psnet_calc_socket_stats();          // routine to calc stats for this frame.
260 #endif
261
262 void ipx_ntoa(net_addr *addr, char *text); // this is a HUGE hack right now. Just the 6 byte equivalent of inet_ntoa
263
264 extern void psnet_init( int protocol, int default_port );
265 extern void psnet_close();
266 extern int psnet_use_protocol( int type );
267 extern void psnet_rel_close_socket( PSNET_SOCKET *sockp );
268 extern int psnet_rel_check();
269 extern int psnet_get_network_status();
270
271 extern void psnet_whoami( net_addr * my_address );
272 extern char* psnet_addr_to_string( char * text, const int max_textlen, net_addr * address );
273 extern void psnet_string_to_addr( net_addr * address, char * text, const int max_textlen );
274 extern int psnet_same( net_addr * a1, net_addr * a2 );
275
276 extern int psnet_send( net_addr * who_to, void * data, int len, int flags = PSNET_FLAG_RAW, int reliable_socket = 0 );
277 extern int psnet_get( void * data, net_addr * from_addr, int flags = PSNET_FLAG_RAW );
278 extern int psnet_broadcast( net_addr * who_to, void * data, int len,int flags = PSNET_FLAG_RAW );
279
280 // functions for reliable socket stuff
281 extern int psnet_rel_send( PSNET_SOCKET socket, ubyte *data, int length, int flags = PSNET_FLAG_RAW );
282 extern int psnet_rel_get( PSNET_SOCKET socket, ubyte *buffer, int max_length, int flags = PSNET_FLAG_RAW);
283
284 extern int psnet_rel_check_for_listen( net_addr *addr );
285 extern void psnet_rel_connect_to_server( PSNET_SOCKET *s, net_addr *server_addr );
286
287 extern void psnet_flush();
288 extern int psnet_is_valid_ip_string( char *ip_string, int allow_port=1 );
289
290 // initialize the buffering system
291 extern void psnet_buffer_init();
292
293 // buffer a packet (maintain order!)
294 extern void psnet_buffer_packet(ubyte *data, int length, net_addr *from);
295
296 // get the index of the next packet in order!
297 extern int psnet_buffer_get_next(ubyte *data, int *length, net_addr *from);
298
299
300 // -------------------------------------------------------------------------------------
301 // PSNET RELIABLE UDP STUFF
302 //
303
304 // initialize the psnet reliable system (return 0 on fail, 1 on success)
305 int psnet_reliable_init();
306
307 // shutdown the reliable system (free up buffers, etc)
308 void psnet_reliable_close();
309
310 // notify the reliable system of a new address at index N
311 void psnet_reliable_notify_new_addr(net_addr *addr,int index);
312
313 // notify the reliable system of a drop at index N
314 void psnet_reliable_notify_drop_addr(net_addr *addr);
315
316 // send a reliable data packet
317 int psnet_reliable_send(ubyte *data,int packet_size,net_addr *addr);
318
319 // process frame for all reliable stuff (call once per frame)
320 void psnet_reliable_process();
321
322 // determine if the passed in reliable data should be processed, and sends an ack if necessary
323 // return # of bytes which should be stripped off the data (reliable data header)
324 int psnet_reliable_should_process(net_addr *addr,ubyte *data,int packet_size);
325
326
327 #ifndef NDEBUG
328 extern void psnet_stats_init();
329 extern int psnet_get_stats( net_addr *addr, int *tr, int *tw );
330 #endif
331
332 #endif // #ifdef PSNET2
333
334 #endif
335