2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Network/PsNet.h $
15 * Header file for the application level network-interface.
18 * Revision 1.2 2002/06/09 04:41:14 relnev
19 * added copyright header
21 * Revision 1.1.1.1 2002/05/03 03:28:12 root
25 * 5 11/19/98 4:19p Dave
26 * Put IPX sockets back in psnet. Consolidated all multiplayer config
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.
33 * 3 11/17/98 11:12a Dave
34 * Removed player identification by address. Now assign explicit id #'s.
36 * 2 10/07/98 10:53a Dave
39 * 1 10/07/98 10:50a Dave
41 * 41 10/02/98 3:22p Allender
42 * fix up the -connect option and fix the -port option
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
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
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.
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
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
64 * 35 5/21/98 3:31p Allender
65 * more RAS stuff -- fix possible problem when unable to read data on
68 * 34 5/18/98 4:10p Allender
69 * ditch any reference to IPX initialziation. put in (butt comment out)
72 * 33 5/01/98 3:15a Dave
73 * Tweaked object update system. Put in new packet buffering system.
75 * 32 4/04/98 4:22p Dave
76 * First rev of UDP reliable sockets is done. Seems to work well if not
79 * 31 4/03/98 1:03a Dave
80 * First pass at unreliable guaranteed delivery packets.
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
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
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.
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.
98 * 26 12/16/97 6:19p Dave
99 * Put in primary weapon support for multiplayer weapon select.
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.
105 * 24 12/11/97 8:15p Dave
106 * Put in network options screen. Xed out olf protocol selection screen.
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
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
117 * 21 11/04/97 3:50p Allender
118 * more reliable socket stuff. Removed admin port. Cleaner sequencing,
121 * 20 11/03/97 8:25p Dave
122 * Got client side reliable sockets working. Got reliable/unreliable
123 * pakcet interleaving done.
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).
129 * 18 10/24/97 6:22p Sandeep
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.
136 * 16 8/26/97 5:02p Dave
137 * Put in admin socket handling thread. Modified all functions to support
140 * 15 8/20/97 4:34p Dave
141 * Added admin socket init to psnet_init()
143 * 14 8/13/97 4:55p Dave
144 * Moved PSNET_FLAG_CHECKSUM bit #define into psnet.h
146 * 13 8/11/97 3:18p Dave
147 * Added administration client socket.
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.
153 * 11 7/30/97 9:39a Allender
154 * network debug stuff -- showing net read/write stats on hud
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
160 * 9 1/03/97 12:01p Lawrance
161 * Now getting return address and port from the network layer header.
163 * 8 1/02/97 2:07p Lawrance
164 * fixed problem with reading socket data
166 * 7 1/01/97 6:45p Lawrance
167 * added support for IPX
183 // use Berkeley reliable sockets if defined - otherwise use Volition reliable sockets
184 #define PSNET_RELIABLE_OLD_SCHOOL
186 #define NET_NONE 0 // if no protocol is active or none are selected
191 #define MAX_PACKET_SIZE 512
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
196 #define DEFAULT_GAME_PORT 7802
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)
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;
210 #undef INVALID_SOCKET
211 #define INVALID_SOCKET (PSNET_SOCKET)(~0)
213 typedef net_addr* PSNET_SOCKET;
214 typedef net_addr* PSNET_SOCKET_RELIABLE
216 #undef INVALID_SOCKET
217 #define INVALID_SOCKET NULL
221 // defines for protocol overheads
222 #define UDP_HEADER_SIZE 34
223 #define TCP_HEADER_SIZE 40
224 #define TCP_HEADER_SIZE_COMPRESSED 6
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
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;
239 extern int Network_status;
240 extern int Tcp_failure_code;
241 extern int Ipx_failure_code;
243 extern int Tcp_active;
245 // specified their internet connnection type
246 #define NETWORK_CONNECTION_NONE 1
247 #define NETWORK_CONNECTION_DIALUP 2
248 #define NETWORK_CONNECTION_LAN 3
250 extern int Psnet_connection;
252 extern ushort Psnet_default_port;
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.
262 void ipx_ntoa(net_addr *addr, char *text); // this is a HUGE hack right now. Just the 6 byte equivalent of inet_ntoa
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();
271 extern void psnet_whoami( net_addr * my_address );
272 extern char* psnet_addr_to_string( char * text, net_addr * address );
273 extern void psnet_string_to_addr( net_addr * address, char * text );
274 extern int psnet_same( net_addr * a1, net_addr * a2 );
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 );
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);
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 );
287 extern void psnet_flush();
288 extern int psnet_is_valid_ip_string( char *ip_string, int allow_port=1 );
290 // initialize the buffering system
291 extern void psnet_buffer_init();
293 // buffer a packet (maintain order!)
294 extern void psnet_buffer_packet(ubyte *data, int length, net_addr *from);
296 // get the index of the next packet in order!
297 extern int psnet_buffer_get_next(ubyte *data, int *length, net_addr *from);
300 // -------------------------------------------------------------------------------------
301 // PSNET RELIABLE UDP STUFF
304 // initialize the psnet reliable system (return 0 on fail, 1 on success)
305 int psnet_reliable_init();
307 // shutdown the reliable system (free up buffers, etc)
308 void psnet_reliable_close();
310 // notify the reliable system of a new address at index N
311 void psnet_reliable_notify_new_addr(net_addr *addr,int index);
313 // notify the reliable system of a drop at index N
314 void psnet_reliable_notify_drop_addr(net_addr *addr);
316 // send a reliable data packet
317 int psnet_reliable_send(ubyte *data,int packet_size,net_addr *addr);
319 // process frame for all reliable stuff (call once per frame)
320 void psnet_reliable_process();
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);
328 extern void psnet_stats_init();
329 extern int psnet_get_stats( net_addr *addr, int *tr, int *tw );
332 #endif // #ifdef PSNET2