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