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