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/Psnet2.h $
15 * Header file for the application level network-interface.
18 * Revision 1.5 2002/06/09 04:41:14 relnev
19 * added copyright header
21 * Revision 1.4 2002/05/27 00:40:47 theoddone33
22 * Fix net_addr vs net_addr_t
24 * Revision 1.3 2002/05/26 21:27:53 theoddone33
25 * More progress (I hate psnet2)
27 * Revision 1.2 2002/05/26 20:22:48 theoddone33
28 * Most of network/ works
30 * Revision 1.1.1.1 2002/05/03 03:28:12 root
34 * 9 8/16/99 4:06p Dave
35 * Big honking checkin.
37 * 8 7/28/99 11:46a Dave
38 * Put in FS2_DEMO defines for port stuff.
40 * 7 6/25/99 11:59a Dave
41 * Multi options screen.
43 * 6 6/07/99 9:51p Dave
44 * Consolidated all multiplayer ports into one.
46 * 5 3/09/99 6:24p Dave
47 * More work on object update revamping. Identified several sources of
48 * unnecessary bandwidth.
50 * 4 11/19/98 4:19p Dave
51 * Put IPX sockets back in psnet. Consolidated all multiplayer config
54 * 3 11/19/98 8:04a Dave
55 * Full support for D3-style reliable sockets. Revamped packet lag/loss
56 * system, made it receiver side and at the lowest possible level.
67 // -------------------------------------------------------------------------------------------------------
68 // PSNET 2 DEFINES/VARS
71 #define NET_NONE 0 // if no protocol is active or none are selected
76 #define MAX_PACKET_SIZE 512
79 #define DEFAULT_GAME_PORT 7802
81 #define DEFAULT_GAME_PORT 7808
84 typedef struct net_addr {
85 uint type; // See NET_ defines above
86 ubyte net_id[4]; // used for IPX only
87 ubyte addr[6]; // address (first 4 used when IP, all 6 used when IPX)
91 // define these in such a manner that a call to psnet_send_reliable is exactly the same and the new code in unobtrusive
92 typedef uint PSNET_SOCKET;
93 typedef uint PSNET_SOCKET_RELIABLE;
95 #define INVALID_SOCKET (PSNET_SOCKET)(~0)
97 // defines for protocol overheads
98 #define UDP_HEADER_SIZE 34
99 #define TCP_HEADER_SIZE 40
100 #define TCP_HEADER_SIZE_COMPRESSED 6
102 // define values for network errors when trying to enter the ready room
103 #define NETWORK_ERROR_NONE 0
104 #define NETWORK_ERROR_NO_TYPE -1
105 #define NETWORK_ERROR_NO_WINSOCK -2
106 #define NETWORK_ERROR_NO_PROTOCOL -3
107 #define NETWORK_ERROR_RELIABLE -4
108 #define NETWORK_ERROR_CONNECT_TO_ISP -5
109 #define NETWORK_ERROR_LAN_AND_RAS -6
111 // psnet packet types
112 #define PSNET_NUM_TYPES 5
113 #define PSNET_TYPE_UNRELIABLE 0
114 #define PSNET_TYPE_RELIABLE 1
115 #define PSNET_TYPE_USER_TRACKER 2
116 #define PSNET_TYPE_GAME_TRACKER 3
117 #define PSNET_TYPE_VALIDATION 4
119 extern net_addr_t Psnet_my_addr; // address information of this machine
120 extern uint Psnet_my_ip;
121 extern int Psnet_my_addr_valid;
123 extern int Network_status;
124 extern int Tcp_failure_code;
125 extern int Ipx_failure_code;
127 extern int Tcp_active;
128 extern int Ipx_active;
130 extern int Socket_type; // protocol type in use (see NET_* defines above)
132 // specified their internet connnection type
133 #define NETWORK_CONNECTION_NONE 1
134 #define NETWORK_CONNECTION_DIALUP 2
135 #define NETWORK_CONNECTION_LAN 3
137 extern int Psnet_connection;
139 extern ushort Psnet_default_port;
141 // Reliable socket states
142 #define RNF_UNUSED 0 // Completely clean socket..
143 #define RNF_CONNECTED 1 // Connected and running fine
144 #define RNF_BROKEN 2 // Broken - disconnected abnormally
145 #define RNF_DISCONNECTED 3 // Disconnected cleanly
146 #define RNF_CONNECTING 4 // We received the connecting message, but haven't told the game yet.
147 #define RNF_LIMBO 5 // between connecting and connected
149 extern SOCKET Unreliable_socket; // all PXO API modules should use this to send and receive on
151 // -------------------------------------------------------------------------------------------------------
152 // PSNET 2 TOP LAYER FUNCTIONS - these functions simply buffer and store packets based upon type (see PSNET_TYPE_* defines)
159 // wrappers around select() and recvfrom() for lagging/losing data, and for sorting through different packet types
160 int RECVFROM(uint s, char * buf, int len, int flags, sockaddr *from, int *fromlen, int psnet_type);
161 int SELECT(int nfds, fd_set *readfds, fd_set *writefds, fd_set*exceptfds, const timeval* timeout, int psnet_type);
163 // wrappers around sendto to sorting through different packet types
164 int SENDTO(uint s, char * buf, int len, int flags, sockaddr * to, int tolen, int psnet_type);
166 // call this once per frame to read everything off of our socket
167 void PSNET_TOP_LAYER_PROCESS();
170 // -------------------------------------------------------------------------------------------------------
174 // initialize psnet to use the specified port
175 void psnet_init(int protocol, int default_port);
180 // set the protocol to use
181 int psnet_use_protocol(int type);
183 // get the status of the network
184 int psnet_get_network_status();
186 // convert a net_addr to a string
187 char *psnet_addr_to_string( char * text, net_addr_t * address );
189 // convert a string to a net addr
190 void psnet_string_to_addr( net_addr_t * address, char * text );
192 // compare 2 addresses
193 int psnet_same( net_addr_t * a1, net_addr_t * a2 );
195 // send data unreliably
196 int psnet_send( net_addr_t * who_to, void * data, int len, int np_index = -1 );
198 // get data from the unreliable socket
199 int psnet_get( void * data, net_addr_t * from_addr );
201 // broadcast data on unreliable socket
202 int psnet_broadcast( net_addr_t * who_to, void * data, int len );
207 // if the passed string is a valid IP string
208 int psnet_is_valid_ip_string( char *ip_string, int allow_port=1 );
210 // mark a socket as having received data
211 void psnet_mark_received(PSNET_SOCKET_RELIABLE socket);
214 // -------------------------------------------------------------------------------------------------------
215 // PSNET 2 RELIABLE SOCKET FUNCTIONS
218 // shutdown a reliable socket
219 void psnet_rel_close_socket(PSNET_SOCKET_RELIABLE *sockp);
221 // obsolete function - left in for compatibility sake
222 int psnet_rel_check();
224 // send data on the reliable socket
225 int psnet_rel_send(PSNET_SOCKET_RELIABLE socket, ubyte *data, int length, int np_index = -1);
228 // -1 socket not connected
229 // 0 No packet ready to receive
230 // >0 Buffer filled with the number of bytes recieved
231 int psnet_rel_get(PSNET_SOCKET_RELIABLE socket, ubyte *buffer, int max_length);
233 // process all active reliable sockets
234 void psnet_rel_work();
236 // get the status of a reliable socket, see RNF_* defines above
237 int psnet_rel_get_status(PSNET_SOCKET_RELIABLE sock);
239 // check the listen socket for pending reliable connections
240 int psnet_rel_check_for_listen(net_addr_t *addr);
242 // perform a reliable socket connect to the specified server
243 void psnet_rel_connect_to_server(PSNET_SOCKET_RELIABLE *s, net_addr_t *server_addr);