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