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