2 * $Logfile: /Freespace2/code/Network/PsNet.cpp $
7 * C file containing application level network-interface.
10 * Revision 1.1 2002/05/03 03:28:10 root
14 * 7 7/15/99 9:20a Andsager
15 * FS2_DEMO initial checkin
17 * 6 11/19/98 4:19p Dave
18 * Put IPX sockets back in psnet. Consolidated all multiplayer config
21 * 5 11/19/98 8:04a Dave
22 * Full support for D3-style reliable sockets. Revamped packet lag/loss
23 * system, made it receiver side and at the lowest possible level.
25 * 4 11/17/98 11:12a Dave
26 * Removed player identification by address. Now assign explicit id #'s.
28 * 3 10/09/98 2:57p Dave
29 * Starting splitting up OS stuff.
31 * 2 10/07/98 10:53a Dave
34 * 1 10/07/98 10:50a Dave
36 * 107 9/15/98 4:04p Allender
37 * added back in the -ip_addr command line switch because it needs to be
38 * in the standalone server only executable
40 * 106 9/14/98 11:28a Allender
41 * support for server bashing of address when received from client. Added
42 * a cmdline.cfg file to process command line arguments from a file
44 * 105 9/08/98 2:20p Allender
45 * temporary code to force IP address to a specific value.
47 * 104 8/21/98 1:15p Dave
48 * Put in log system hooks in useful places.
50 * 103 8/12/98 4:50p Allender
51 * fix error with new write socket code
53 * 102 8/11/98 11:45a Allender
54 * fix major problem in psnet_send where select() would block until ready
55 * to send, which for slow modems could be a long time. Doesn't block,
56 * and now correctly returns when socket is not set for writing
58 * 101 8/07/98 10:40a Allender
59 * new command line flags for starting netgames. Only starting currently
60 * works, and PXO isn't implemented yet
62 * 100 7/28/98 4:43p Allender
63 * fix bug where error on receiving of reliable data would sometimes
66 * 99 7/06/98 5:05p Allender
67 * dont use default port to send UDP data to. Use port stored in address
69 * 98 6/13/98 9:32p Mike
70 * Kill last character in file which caused "Find in Files" to report the
71 * file as "not a text file."
73 * 97 6/13/98 6:01p Hoffoss
74 * Externalized all new (or forgot to be added) strings to all the code.
76 * 96 6/13/98 3:19p Hoffoss
77 * NOX()ed out a bunch of strings that shouldn't be translated.
79 * 95 6/10/98 2:56p Dave
80 * Substantial changes to reduce bandwidth and latency problems.
82 * 94 6/05/98 9:55a Lawrance
85 * 93 5/24/98 2:17p Allender
86 * make reliable socket read all data before returning
88 * 92 5/22/98 10:54a Allender
89 * new dialog information for networking to tell user if connection type
90 * doesn't match the setup specified type
92 * 91 5/21/98 3:31p Allender
93 * more RAS stuff -- fix possible problem when unable to read data on
96 * 90 5/20/98 4:34p John
97 * fixed some bugs with rasenum connections. HAd to specify ansi versions
98 * in dll. Also, my pc returned 0 connections, before that still said
99 * there was a connection.
106 #include <windowsx.h>
112 #include <raserror.h>
118 #include "multiutil.h"
119 #include "multilag.h"
120 #include "osregistry.h"
122 #include "multi_log.h"
127 // old style packet buffering
128 // #define PSNET_BUFFER_OLD_SCHOOL
130 // sockets for IPX and TCP
132 SOCKET IPX_reliable_socket;
133 SOCKET IPX_listen_socket;
136 SOCKET TCP_reliable_socket;
137 SOCKET TCP_listen_socket;
139 // the sockets that the game will use when selecting network type
140 SOCKET Unreliable_socket = INVALID_SOCKET;
141 SOCKET Reliable_socket = INVALID_SOCKET;
142 SOCKET Listen_socket = INVALID_SOCKET;
144 BOOL Psnet_my_addr_valid;
145 net_addr Psnet_my_addr;
147 ubyte Null_address[6];
150 int Can_broadcast; // can we do broadcasting on our socket?
151 int Ipx_can_broadcast = 0;
152 int Tcp_can_broadcast = 0;
155 int Tcp_failure_code = 0;
156 int Ipx_failure_code = 0;
158 int Psnet_connection;
160 ushort Psnet_default_port;
162 // specified their internet connnection type
163 #define NETWORK_CONNECTION_NONE 1
164 #define NETWORK_CONNECTION_DIALUP 2
165 #define NETWORK_CONNECTION_LAN 3
167 // defines and variables to indicate network connection status
168 #define NETWORK_STATUS_NOT_INITIALIZED 1
169 #define NETWORK_STATUS_NO_WINSOCK 2 // winsock failed to initialize
170 #define NETWORK_STATUS_NO_PROTOCOL 3 // specified protocol doesn't appear to be loaded
171 #define NETWORK_STATUS_NO_RELIABLE 4
172 #define NETWORK_STATUS_RUNNING 5 // everything should be running
174 // defintion of structures that actually leave this machine. psnet_send give us only
175 // the data that we want to send. We will add a header onto this data (packet sequence
176 // number, possibly a checksum). We must include a 2 byte flags variable into both structure
177 // since the receiving end of this packet must know whether or not to checksum the packet.
179 // use the pack pragma to pack these structures to 2 byte aligment. Really only needed for
182 #pragma pack(push, 2)
184 // packet def for a checksum'ed packet
185 #define MAX_CHECKSUM_PACKET_SIZE 1024
186 typedef struct network_checksum_packet
191 ubyte data[MAX_CHECKSUM_PACKET_SIZE];
192 } network_checksum_packet;
194 // definition for a non-checksum packet
195 typedef struct network_packet
199 ubyte data[MAX_PACKET_SIZE];
200 } network_naked_packet;
202 // structure definition for our packet buffers
203 typedef struct network_packet_buffer
208 ubyte data[MAX_PACKET_SIZE];
209 } network_packet_buffer;
213 #define MAX_PACKET_BUFFERS 96
215 #ifdef PSNET_BUFFER_OLD_SCHOOL
216 LOCAL network_packet_buffer packet_buffers[MAX_PACKET_BUFFERS]; // buffer to hold packets sent to us
217 LOCAL short packet_free_list[MAX_PACKET_BUFFERS]; // contains id's of free packet buffers
218 LOCAL Num_packet_buffers;
219 LOCAL int Largest_packet_index = 0;
225 // variables to keep track of bytes read/written during the course of network play
228 #define PSNET_FRAME_FILTER 32
229 #define MAX_NET_STATS 32
233 net_addr addr; // stats for this address
234 int total_read; // total bytes read
235 int total_written; // total bytes read.
243 net_stats Psnet_stats[MAX_NET_STATS];
244 socket_xlate Psnet_socket_addr[MAX_NET_STATS];
246 int psnet_bytes_read_frame; // running count of bytes read this frame
247 int psnet_bytes_written_frame; // running count of bytes written this frame
249 int Psnet_bytes_read; // globally available numbers for printing on the hud
250 int Psnet_bytes_written;
252 int psnet_read_sizes[PSNET_FRAME_FILTER];
253 int psnet_write_sizes[PSNET_FRAME_FILTER];
255 int psnet_read_total;
256 int psnet_write_total;
258 int psnet_frame_count;
262 #define MAXHOSTNAME 128
264 #define MAX_RECEIVE_BUFSIZE (1<<15) // 32 K, eh?
265 #define MAX_SEND_RETRIES 20 // number of retries when sending would block
266 #define MAX_LINGER_TIME 0 // in seconds -- when lingering to close a socket
270 int psnet_same_no_port( net_addr * a1, net_addr * a2 );
272 // do stats for a particular address. num_bytes is the number of bytes either read or
273 // written (depending on the read flag. read flag is true when reading, 0 when writing
274 void psnet_do_net_stats( net_addr *addr, int num_bytes, int read )
278 for ( i = 0; i < MAX_NET_STATS; i++ ) {
279 if ( !Psnet_stats[i].in_use )
281 if ( addr && psnet_same_no_port(&Psnet_stats[i].addr, addr) )
285 if ( i == MAX_NET_STATS ) {
286 // find the first NULL entry
287 for ( i = 0; i < MAX_NET_STATS; i++ ) {
288 if ( !Psnet_stats[i].in_use )
291 if ( i == MAX_NET_STATS ) {
292 Int3(); // we should always have a slot
295 Psnet_stats[i].addr = *addr;
296 Psnet_stats[i].in_use = 1;
299 Psnet_stats[i].total_read += num_bytes;
301 Psnet_stats[i].total_written += num_bytes;
304 // returns the stats for the given network address. returns 1 when (i.e. net_addr was found),
305 // 0 otherwise. total read and total written are returned in the parameters
306 int psnet_get_stats( net_addr *addr, int *tr, int *tw )
310 for ( i = 0; i < MAX_NET_STATS; i++ ) {
311 if ( !Psnet_stats[i].in_use )
314 if ( addr && psnet_same_no_port(&Psnet_stats[i].addr, addr) )
318 if ( i == MAX_NET_STATS )
321 *tr = Psnet_stats[i].total_read;
322 *tw = Psnet_stats[i].total_written;
327 void psnet_stats_init()
331 for ( i = 0; i < MAX_NET_STATS; i++ ) {
332 Psnet_stats[i].in_use = 0;
333 Psnet_stats[i].total_read = 0;
334 Psnet_stats[i].total_written = 0;
335 Psnet_socket_addr[i].socket = INVALID_SOCKET;
341 #define NETWORK_STATUS_NOT_INITIALIZED 1
342 #define NETWORK_STATUS_NO_WINSOCK 2 // winsock failed to initialize
343 #define NETWORK_STATUS_NO_TCPIP 3 // TCP/IP doesn't appear to be loaded
344 #define NETWORK_STATUS_RUNNING 4 // everything should be running
346 // function called from high level FreeSpace code to determine the status of the networking
347 // code returns one of a handful of macros
348 int psnet_get_network_status()
350 // first case is when "none" is selected
351 if ( Psnet_connection == NETWORK_CONNECTION_NONE ) {
352 return NETWORK_ERROR_NO_TYPE;
355 // first, check the connection status of the network
356 if ( Network_status == NETWORK_STATUS_NO_WINSOCK )
357 return NETWORK_ERROR_NO_WINSOCK;
359 if ( Network_status == NETWORK_STATUS_NO_TCPIP )
360 return NETWORK_ERROR_NO_TCPIP;
362 // network is running -- be sure that the RAS people know to connect if they currently cannot.
364 if ( Psnet_connection == NETWORK_CONNECTION_DIALUP ) {
365 // if on a dialup connection, be sure that RAS is active.
366 if ( !Ras_connected ) {
367 return NETWORK_ERROR_CONNECT_TO_ISP;
369 } else if ( Psnet_connection == NETWORK_CONNECTION_LAN ) {
370 // if on a LAN, and they have a dialup connection active, return error to indicate that they need
371 // to pick the right connection type
372 if ( Ras_connected ) {
373 return NETWORK_ERROR_LAN_AND_RAS;
376 return NETWORK_ERROR_NONE;
379 DWORD (__stdcall *pRasEnumConnections)(LPRASCONN lprasconn, LPDWORD lpcb, LPDWORD lpcConnections) = NULL;
380 DWORD (__stdcall *pRasGetConnectStatus)(HRASCONN hrasconn, LPRASCONNSTATUS lprasconnstatus ) = NULL;
381 DWORD (__stdcall *pRasGetProjectionInfo)(HRASCONN hrasconn, RASPROJECTION rasprojection, LPVOID lpprojection, LPDWORD lpcb ) = NULL;
383 // functions to get the status of a RAS connection
384 void psnet_ras_status()
387 unsigned long size, num_connections, i;
388 RASCONN rasbuffer[25];
389 HINSTANCE ras_handle;
393 // first, call a LoadLibrary to load the RAS api
394 ras_handle = LoadLibrary( NOX("rasapi32.dll") );
395 if ( ras_handle == NULL ) {
399 pRasEnumConnections = (DWORD (__stdcall *)(LPRASCONN, LPDWORD, LPDWORD))GetProcAddress(ras_handle, NOX("RasEnumConnectionsA"));
400 if (!pRasEnumConnections) {
401 FreeLibrary( ras_handle );
404 pRasGetConnectStatus = (DWORD (__stdcall *)(HRASCONN, LPRASCONNSTATUS))GetProcAddress(ras_handle, NOX("RasGetConnectStatusA"));
405 if (!pRasGetConnectStatus) {
406 FreeLibrary( ras_handle );
409 pRasGetProjectionInfo = (DWORD (__stdcall *)(HRASCONN, RASPROJECTION, LPVOID, LPDWORD))GetProcAddress(ras_handle, NOX("RasGetProjectionInfoA"));
410 if (!pRasGetProjectionInfo) {
411 FreeLibrary( ras_handle );
415 size = sizeof(rasbuffer);
416 rasbuffer[0].dwSize = sizeof(RASCONN);
418 rval = pRasEnumConnections( rasbuffer, &size, &num_connections );
420 FreeLibrary( ras_handle );
424 // JAS: My computer gets to this point, but I have no RAS connections,
426 if ( num_connections < 1 ) {
427 nprintf(("Network", "Found no connections\n" ));
428 FreeLibrary( ras_handle );
432 nprintf(("Network", "Found %d connections\n", num_connections));
434 for (i = 0; i < num_connections; i++ ) {
435 RASCONNSTATUS status;
439 nprintf(("Network", "Connection %d:\n", i));
440 nprintf(("Network", "Entry Name: %s\n", rasbuffer[i].szEntryName));
441 nprintf(("Network", "Device Type: %s\n", rasbuffer[i].szDeviceType));
442 nprintf(("Network", "Device Name: %s\n", rasbuffer[i].szDeviceName));
444 // get the connection status
445 status.dwSize = sizeof(RASCONNSTATUS);
446 rval = pRasGetConnectStatus(rasbuffer[i].hrasconn, &status);
448 FreeLibrary( ras_handle );
452 nprintf(("Network", "\tStatus: %s\n", (status.rasconnstate==RASCS_Connected)?"Connected":"Not Connected"));
454 // get the projection informatiom
455 size = sizeof(projection);
456 projection.dwSize = size;
457 rval = pRasGetProjectionInfo(rasbuffer[i].hrasconn, RASP_PppIp, &projection, &size );
459 FreeLibrary( ras_handle );
463 printf(("Network", "\tIP Address: %s\n", projection.szIpAddress));
468 FreeLibrary( ras_handle );
471 // -------------------------------------------------------------------------------------------------
472 // netmisc_calc_checksum() calculates the checksum of a block of memory.
475 ushort psnet_calc_checksum( void * vptr, int len )
477 ubyte * ptr = (ubyte *)vptr;
478 unsigned int sum1,sum2;
484 if (sum1 >= 255 ) sum1 -= 255;
489 return (unsigned short)((sum1<<8)+ sum2);
492 // -------------------------------------------------------------------------------------------------
493 // psnet_set_socket_mode()
497 uint psnet_set_socket_mode(SOCKET sock_id, int opt, BOOL toggle)
499 return (setsockopt(sock_id, SOL_SOCKET, opt, (LPSTR)&toggle, sizeof(toggle)));
503 // -------------------------------------------------------------------------------------------------
504 // sock_get_ip() returns the IP address of this computer.
510 char LclHost[MAXHOSTNAME];
517 // Init local address to zero
518 LclAddr.sin_addr.s_addr = INADDR_ANY;
520 // Get the local host name
521 nRet = gethostname(LclHost, MAXHOSTNAME );
522 if (nRet != SOCKET_ERROR ) {
523 // Resolve host name for local address
524 Hostent = gethostbyname((LPSTR)LclHost);
526 LclAddr.sin_addr.s_addr = *((u_long FAR *)(Hostent->h_addr));
529 return LclAddr.sin_addr.s_addr;
535 // psnet_get_ip() attempts to get the local IP address of this machine
536 // returns 0 on failure, and 1 on success
537 int psnet_get_ip( SOCKET s )
540 SOCKADDR_IN local_addr;
542 if ( Psnet_my_addr_valid ){
546 memset(&local_addr, 0, sizeof(SOCKADDR_IN));
547 len = sizeof(SOCKADDR_IN);
548 rval = getsockname(s, (SOCKADDR *)&local_addr, &len );
549 if ( rval == SOCKET_ERROR )
552 // now we should have an IP address of this machine.
553 memcpy(Psnet_my_addr.addr, &(local_addr.sin_addr), 4);
554 Psnet_my_addr.port = ntohs(local_addr.sin_port);
556 Psnet_my_addr_valid = 1;
560 // -------------------------------------------------------------------------------------------------
567 #ifndef PSNET_RELIABLE_OLD_SCHOOL
568 psnet_reliable_close();
571 if ( Network_status != NETWORK_STATUS_RUNNING )
574 WSACancelBlockingCall();
576 if ( TCP_listen_socket != INVALID_SOCKET ) {
577 shutdown( TCP_listen_socket, 1 );
578 closesocket( TCP_listen_socket );
581 if ( TCP_reliable_socket != INVALID_SOCKET ) {
582 shutdown( TCP_reliable_socket, 1 );
583 closesocket( TCP_reliable_socket );
586 if ( TCP_socket != INVALID_SOCKET ) {
587 shutdown( TCP_socket, 1 );
588 closesocket( TCP_socket );
592 //Warning( LOCATION, "Error closing wsock!\n" );
595 Network_status = NETWORK_STATUS_NOT_INITIALIZED;
598 // function to initialize a reliable socket. All it does is call the socket() command.
599 // returns 1 on success, 0 on failure.
600 int psnet_init_stream( SOCKET *s, int type )
606 sock = socket(AF_IPX,SOCK_STREAM,NSPROTO_SPX);
607 if ( sock == INVALID_SOCKET )
612 sock = socket(AF_INET,SOCK_STREAM,0);
613 if ( sock == INVALID_SOCKET )
627 // called by psnet_init to initialize the listen socket used by a host/server
628 int psnet_init_reliable(ushort port, int should_listen, int type)
630 SOCKET sock = 0; // JAS: Get rid of optimized warning
631 SOCKADDR_IN sockaddr; // UDP/TCP socket structure
632 SOCKADDR_IPX ipx_addr; // IPX socket structure
634 // set up the reliable TCP transport socket
635 if ( !psnet_init_stream(&sock, type) ) {
636 nprintf(("Network", "Unable to initialize reliable socket on port %d (%d)!\n" , port, WSAGetLastError() ));
637 return INVALID_SOCKET;
640 // bind the socket to the port
643 memset(&ipx_addr, 0, sizeof(SOCKADDR_IPX));
644 ipx_addr.sa_socket = htons( port );
645 ipx_addr.sa_family = AF_IPX;
646 if (bind(sock, (SOCKADDR *)&ipx_addr, sizeof(SOCKADDR_IPX)) == SOCKET_ERROR) {
647 nprintf(("Network", "Unable to bind reliable socket on port %d (%d)!\n" , port, WSAGetLastError() ));
648 return INVALID_SOCKET;
653 memset( &sockaddr, 0, sizeof(SOCKADDR_IN) );
654 sockaddr.sin_family = AF_INET;
655 sockaddr.sin_addr.s_addr = INADDR_ANY; //fill in with our IP
656 sockaddr.sin_port = htons( port );
657 if ( bind(sock, (SOCKADDR*)&sockaddr, sizeof (sockaddr)) == SOCKET_ERROR) {
658 nprintf(("Network", "Unable to bind reliable socket on port %d (%d)!\n" , port, WSAGetLastError() ));
659 return INVALID_SOCKET;
665 return INVALID_SOCKET;
669 // set up the listen on the Reliable_socket port
670 if ( should_listen && listen(sock, 5) ) {
671 nprintf(("Network", "Unable to listen on Listen Socket (%d)!\n" , WSAGetLastError() ));
672 return INVALID_SOCKET;
675 // make any reliable sockets which we create that aren't listening non-blocking sockets
676 if ( !should_listen ) {
681 error = ioctlsocket( sock, FIONBIO, &arg );
682 if ( error == SOCKET_ERROR ) {
683 nprintf(("Network", "Unable to make reliable socket non-blocking -- %d", WSAGetLastError() ));
684 return INVALID_SOCKET;
687 // set the reuse option
688 if ( psnet_set_socket_mode(sock, SO_REUSEADDR, 1) == SOCKET_ERROR ) {
689 error = WSAGetLastError();
690 nprintf(("Network", "Error setting socket to reuse address -- %d\n", error));
698 void psnet_socket_options( SOCKET sock )
701 int ret, cursize, cursizesize, bufsize, trysize;
703 // Set the mode of the socket to allow broadcasting. We need to be able to broadcast
704 // when a game is searched for in IPX mode.
706 if(setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (LPSTR)&broadcast, sizeof(broadcast) )){
712 // try and increase the size of my receive buffer
713 bufsize = MAX_RECEIVE_BUFSIZE;
715 // set the current size of the receive buffer
716 cursizesize = sizeof(int);
717 getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (LPSTR)&cursize, &cursizesize);
718 for ( trysize = bufsize; trysize >= cursize; trysize >>= 1 ) {
719 ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (LPSTR)&trysize, sizeof(trysize));
720 if ( ret == SOCKET_ERROR ) {
723 wserr = WSAGetLastError();
724 if ( (wserr == WSAENOPROTOOPT) || (wserr == WSAEINVAL) )
729 getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (LPSTR)&cursize, &cursizesize);
730 nprintf(("Network", "Receive buffer set to %d\n", cursize));
732 // set the current size of the send buffer
733 cursizesize = sizeof(int);
734 getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (LPSTR)&cursize, &cursizesize);
735 for ( trysize = bufsize; trysize >= cursize; trysize >>= 1 ) {
736 ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (LPSTR)&trysize, sizeof(trysize));
737 if ( ret == SOCKET_ERROR ) {
740 wserr = WSAGetLastError();
741 if ( (wserr == WSAENOPROTOOPT) || (wserr == WSAEINVAL) )
746 getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (LPSTR)&cursize, &cursizesize);
747 nprintf(("Network", "Send buffer set to %d\n", cursize));
751 // psnet_init called at game startup time and initializes all sockets that the game might use (i.e. for both
753 void psnet_init( int protocol, int port_num )
755 char *internet_connection;
758 // UDP/TCP socket structure
759 SOCKADDR_IN sockaddr;
761 // IPX socket structure
762 //SOCKADDR_IPX ipx_addr;
764 #if defined(DEMO) || defined(OEM_BUILD) // not for FS2_DEMO
768 // GAME PORT INITIALIZATION STUFF
769 if ( Network_status == NETWORK_STATUS_RUNNING )
772 internet_connection = os_config_read_string(NULL, "NetworkConnection", "none");
773 if ( !stricmp(internet_connection, NOX("dialup")) ) {
774 Psnet_connection = NETWORK_CONNECTION_DIALUP;
775 } else if ( !stricmp(internet_connection, NOX("lan")) ) {
776 Psnet_connection = NETWORK_CONNECTION_LAN;
778 Psnet_connection = NETWORK_CONNECTION_NONE;
781 Network_status = NETWORK_STATUS_NO_WINSOCK;
782 if (WSAStartup(0x101, &wsa_data )){
786 // get the port for running this game on. Be careful that it cannot be out of bounds
787 Psnet_default_port = DEFAULT_GAME_PORT;
788 if ( (port_num > 1023) && (port_num < USHRT_MAX) ) {
789 Psnet_default_port = (ushort)port_num;
792 #ifndef PSNET_RELIABLE_OLD_SCHOOL
793 if(!psnet_reliable_init()){
794 nprintf(("Network","PSNET RELIABLE : init failed - no networking available!\n"));
799 // initialize TCP now
800 TCP_socket = INVALID_SOCKET;
801 TCP_reliable_socket = INVALID_SOCKET;
802 TCP_listen_socket = INVALID_SOCKET;
804 Network_status = NETWORK_STATUS_NO_TCPIP;
805 TCP_socket = socket( AF_INET, SOCK_DGRAM, 0 );
806 if ( TCP_socket == INVALID_SOCKET ) {
807 Tcp_failure_code = WSAGetLastError();
808 nprintf(("Network", "Error on TCP startup %d\n", Tcp_failure_code));
813 memset(&sockaddr,0,sizeof(SOCKADDR_IN));
814 sockaddr.sin_family = AF_INET;
816 sockaddr.sin_addr.s_addr = INADDR_ANY; //fill in with our IP
818 sockaddr.sin_port = htons( Psnet_default_port );
819 if ( bind(TCP_socket, (SOCKADDR*)&sockaddr, sizeof (sockaddr)) == SOCKET_ERROR) {
820 Tcp_failure_code = WSAGetLastError();
821 nprintf(( "Network", "Couldn't bind TCP socket (%d)! Invalidating TCP\n", Tcp_failure_code ));
825 #ifdef PSNET_RELIABLE_OLD_SCHOOL
826 // set up reliable socket (using SPX).
827 TCP_reliable_socket = psnet_init_reliable( 0, 0, NET_TCP );
828 if ( TCP_reliable_socket == INVALID_SOCKET ) {
829 Tcp_failure_code = WSAGetLastError();
830 nprintf(( "Network", "Couldn't initialize TCP reliable socket (OLD SCHOOL) (%d)! Invalidating TCP\n", Tcp_failure_code ));
834 TCP_listen_socket = psnet_init_reliable( (u_short)(Psnet_default_port-1), 1, NET_TCP );
835 if ( TCP_listen_socket == INVALID_SOCKET ) {
836 Tcp_failure_code = WSAGetLastError();
837 nprintf(( "Network", "Couldn't initialize TCP listen socket (OLD SCHOOL) (%d)! Invalidating TCP\n", Tcp_failure_code ));
842 psnet_socket_options( TCP_socket );
843 Tcp_can_broadcast = Can_broadcast;
846 // fire up a second UDP socket for "reliable" transport
847 #ifndef PSNET_RELIABLE_OLD_SCHOOL
848 TCP_reliable_socket = socket( AF_INET, SOCK_DGRAM, 0 );
849 if ( TCP_reliable_socket != INVALID_SOCKET ) {
851 memset(&sockaddr,0,sizeof(SOCKADDR_IN));
852 sockaddr.sin_family = AF_INET;
854 sockaddr.sin_addr.s_addr = INADDR_ANY; //fill in with our IP
855 //unsigned long my_ip = sock_get_ip();
856 //memcpy(&sockaddr.sin_addr.s_addr,&my_ip,sizeof(uint));
858 sockaddr.sin_port = htons( (ushort)(port+1) );
859 if ( bind(TCP_reliable_socket, (SOCKADDR*)&sockaddr, sizeof (sockaddr)) == SOCKET_ERROR) {
860 nprintf(( "Network", "Couldn't bind TCP socket (%d)! Invalidating TCP\n", WSAGetLastError() ));
864 psnet_socket_options( TCP_reliable_socket );
871 Network_status = NETWORK_STATUS_RUNNING;
873 nprintf(("Network","TCP Initialized\n"));
875 // with TCP initialized, see if the RAS services are installed
879 psnet_frame_int = -1; // for counting bytes per frame
882 #ifdef PSNET_BUFFER_OLD_SCHOOL
886 // initialize the packet buffer stuff. We use an array (the packet_free_list) to determine
887 // which of the packet buffers we can use next.
889 for (i=0; i<MAX_PACKET_BUFFERS; i++ ) {
890 packet_buffers[i].sequence_number = -1;
891 packet_free_list[i] = (short)i;
893 Num_packet_buffers = 0;
894 Largest_packet_index = 0;
896 // initialize the new buffering system
901 #define MAX_TMPBUF_SIZE 1024
903 #ifdef PSNET_RELIABLE_OLD_SCHOOL
904 // function to shutdown and close the given socket. It takes a couple of things into consideration
905 // when closing, such as possibly reiniting reliable sockets if they are closed here.
906 void psnet_rel_close_socket( PSNET_SOCKET *p_sockp )
912 sockp = (SOCKET *)p_sockp;
914 if ( *sockp == INVALID_SOCKET )
918 if ( *sockp == IPX_reliable_socket )
919 nprintf(("Network", "Closing my reliable IPX socket\n"));
920 else if ( *sockp == TCP_reliable_socket )
921 nprintf(("network", "Closing my relibale TCP socket\n"));
923 nprintf(("Network", "Closing client's reliable socket\n"));
926 // make the socket blocking
927 //unsigned long val = 0;
928 //rval = ioctlsocket(*sockp,FIONBIO,&val);
929 //if ( rval == SOCKET_ERROR ) {
930 // error = WSAGetLastError();
931 // nprintf(("Network", "Error %d when trying to make reliable socket blocking.\n", error));
934 // set the socket to linger with a timeout of 0
938 rval = setsockopt(*sockp,SOL_SOCKET,SO_LINGER,(char*)&l_val,sizeof(linger));
939 if ( rval == SOCKET_ERROR ) {
940 error = WSAGetLastError();
941 nprintf(("Network", "Error %d when trying to set linger value.\n", error));
944 // see if this socket is my Reliable_socket. If so, I will have to reinitialize it!
946 if ( *sockp == IPX_reliable_socket )
947 reinit_type = NET_IPX;
948 else if ( *sockp == TCP_reliable_socket )
949 reinit_type = NET_TCP;
952 rval = shutdown( *sockp, 2); // shut the whole damn thing down.
953 if ( rval == SOCKET_ERROR ) {
954 error = WSAGetLastError();
955 nprintf(("Network", "Error %d on socket shutdown\n", error));
959 shutdown( *sockp, 1 );
961 while ( rval && (rval != SOCKET_ERROR) ) {
962 char tmp_buf[MAX_TMPBUF_SIZE];
964 rval = recv( *sockp, tmp_buf, MAX_TMPBUF_SIZE, 0 );
965 if ( rval == SOCKET_ERROR ) {
968 error = WSAGetLastError();
969 if ( error == WSAEWOULDBLOCK )
974 rval = closesocket( *sockp );
975 if ( rval == SOCKET_ERROR ) {
976 error = WSAGetLastError();
977 nprintf(("Network", "Error %d on closing socket\n", error ));
980 *sockp = INVALID_SOCKET;
982 // see if we need to reinitialize
983 if ( reinit_type != -1 ) {
986 port = Psnet_default_port;
987 if ( reinit_type == NET_IPX ){
988 IPX_reliable_socket = psnet_init_reliable( 0, 0, NET_IPX);
989 Reliable_socket = IPX_reliable_socket;
991 else if ( reinit_type == NET_TCP){
992 TCP_reliable_socket = psnet_init_reliable( 0, 0, NET_TCP);
993 Reliable_socket = TCP_reliable_socket;
998 // function to shutdown and close the given socket. It takes a couple of things into consideration
999 // when closing, such as possibly reiniting reliable sockets if they are closed here.
1000 void psnet_close_socket( PSNET_SOCKET *p_sockp )
1004 // copy the address to be removed
1005 memcpy(&remove,&((net_addr*)(*p_sockp)),sizeof(net_addr));
1007 // remove it from the reliable address list
1008 psnet_reliable_notify_drop_addr(&remove);
1010 // invalidate the passed in "socket"
1015 // function to check the status of the reliable socket and try to re-initialize it if necessary.
1016 // win95 seems to have trouble doing a reinit of the socket immediately after close, so this
1017 // function exists to check the status, and reinitialize if we need to
1018 int psnet_rel_check()
1020 // if our Reliable_socket is valid, simply return true to indicate all is well.
1021 if ( Reliable_socket != INVALID_SOCKET )
1024 // based on our protocol type, try to reinitialize the socket we need.
1025 switch( Socket_type ) {
1028 IPX_reliable_socket = psnet_init_reliable( 0, 0, NET_IPX);
1029 Reliable_socket = IPX_reliable_socket;
1033 TCP_reliable_socket = psnet_init_reliable( 0, 0, NET_TCP);
1034 Reliable_socket = TCP_reliable_socket;
1039 // return our status.
1040 if ( Reliable_socket == INVALID_SOCKET )
1046 // this function is called from a high level at FreeSpace to set the protocol that the user wishes to use
1047 int psnet_use_protocol( int protocol )
1050 SOCKADDR_IPX ipx_addr;
1052 // zero out my address
1053 Psnet_my_addr_valid = 0;
1054 memset( &Psnet_my_addr, 0, sizeof(Psnet_my_addr) );
1056 // wait until we choose a protocol to determine if we can broadcast
1059 switch ( protocol ) {
1063 // assign the IPX_* sockets to the socket values used elsewhere
1064 Unreliable_socket = IPX_socket;
1065 Reliable_socket = IPX_reliable_socket;
1066 Listen_socket = IPX_listen_socket;
1068 Can_broadcast = Ipx_can_broadcast;
1070 nprintf(("Network","Psnet : IPX broadcast\n"));
1073 // get the socket name for the IPX_socket, and put it into My_addr
1074 len = sizeof(SOCKADDR_IPX);
1075 if ( getsockname(IPX_socket, (SOCKADDR *)&ipx_addr, &len) == SOCKET_ERROR ) {
1076 nprintf(("Network", "Unable to get sock name for IPX unreliable socket (%d)\n", WSAGetLastError() ));
1080 memcpy(Psnet_my_addr.net_id, ipx_addr.sa_netnum, 4);
1081 memcpy(Psnet_my_addr.addr, ipx_addr.sa_nodenum, 6);
1082 Psnet_my_addr.port = DEFAULT_GAME_PORT;
1084 nprintf(("Network","Psnet using - NET_IPX\n"));
1088 if ( Network_status != NETWORK_STATUS_RUNNING )
1091 // assign the TCP_* sockets to the socket values used elsewhere
1092 Unreliable_socket = TCP_socket;
1093 Reliable_socket = TCP_reliable_socket;
1094 Listen_socket = TCP_listen_socket;
1096 Can_broadcast = Tcp_can_broadcast;
1098 nprintf(("Network","Psnet : TCP broadcast\n"));
1101 nprintf(("Network","Psnet using - NET_TCP\n"));
1109 Psnet_my_addr.type = protocol;
1110 Socket_type = protocol;
1115 #define MAX_CONNECT_TRIES 10
1117 // attacmpt to connect() to the server's tcp socket. socket parameter is simply assigned to the
1118 // Reliable_socket socket created in psnet_init
1119 void psnet_rel_connect_to_server( PSNET_SOCKET *psocket, net_addr *server_addr)
1121 SOCKADDR_IN sockaddr; // UDP/TCP socket structure
1122 SOCKADDR_IPX ipx_addr; // IPX socket structure
1123 SOCKADDR *addr; // pointer to SOCKADDR to make coding easier
1127 int name_length, val;
1129 memset(iaddr, 0x00, 6);
1130 memcpy(iaddr, server_addr->addr, 6);
1131 port = (u_short)(server_addr->port - 1);
1133 socket = (SOCKET *)psocket;
1135 switch( Socket_type ) {
1137 ipx_addr.sa_family = AF_IPX;
1138 memcpy(ipx_addr.sa_nodenum, iaddr, 6);
1139 memcpy(ipx_addr.sa_netnum, server_addr->net_id, 4);
1140 ipx_addr.sa_socket = htons(port);
1141 addr = (SOCKADDR *)&ipx_addr;
1142 name_length = sizeof(ipx_addr);
1146 sockaddr.sin_family = AF_INET;
1147 memcpy(&sockaddr.sin_addr.s_addr, iaddr, 4);
1148 sockaddr.sin_port = htons(port);
1149 addr = (SOCKADDR *)&sockaddr;
1150 name_length = sizeof(sockaddr);
1159 ml_string(NOX("Attempting to perform reliable TCP connect to server"));
1161 // connect this guys Reliable_socket to the socket parameter passed in
1162 *socket = INVALID_SOCKET;
1163 val = connect(Reliable_socket, addr, name_length);
1165 // I suppose that we could have a valid socket right away
1166 if ( val != SOCKET_ERROR ) {
1167 *socket = Reliable_socket;
1171 error = WSAGetLastError();
1172 if ( error == WSAEWOULDBLOCK ) { // use select to find out when socket is writeable
1173 int is_set, num_tries;
1175 struct timeval timeout;
1177 // call select -- and return if an error leaving our connection to the client invalid
1181 FD_SET( Reliable_socket, &wfds );
1183 timeout.tv_usec = 500000; //500000 micro seconds is 1/2 second
1185 is_set = select( -1, NULL, &wfds, NULL, &timeout);
1186 // check for error on select first
1187 if ( is_set == SOCKET_ERROR ) {
1188 nprintf(("Network", "Error on select for connect %d\n", WSAGetLastError() ));
1190 } else if (is_set) { // if set, then we have connection, move forward
1193 Sleep(10); // sleep for 10 ms and try again
1196 } while ( num_tries < MAX_CONNECT_TRIES );
1198 if ( num_tries == MAX_CONNECT_TRIES )
1201 *socket = Reliable_socket;
1204 nprintf(("Network", "Connecting with server failed %d\n", error ));
1205 *socket = INVALID_SOCKET;
1209 // if we have a connection, get my address from getsockname
1210 if ( *socket == Reliable_socket ) {
1211 if ( !psnet_get_ip(Reliable_socket) ) {
1212 *socket = INVALID_SOCKET;
1219 void psnet_calc_bytes_per_frame()
1224 // -------------------------------------------------------------------------------------------------
1229 int psnet_same_no_port( net_addr * a1, net_addr * a2 )
1231 if ( !memcmp(a1->addr, a2->addr, 6) )
1236 int psnet_same( net_addr * a1, net_addr * a2 )
1238 return psnet_same_no_port( a1, a2 );
1240 if ( !memcmp(a1->addr, a2->addr, 6) && a1->port == a2->port)
1246 // -------------------------------------------------------------------------------------------------
1251 char* psnet_addr_to_string( char * text, net_addr * address )
1254 if ( Network_status != NETWORK_STATUS_RUNNING ) {
1255 strcpy( text, XSTR("[no networking]",910) );
1261 switch ( address->type ) {
1263 sprintf(text, "%x %x %x %x: %x %x %x %x %x %x", address->net_id[0],
1276 memcpy(&temp_addr.s_addr, address->addr, 4);
1277 strcpy( text, inet_ntoa(temp_addr) );
1290 // -------------------------------------------------------------------------------------------------
1291 // psnet_undescribe()
1295 void psnet_string_to_addr( net_addr * address, char * text )
1298 char str[255], *c, *port;
1301 if ( Network_status != NETWORK_STATUS_RUNNING ) {
1302 strcpy( text, XSTR("[no networking]",910) );
1306 // copy the text string to local storage to look for ports
1307 Assert( strlen(text) < 255 );
1309 c = strrchr(str, ':');
1316 switch ( address->type ) {
1318 Int3(); // no support for this yet
1322 addr.s_addr = inet_addr(str);
1323 // if we get INADDR_NONE returns, then we need to try and resolve the host
1325 if ( addr.s_addr == INADDR_NONE ) {
1326 he = gethostbyname( str );
1327 // returns a non-null pointer if successful, so get the address
1329 addr.s_addr = ((in_addr *)(he->h_addr))->s_addr; // this is the address in network byte order
1331 addr.s_addr = INADDR_NONE;
1335 memset(address->addr, 0x00, 6);
1336 memcpy(address->addr, &addr.s_addr, 4);
1338 address->port = (ushort)(atoi(port));
1349 // psnet_get_socket_data() will get data out of the socket and stuff it into the packet_buffers
1350 // The original psnet_get() now calls this function, then determines which of the packet buffers
1351 // to package up and use
1352 void psnet_get_socket_data(SOCKET socket, int flags = PSNET_FLAG_RAW)
1354 SOCKADDR_IN ip_addr; // UDP/TCP socket structure
1355 SOCKADDR_IPX ipx_addr; // IPX socket structure
1358 int read_len, from_len;
1360 network_checksum_packet packet_read;
1361 network_checksum_packet packet_data;
1363 // clear the addresses to remove compiler warnings
1364 memset(&ip_addr, 0, sizeof(SOCKADDR_IN));
1365 memset(&ipx_addr, 0, sizeof(SOCKADDR_IPX));
1367 if ( Network_status != NETWORK_STATUS_RUNNING ) {
1368 nprintf(("Network","Network ==> socket not inited in psnet_get\n"));
1373 #ifdef PSNET_BUFFER_OLD_SCHOOL
1375 // if there are no more packet buffers that we can use, then we must bail here before reading
1377 if ( Num_packet_buffers >= MAX_PACKET_BUFFERS ) {
1378 nprintf(("Network", "Packet buffer overrun in psnet_get()\n"));
1383 // check if there is any data on the socket to be read. The amount of data that can be
1384 // atomically read is stored in len.
1387 FD_SET( socket, &rfds );
1389 timeout.tv_usec = 0;
1391 if ( select( -1, &rfds, NULL, NULL, &timeout) == SOCKET_ERROR ) {
1392 nprintf(("Network", "Error %d doing a socket select on read\n", WSAGetLastError()));
1396 // if the read file descriptor is not set, then bail!
1397 if ( !FD_ISSET(socket, &rfds ) )
1400 // get data off the socket and process
1401 read_len = SOCKET_ERROR;
1402 switch ( Socket_type ) {
1404 from_len = sizeof(SOCKADDR_IPX);
1405 if(flags & PSNET_FLAG_RAW){
1406 read_len = recvfrom( socket, (char*)packet_read.data, MAX_PACKET_SIZE, 0, (SOCKADDR*)&ipx_addr, &from_len );
1408 read_len = recvfrom( socket, (char *)&packet_read, sizeof(packet_data), 0, (SOCKADDR*)&ipx_addr, &from_len );
1413 from_len = sizeof(SOCKADDR_IN);
1414 if(flags & PSNET_FLAG_RAW){
1415 read_len = recvfrom( socket, (char*)packet_read.data, MAX_PACKET_SIZE, 0, (SOCKADDR*)&ip_addr, &from_len );
1417 read_len = recvfrom( socket, (char *)&packet_read, sizeof(packet_data), 0, (SOCKADDR*)&ip_addr, &from_len );
1426 // set the from_addr for storage into the packet buffer structure
1427 from_addr.type = Socket_type;
1429 switch ( Socket_type ) {
1431 if(socket == Reliable_socket){
1432 from_addr.port = Psnet_default_port;
1434 from_addr.port = ntohs( ipx_addr.sa_socket );
1436 memcpy(from_addr.addr, ipx_addr.sa_nodenum, 6 );
1437 memcpy(from_addr.net_id, ipx_addr.sa_netnum, 4 );
1441 if(socket == Reliable_socket){
1442 from_addr.port = Psnet_default_port;
1444 from_addr.port = ntohs( ip_addr.sin_port );
1446 memset(from_addr.addr, 0x00, 6);
1447 memcpy(from_addr.addr, &ip_addr.sin_addr.S_un.S_addr, 4);
1455 if ( read_len == SOCKET_ERROR ) {
1456 int x = WSAGetLastError();
1457 nprintf(("Network", "Read error on socket. Winsock error %d \n", x));
1461 #ifndef PSNET_RELIABLE_OLD_SCHOOL
1463 // now we check to see if this is a reliable packet, and act accordindly
1464 if(socket == Reliable_socket){
1465 // this function processes the incoming packet, and determines if the system should continue to process the data
1466 shave_size = psnet_reliable_should_process(&from_addr,packet_read.data,read_len);
1470 // copy in from data+2, so we skip the reliable data header
1471 memcpy(packet_data.data,packet_read.data + shave_size,read_len);
1473 // subtract out the size of the reliable header
1474 read_len -= shave_size;
1477 memcpy(packet_data.data,packet_read.data,read_len);
1480 memcpy(packet_data.data,packet_read.data,read_len);
1483 #ifdef PSNET_BUFFER_OLD_SCHOOL
1487 // if we had no error reading from the socket, then determine if we need to calculate a
1488 // checksum on the packet
1489 if ( !(flags & PSNET_FLAG_RAW) && (packet_data.flags & PSNET_FLAG_CHECKSUM) ) {
1492 // calculate the size of the data that is actual real data
1493 len = read_len - (sizeof(packet_data) - MAX_CHECKSUM_PACKET_SIZE);
1494 checksum = psnet_calc_checksum( packet_data.data, len );
1495 if ( checksum != packet_data.checksum ) {
1496 nprintf(("Network", "bad checksum on incoming packet -- discarding\n"));
1499 data = packet_data.data;
1500 packet_id = packet_data.sequence_number;
1502 network_naked_packet *packet;
1504 // just read in the raw socket data and length
1505 if(flags & PSNET_FLAG_RAW){
1506 data = packet_data.data;
1511 // we don't have a checksum packet. cast the data to the naked packet type and
1512 // copy it over to passed parameters
1513 packet = (network_naked_packet *)&packet_data;
1515 len = read_len - (sizeof(network_naked_packet) - MAX_PACKET_SIZE);
1516 data = packet->data;
1518 packet_id = packet->sequence_number;
1522 // look at the sequence number compared to what we last received
1523 if ( Last_packet_id > -1 ) {
1524 if ( packet_id != (Last_packet_id+1) ) {
1525 //if ( packet_id < Last_packet_id )
1526 // nprintf(("network", "packet %d came in delayed (last id was %d\n", packet_id, Last_packet_id));
1527 //else if ( packet_id > (Last_packet_id+1) )
1528 // nprintf(("Network", "missed %d packet(s). last id %d. This id %d\n", (packet_id - Last_packet_id), Last_packet_id, packet_id));
1531 Last_packet_id = packet_id;
1534 psnet_do_net_stats( &from_addr, read_len, 1 );
1535 psnet_bytes_read_frame += read_len;
1538 // put all of the data (length, who from, etc.) into the next available packet buffer
1539 // slot. We should be assured of a slot here because of the check at the beginning
1540 // of the while loop
1541 Assert ( Num_packet_buffers < MAX_PACKET_BUFFERS );
1542 id = packet_free_list[ Num_packet_buffers++ ];
1543 if (id > Largest_packet_index ) Largest_packet_index = id;
1544 packet_buffers[id].len = len; // use the flags field of the packet structure to hold the data length
1545 packet_buffers[id].sequence_number = packet_id;
1546 packet_buffers[id].from_addr = from_addr;
1547 memcpy( packet_buffers[id].data, data, len );
1550 psnet_do_net_stats( &from_addr, read_len, 1 );
1551 psnet_bytes_read_frame += read_len;
1554 // buffer the packet
1555 psnet_buffer_packet(packet_data.data,read_len,&from_addr);
1560 // -------------------------------------------------------------------------------------------------
1565 int psnet_send( net_addr * who_to, void * data, int len, int flags, int reliable_socket )
1569 SOCKADDR_IN sockaddr; // UDP/TCP socket structure
1570 SOCKADDR_IPX ipx_addr; // IPX socket structure
1572 ubyte iaddr[6], *send_data;
1575 struct timeval timeout;
1577 if(!reliable_socket){
1578 send_sock = Unreliable_socket;
1580 send_sock = Reliable_socket;
1583 if ( Network_status != NETWORK_STATUS_RUNNING ) {
1584 nprintf(("Network","Network ==> Socket not inited in psnet_send\n"));
1588 if ( psnet_same( who_to, &Psnet_my_addr) ){
1592 memset(iaddr, 0x00, 6);
1593 memcpy(iaddr, who_to->addr, 6);
1595 if ( memcmp(iaddr, Null_address, 6) == 0) {
1596 nprintf(("Network","Network ==> send to address is 0 in psnet_send\n"));
1600 if(send_sock == Unreliable_socket){
1601 port = who_to->port;
1602 } else if(send_sock == Reliable_socket){
1603 port = DEFAULT_GAME_PORT + 1;
1605 port = who_to->port;
1609 nprintf(("Network","Network ==> destination port %d invalid in psnet_send\n", port));
1613 #ifdef PSNET_BUFFER_OLD_SCHOOL
1614 network_checksum_packet Send_network_checksum_packet;
1615 network_naked_packet Send_network_naked_packet;
1617 // determine from the flags whether or not this packet should have a checksum.
1618 if ( flags & PSNET_FLAG_CHECKSUM ) {
1619 // can't send raw data with a checksum, dumbass!
1620 Assert(!(flags & PSNET_FLAG_RAW));
1622 Send_network_checksum_packet.sequence_number = Next_packet_id++;
1623 Send_network_checksum_packet.flags = PSNET_FLAG_CHECKSUM;
1625 Send_network_checksum_packet.checksum = psnet_calc_checksum(data, len);
1626 memcpy( Send_network_checksum_packet.data, data, len );
1627 send_len = sizeof(Send_network_checksum_packet) - MAX_CHECKSUM_PACKET_SIZE + len;
1628 send_data = (ubyte *)&Send_network_checksum_packet;
1630 // send standard psnet stuff
1631 if(!(flags & PSNET_FLAG_RAW)){
1632 Send_network_naked_packet.sequence_number = Next_packet_id++;
1633 Send_network_naked_packet.flags = 0;
1635 memcpy(Send_network_naked_packet.data, data, len);
1636 send_len = sizeof(Send_network_naked_packet) - MAX_PACKET_SIZE + len; // gets us the real size of the structure
1637 send_data = (ubyte *)&Send_network_naked_packet;
1641 send_data = (ubyte*)data;
1646 send_data = (ubyte*)data;
1652 FD_SET( send_sock, &wfds );
1654 timeout.tv_usec = 0;
1656 if ( select( -1, NULL, &wfds, NULL, &timeout) == SOCKET_ERROR ) {
1657 nprintf(("Network", "Error on blocking select for write %d\n", WSAGetLastError() ));
1661 // if the write file descriptor is not set, then bail!
1662 if ( !FD_ISSET(send_sock, &wfds ) )
1666 switch ( who_to->type ) {
1669 ipx_addr.sa_socket = htons(port);
1670 ipx_addr.sa_family = AF_IPX;
1671 memcpy(ipx_addr.sa_nodenum, iaddr, 6);
1672 memcpy(ipx_addr.sa_netnum, who_to->net_id, 4);
1674 ret = sendto(send_sock, (char *)send_data, send_len, 0, (SOCKADDR*)&ipx_addr, sizeof(ipx_addr));
1675 if ( (ret != SOCKET_ERROR) && (ret != send_len) )
1676 nprintf(("Network", "requested to send %d bytes -- sent %d instead!!!\n", send_len, ret));
1682 sockaddr.sin_family = AF_INET;
1683 memcpy(&sockaddr.sin_addr.s_addr, iaddr, 4);
1684 sockaddr.sin_port = htons(port);
1686 ret = sendto( send_sock, (char *)send_data, send_len, 0, (SOCKADDR*)&sockaddr, sizeof(sockaddr) );
1690 Assert(0); // unknown protocol
1695 if ( ret != SOCKET_ERROR ) {
1697 psnet_bytes_written_frame += send_len;
1698 psnet_do_net_stats( who_to, send_len, 0 );
1702 //Warning( LOCATION, "Couldn't send data (0x%x)!\n", WSAGetLastError() );
1706 #ifdef PSNET_BUFFER_OLD_SCHOOL
1707 // routine to "free" a packet buffer
1708 void free_packet( int id )
1710 packet_buffers[id].sequence_number = -1;
1711 packet_free_list[ --Num_packet_buffers ] = (short)id;
1712 if ( Largest_packet_index == id)
1713 while ((--Largest_packet_index>0) && (packet_buffers[Largest_packet_index].sequence_number == -1 ));
1717 #ifdef PSNET_RELIABLE_OLD_SCHOOL
1718 // psnet_send_reliable sends the given data through the given reliable socket.
1720 #define MAX_RSEND_BUFFER 2048
1721 ubyte rsend_buffer[MAX_RSEND_BUFFER];
1723 int psnet_rel_send( PSNET_SOCKET psocket, ubyte *data, int length, int flags )
1726 int num_sent, total_sent, error, retries;
1728 unsigned short s_length;
1730 socket = (SOCKET)psocket;
1733 if ( Network_status != NETWORK_STATUS_RUNNING ) {
1734 nprintf(("Network","Network ==> Socket not inited in psnet_send\n"));
1738 if ( socket == INVALID_SOCKET ) // might happen in race conditions -- should get cleaned up.
1741 Assert( length < MAX_RSEND_BUFFER );
1743 // copy the length of the data into the beginning of the buffer. then put the data into the buffer
1744 // after the length value
1745 Assert( length > 0 );
1746 s_length = (ushort)length;
1747 memcpy( &rsend_buffer[0], &s_length, sizeof(s_length) );
1748 memcpy( &rsend_buffer[2], data, length );
1754 num_sent = send( socket, (char *)rsend_buffer, length+sizeof(s_length), 0 );
1755 if ( num_sent == SOCKET_ERROR ) {
1756 error = WSAGetLastError();
1757 if ( (error != WSAEWOULDBLOCK) || (retries > MAX_SEND_RETRIES) ) { // means that we would block on send -- not really an error
1759 // if error is would block, then set error to aborted connection
1760 if ( error == WSAEWOULDBLOCK )
1761 error = WSAECONNABORTED;
1763 multi_eval_socket_error(socket, error);
1766 retries++; // keep a try count
1769 send_data += num_sent;
1770 total_sent += num_sent;
1772 } while ( length > 0 );
1775 psnet_bytes_written_frame += total_sent;
1778 //index = psnet_find_stats_loc( who_to );
1779 //if ( index != -1 )
1780 // Psnet_stats[index].total_written += send_len;
1785 // get data off the reliable socket
1786 int psnet_rel_get( PSNET_SOCKET psocket, ubyte *buffer, int max_len, int flags)
1788 int from_len, total_read, error;
1791 struct timeval timeout;
1793 ubyte rread_buffer[2];
1795 socket = (SOCKET)psocket;
1797 // see if there is data to be read
1799 FD_SET( socket, &rfds );
1801 timeout.tv_usec = 0;
1803 if ( select( -1, &rfds, NULL, NULL, &timeout) == SOCKET_ERROR ) {
1804 nprintf(("Network", "Error on select for read reliable: %d\n", WSAGetLastError() ));
1808 // if no data, then we can leave.
1809 if ( !FD_ISSET(socket, &rfds ) )
1812 // we know we have data to read. We must read the two byte length of the packet first
1816 from_len = recv(socket, (char *)(&rread_buffer[total_read]), read_len - total_read, 0);
1818 // from_len will be 0 when back end gracefully closes connection. We will assume that since
1819 // the close is graceful, we will info from him telling us he's left. So we'll ignore
1820 // this condition here.
1821 if ( from_len == 0 ) {
1822 nprintf(("Network", "Dumping player because recv returned 0\n"));
1823 multi_eval_socket_error( socket, WSAECONNRESET ); // this error drops player from game.
1827 // on a socket error, we need to check for WSAEWOULDBLOCK meaning that there is no more
1829 else if ( from_len == SOCKET_ERROR ) {
1830 error = WSAGetLastError();
1831 if ( error != WSAEWOULDBLOCK )
1832 multi_eval_socket_error( socket, error );
1833 return 0; // get it next frame?
1836 total_read += from_len;
1837 } while ( total_read < read_len );
1841 memcpy(&read_len, &rread_buffer[0], 2);
1842 Assert( (read_len > 0) && (read_len < max_len) );
1843 if ( read_len == 0 )
1847 from_len = recv(socket, (char *)(buffer + total_read), read_len - total_read, 0);
1849 // from_len will be 0 when back end gracefully closes connection. We will assume that since
1850 // the close is graceful, we will info from him telling us he's left. So we'll ignore
1851 // this condition here.
1852 if ( from_len == 0 ) {
1853 nprintf(("Network", "Dumping player because recv returned 0\n"));
1854 multi_eval_socket_error( socket, WSAECONNRESET ); // this error drops player from game.
1858 // on a socket error, we need to check for WSAEWOULDBLOCK meaning that there is no more
1860 else if ( from_len == SOCKET_ERROR ) {
1861 error = WSAGetLastError();
1862 if ( error != WSAEWOULDBLOCK ) {
1863 multi_eval_socket_error( socket, error );
1869 total_read += from_len;
1870 } while ( total_read < read_len );
1875 // psnet_send_reliable sends the given data through the given reliable socket.
1876 int psnet_send_reliable( PSNET_SOCKET psocket, ubyte *data, int length, int flags )
1878 // don't do anything if the socket is null
1879 if(psocket == NULL){
1883 // send a reliable data packet
1884 return psnet_reliable_send(data,length,(net_addr*)psocket);
1887 int psnet_get_reliable( PSNET_SOCKET psocket, ubyte *buffer, int max_len, int flags)
1890 int best, best_id, i, n, size;
1892 // call the routine to read data out of the socket (which stuffs it into the packet buffers)
1893 psnet_get_socket_data(Reliable_socket,flags);
1895 #ifdef PSNET_BUFFER_OLD_SCHOOL
1896 // now determine which (if any) of the packet buffers we should look at!
1901 for (i=0; i <= Largest_packet_index; i++ ) {
1902 if ( packet_buffers[i].sequence_number > -1 ) {
1904 if ( best == -1 || (packet_buffers[i].sequence_number < best) ) {
1905 best = packet_buffers[i].sequence_number;
1911 //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
1912 //mprintf( (1, "<%d> ", neterrors ));
1914 if ( best_id < 0 ) return 0;
1916 size = packet_buffers[best_id].len;
1917 memcpy( buffer, packet_buffers[best_id].data, size );
1918 memcpy( psocket, &packet_buffers[best_id].from_addr, sizeof(net_addr) );
1919 free_packet(best_id);
1928 // function which checks the Listen_socket for possibly incoming requests to be connected.
1929 // returns 0 on error or nothing waiting. 1 if we should try to accept
1930 int psnet_rel_check_for_listen(net_addr *from_addr)
1934 SOCKET sock; // when trying to accept, this is new socket
1935 SOCKADDR_IN ip_addr; // UDP/TCP socket structure
1936 SOCKADDR_IPX ipx_addr; // IPX socket structure
1937 int from_len, error;
1941 FD_SET( Listen_socket, &rfds );
1943 timeout.tv_usec = 0;
1944 if ( select(-1, &rfds, NULL, NULL, &timeout) == SOCKET_ERROR ) {
1945 nprintf(("Network", "Error %d doing select on listen socket\n", WSAGetLastError() ));
1949 // check to see if Listen_socket has something -- if not, return
1950 if ( !FD_ISSET(Listen_socket, &rfds) )
1951 return INVALID_SOCKET;
1953 sock = INVALID_SOCKET;
1954 switch ( Socket_type ) {
1956 from_len = sizeof(SOCKADDR_IPX);
1957 sock = accept( Listen_socket, (SOCKADDR*)&ipx_addr, &from_len );
1958 from_addr->port = ntohs( ipx_addr.sa_socket );
1959 memcpy(from_addr->addr, ipx_addr.sa_nodenum, 6 );
1960 memcpy(from_addr->net_id, ipx_addr.sa_netnum, 4 );
1961 nprintf(("Network","Accepted SPX connection!!\n"));
1965 from_len = sizeof(SOCKADDR_IN);
1966 sock = accept( Listen_socket, (SOCKADDR*)&ip_addr, &from_len );
1967 from_addr->port = ntohs( ip_addr.sin_port );
1968 memset(from_addr->addr, 0x00, 6);
1969 memcpy(from_addr->addr, &ip_addr.sin_addr.S_un.S_addr, 4);
1970 nprintf(("Network","Accepted TCP connected!!\n"));
1978 if ( !psnet_get_ip(sock) ) {
1979 return INVALID_SOCKET;
1982 // make the new socket non-blocking
1983 if(sock != INVALID_SOCKET){
1985 error = ioctlsocket( sock, FIONBIO, &arg );
1986 if ( error == SOCKET_ERROR ) {
1987 nprintf(("Network", "Unable to make accepted socket non-blocking -- %d", WSAGetLastError() ));
1988 return INVALID_SOCKET;
1996 // psnet_get() will call the above function to read data out of the socket. It will then determine
1997 // which of the buffers we should use and pass to the routine which called us
1998 int psnet_get( void * data, net_addr* from_addr, int flags )
2000 // USE THIS CODE TO TEST NON-BUFFERED SOCKET READS. OUT-OF-ORDER PACKETS DROP TO NEARLY 0
2005 int read_len,from_len;
2006 SOCKADDR_IN ip_addr;
2008 FD_SET( Unreliable_socket, &rfds );
2010 timeout.tv_usec = 0;
2012 if ( select( -1, &rfds, NULL, NULL, &timeout) == SOCKET_ERROR ) {
2013 nprintf(("Network", "Error %d doing a socket select on read\n", WSAGetLastError()));
2017 // if the read file descriptor is not set, then bail!
2018 if ( !FD_ISSET(Unreliable_socket, &rfds ) )
2021 // get data off the socket and process
2022 read_len = SOCKET_ERROR;
2023 from_len = sizeof(SOCKADDR_IN);
2024 read_len = recvfrom( Unreliable_socket, (char*)data, MAX_PACKET_SIZE, 0, (SOCKADDR*)&ip_addr, &from_len );
2025 from_addr->port = ntohs( ip_addr.sin_port );
2026 memset(from_addr->addr, 0x00, 6);
2027 memcpy(from_addr->addr, &ip_addr.sin_addr.S_un.S_addr, 4);
2031 // call the routine to read data out of the socket (which stuffs it into the packet buffers)
2032 psnet_get_socket_data(Unreliable_socket,flags);
2034 #ifdef PSNET_BUFFER_OLD_SCHOOL
2035 int best, best_id, i, n, size;
2037 // now determine which (if any) of the packet buffers we should look at!
2042 for (i=0; i <= Largest_packet_index; i++ ) {
2043 if ( packet_buffers[i].sequence_number > -1 ) {
2045 if ( best == -1 || (packet_buffers[i].sequence_number < best) ) {
2046 best = packet_buffers[i].sequence_number;
2052 //mprintf( (0, "Best id = %d, pn = %d, last_ecb = %x, len=%x, ne = %d\n", best_id, best, last_ecb, lastlen, neterrors ));
2053 //mprintf( (1, "<%d> ", neterrors ));
2055 if ( best_id < 0 ) return 0;
2057 size = packet_buffers[best_id].len;
2058 memcpy( data, packet_buffers[best_id].data, size );
2059 memcpy( from_addr, &packet_buffers[best_id].from_addr, sizeof(net_addr) );
2060 free_packet(best_id);
2066 // try and get a free buffer and return its size
2067 if(psnet_buffer_get_next((ubyte*)data,&buffer_size,from_addr)){
2078 // -------------------------------------------------------------------------------------------------
2079 // psnet_broadcast()
2083 int psnet_broadcast( net_addr * who_to, void * data, int len, int flags )
2085 if ( Network_status != NETWORK_STATUS_RUNNING ) {
2086 nprintf(("Network","Network ==> Socket not inited in psnet_broadcast\n"));
2090 if ( !Can_broadcast ) {
2091 nprintf(("Network", "Cannot broadcast -- returning without doing anything\n"));
2095 ubyte broadcast[6] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
2097 // broadcasting works on a local subnet which is all we really want to do for now anyway.
2098 // we might keep this in as an option for freespace later.
2099 switch ( who_to->type ) {
2103 memcpy(who_to->addr, broadcast, 6);
2104 psnet_send(who_to, data, len, flags);
2112 // called to clear out the socket of any remaining data
2116 ubyte data[MAX_PACKET_SIZE];
2119 while ( psnet_get( data, &from_addr ) > 0 ) ;
2125 // function to keep track of bytes read/written on average during the frame
2126 void psnet_calc_socket_stats()
2128 if ( psnet_frame_int == -1 ) {
2130 for ( i = 0; i < PSNET_FRAME_FILTER; i++ ) {
2131 psnet_read_sizes[i] = 0;
2132 psnet_write_sizes[i] = 0;
2134 psnet_read_total = 0;
2135 psnet_write_total = 0;
2136 psnet_frame_int = 0;
2138 psnet_read_total -= psnet_read_sizes[psnet_frame_int];
2139 psnet_write_total -= psnet_write_sizes[psnet_frame_int];
2141 psnet_read_total += psnet_bytes_read_frame;
2142 psnet_write_total += psnet_bytes_written_frame;
2144 psnet_read_sizes[psnet_frame_int] = psnet_bytes_read_frame;
2145 psnet_write_sizes[psnet_frame_int] = psnet_bytes_written_frame;
2147 psnet_frame_int = (psnet_frame_int + 1 ) % PSNET_FRAME_FILTER;
2149 if ( psnet_frame_count > 0 ) {
2150 if ( psnet_frame_count >= PSNET_FRAME_FILTER )
2151 Psnet_bytes_read = psnet_read_total / PSNET_FRAME_FILTER;
2153 Psnet_bytes_read = psnet_read_total / psnet_frame_count;
2155 if ( psnet_frame_count >= PSNET_FRAME_FILTER )
2156 Psnet_bytes_written = psnet_write_total / PSNET_FRAME_FILTER;
2158 Psnet_bytes_written = psnet_write_total / psnet_frame_count;
2161 psnet_frame_count++;
2162 psnet_bytes_read_frame = 0;
2163 psnet_bytes_written_frame = 0;
2167 int psnet_is_valid_numeric_ip(char *ip)
2171 int val1,val2,val3,val4;
2173 // get the first ip value
2175 token = strtok(copy,".");
2179 // get the value of the token
2181 if((val1 < 0) || (val1 > 255)){
2187 token = strtok(NULL,".");
2191 // get the value of the token
2193 if((val2 < 0) || (val2 > 255)){
2199 token = strtok(NULL,".");
2203 // get the value of the token
2205 if((val3 < 0) || (val3 > 255)){
2211 token = strtok(NULL,"");
2215 // get the value of the token
2217 if((val4 < 0) || (val4 > 255)){
2222 // make sure he hasn't entered all 0's
2223 if((val1 == 0) && (val2 == 0) && (val3 == 0) && (val4 == 0)){
2232 // returns true or false if the given string is a valid ip string or not.
2233 // allow port allows us to append the port number on the end of the ip string with
2235 // so we must be sure to remove the port number
2236 int psnet_is_valid_ip_string( char *ip_string, int allow_port )
2239 struct hostent *host_ent;
2242 // our addresses may have ports, so make local copy and remove port number
2243 Assert( strlen(ip_string) < 255 );
2244 strcpy(str, ip_string);
2245 c = strrchr(str, ':');
2250 addr.s_addr = inet_addr(ip_string);
2251 if ( addr.s_addr != INADDR_NONE ){
2252 // make sure the ip string is a valid format string
2253 if(psnet_is_valid_numeric_ip(ip_string)){
2258 // try name resolution
2259 host_ent = gethostbyname( ip_string );
2264 // valid host entry so return 1;
2269 // ------------------------------------------------------------------------------------------------------
2270 // PACKET BUFFERING FUNCTIONS
2273 #ifndef PSNET_BUFFER_OLD_SCHOOL
2275 // a sequence number of -1 will indicate that this packet is not valid
2276 LOCAL network_packet_buffer Psnet_buffers[MAX_PACKET_BUFFERS];
2277 LOCAL int Psnet_seq_number = 0;
2278 LOCAL int Psnet_lowest_id = 0;
2279 LOCAL int Psnet_highest_id = 0;
2281 // initialize the buffering system
2282 void psnet_buffer_init()
2286 // blast the buffer clean
2287 memset(Psnet_buffers,0,sizeof(network_packet_buffer) * MAX_PACKET_BUFFERS);
2289 // set all buffer sequence #'s to -1
2290 for(idx=0;idx<MAX_PACKET_BUFFERS;idx++){
2291 Psnet_buffers[idx].sequence_number = -1;
2294 // initialize the sequence #
2295 Psnet_seq_number = 0;
2296 Psnet_lowest_id = -1;
2297 Psnet_highest_id = -1;
2300 // buffer a packet (maintain order!)
2301 void psnet_buffer_packet(ubyte *data, int length, net_addr *from)
2306 // find the first empty packet
2307 for(idx=0;idx<MAX_PACKET_BUFFERS;idx++){
2308 if(Psnet_buffers[idx].sequence_number == -1){
2314 // if we didn't find the buffer, report an overrun
2316 nprintf(("Network","WARNING - Buffer overrun in psnet\n"));
2319 memcpy(Psnet_buffers[idx].data,data,length);
2320 Psnet_buffers[idx].len = length;
2321 memcpy(&Psnet_buffers[idx].from_addr,from,sizeof(net_addr));
2322 Psnet_buffers[idx].sequence_number = Psnet_seq_number;
2324 // keep track of the highest id#
2325 Psnet_highest_id = Psnet_seq_number++;
2327 // set the lowest id# for the first time
2328 if(Psnet_lowest_id == -1){
2329 Psnet_lowest_id = Psnet_highest_id;
2334 // get the index of the next packet in order!
2335 int psnet_buffer_get_next(ubyte *data, int *length, net_addr *from)
2340 // if there are no buffers, do nothing
2341 if((Psnet_lowest_id == -1) || (Psnet_lowest_id > Psnet_highest_id)){
2345 // search until we find the lowest packet index id#
2346 for(idx=0;idx<MAX_PACKET_BUFFERS;idx++){
2347 // if we found the buffer
2348 if(Psnet_buffers[idx].sequence_number == Psnet_lowest_id){
2354 // at this point, we should _always_ have found the buffer
2357 // copy out the buffer data
2358 memcpy(data,Psnet_buffers[idx].data,Psnet_buffers[idx].len);
2359 *length = Psnet_buffers[idx].len;
2360 memcpy(from,&Psnet_buffers[idx].from_addr,sizeof(net_addr));
2362 // now we need to cleanup the packet list
2364 // mark the buffer as free
2365 Psnet_buffers[idx].sequence_number = -1;
2373 // initialize the buffering system
2374 void psnet_buffer_init() {}
2376 // buffer a packet (maintain order!)
2377 void psnet_buffer_packet(ubyte *data, int length) {}
2379 // get the index of the next packet in order!
2380 int psnet_buffer_get_next() { return -1; }
2384 // ------------------------------------------------------------------------------------------------------
2385 // RELIABLE UDP FUNCTIONS
2388 // verbose debug output
2389 #define PSNET_RELIABLE_VERBOSE
2391 // overall buffer space allocated to the buffers
2392 #define PSNET_RELIABLE_MAX_OUT_BUFFER_SIZE (1<<18) // 256k
2393 #define PSNET_RELIABLE_MAX_IN_BUFFER_SIZE (1<<12) // 4k
2395 // outgoing reliable packets
2396 typedef struct reliable_packet_out {
2397 ubyte data[MAX_PACKET_SIZE]; // data in the original packet
2398 ushort packet_size; // size of the original packet
2399 ushort player_flags; // bitflags indexing (1<<N) into the Psnet_reliable_addr[N] array
2400 ushort player_acks; // bitflags indexing (1<<N) into the Psnet_reliable_addr[N] array
2401 int player_stamps[MAX_PLAYERS]; // timeouts for each player
2402 ubyte num_resends[MAX_PLAYERS]; // # of times we've resent to a given player
2403 ushort out_id; // identifier of the packet
2404 fix age; // how old this packet is
2405 } reliable_packet_out;
2407 // incoming reliable packets
2408 typedef struct reliable_packet_in {
2409 ushort in_id; // identifier of the received packet
2410 fix age; // when we received the packet
2411 } reliable_packet_in;
2413 // # of outgoing and incoming buffers we'll allocate
2414 #define PSNET_RELIABLE_NUM_OUT_BUFFERS (PSNET_RELIABLE_MAX_OUT_BUFFER_SIZE / sizeof(reliable_packet_out))
2415 #define PSNET_RELIABLE_NUM_IN_BUFFERS (PSNET_RELIABLE_MAX_IN_BUFFER_SIZE / sizeof(reliable_packet_in))
2417 // timeout to be used for the packets
2418 #define PSNET_RELIABLE_TIMEOUT 1500 // in ms
2420 // # of repeats to use
2421 #define PSNET_RELIABLE_REPEAT 8 // repeat this many times max
2424 #define PSNET_RELIABLE_INVALID 0
2427 #define PSNET_RELIABLE_ACK 0xffff
2429 // the outgoing and incoming buffers themselves
2430 reliable_packet_out *Psnet_reliable_out[PSNET_RELIABLE_NUM_OUT_BUFFERS];
2431 reliable_packet_in *Psnet_reliable_in[PSNET_RELIABLE_NUM_IN_BUFFERS];
2433 // psnet reliable address list
2434 net_addr Psnet_reliable_addr[MAX_PLAYERS]; // address of registered "reliable" addrs
2435 int Psnet_reliable_addr_flags; // bitflags indicating which of the above are valid
2437 // my local identifier # (will only use lower 12 bits)
2438 ushort Psnet_reliable_local_id = 0x1;
2440 // is the reliable system initialized
2441 int Psnet_reliable_inited = 0;
2443 // # of times a packet has not been delivered
2444 int Psnet_reliable_fail_count = 0;
2446 // # of times packets have had to be resent
2447 int Psnet_reliable_resend_count = 0;
2449 // # of times we've run out of packets and had to overwrite existing ones
2450 int Psnet_reliable_overwrite_count = 0;
2452 // forward declarations ------------------------------------------------------
2454 // free up all used buffers
2455 void psnet_reliable_free_all_buffers();
2457 // get the index of the passed address or -1 if it doesn't exist
2458 int psnet_reliable_addr_index(net_addr *addr);
2460 // get the index into the address array from the passed bitflag
2461 int psnet_reliable_index_addr(int flags);
2463 // kill all outgoing packets belonging to the passed index
2464 void psnet_reliable_kill_outgoing(int index);
2466 // generate a unique id #, given the passed in value and the address it came from, return PSNET_RELIABLE_INVALID
2467 // upper 12 bytes should be valid # and the lower 4 should be free for this function to fill in
2468 ushort psnet_reliable_get_unique_id(ushort id_num,net_addr *from);
2470 // get the upper 12 bit version of my id# and increment the original
2471 ushort psnet_reliable_get_next_id();
2473 // get the index of a free outgoing relible packet buffer, killing the oldest if necessary
2474 int psnet_reliable_get_free_outgoing();
2476 // get the index of a free incoming relible packet buffer, killing the oldest if necessary
2477 int psnet_reliable_get_free_incoming();
2479 // actually send the data contained with the reliable_packet_out
2480 int psnet_reliable_send_packet(reliable_packet_out *out,int force_index = 0);
2482 // evaluate the status of a reliable packet - and free it up if the thing is _done_
2483 void psnet_reliable_evaluate_status(reliable_packet_out *out);
2485 // determine if the passed id# exists in the in-packet list, return the instance where it exists, or -1 if it doesn't
2486 int psnet_reliable_find_in_id(ushort id_num);
2488 // determine if the passed id# exists in the out-packet list, return the instance where it exists, or -1 if it doesn't
2489 int psnet_reliable_find_out_id(ushort id_num);
2491 // send an ack to the specified address
2492 void psnet_reliable_send_ack(net_addr *addr,ushort id_num);
2495 // extern functions -----------------------------------------------------------
2497 // initialize the psnet reliable system (return 0 on fail, 1 on success)
2498 int psnet_reliable_init()
2502 // if the system is already inited, do nothing
2503 if(Psnet_reliable_inited){
2507 #ifdef PSNET_RELIABLE_VERBOSE
2508 nprintf(("Network","PSNET RELIABLE SIZES : \n OUT BUFFER SIZE : %d\n OUT BUFFER COUNT %d\n IN BUFFER SIZE %d\n IN BUFFER COUNT %d\n",
2509 PSNET_RELIABLE_MAX_OUT_BUFFER_SIZE,PSNET_RELIABLE_NUM_OUT_BUFFERS,PSNET_RELIABLE_MAX_IN_BUFFER_SIZE,PSNET_RELIABLE_NUM_IN_BUFFERS));
2513 for(idx=0;idx<PSNET_RELIABLE_NUM_OUT_BUFFERS;idx++){
2514 Psnet_reliable_out[idx] = NULL;
2516 for(idx=0;idx<PSNET_RELIABLE_NUM_IN_BUFFERS;idx++){
2517 Psnet_reliable_in[idx] = NULL;
2520 // initialize all outgoing buffers
2521 for(idx=0;idx<PSNET_RELIABLE_NUM_OUT_BUFFERS;idx++){
2522 Psnet_reliable_out[idx] = NULL;
2523 Psnet_reliable_out[idx] = (reliable_packet_out*)malloc(sizeof(reliable_packet_out));
2525 // if we failed to allocate the buffer, return failure
2526 if(Psnet_reliable_out[idx] == NULL){
2527 psnet_reliable_free_all_buffers();
2530 memset(Psnet_reliable_out[idx],0,sizeof(reliable_packet_out));
2533 // initialize all incoming buffers
2534 for(idx=0;idx<PSNET_RELIABLE_NUM_IN_BUFFERS;idx++){
2535 Psnet_reliable_in[idx] = NULL;
2536 Psnet_reliable_in[idx] = (reliable_packet_in*)malloc(sizeof(reliable_packet_in));
2538 // if we failed to allocate the buffer, return failure
2539 if(Psnet_reliable_in[idx] == NULL){
2540 psnet_reliable_free_all_buffers();
2543 memset(Psnet_reliable_in[idx],0,sizeof(reliable_packet_in));
2546 // blast the reliable address list free
2547 memset(Psnet_reliable_addr,0,sizeof(net_addr) * MAX_PLAYERS);
2548 Psnet_reliable_addr_flags = 0;
2550 // set the system to be initialized
2551 Psnet_reliable_inited = 1;
2553 // initialize my local id #
2554 Psnet_reliable_local_id = 0x1;
2556 // initialize the packet delivery fail count
2557 Psnet_reliable_fail_count = 0;
2559 // intialize the packet necessary resend count
2560 Psnet_reliable_resend_count = 0;
2562 // initialize # of times we've run out of packets and had to overwrite existing ones
2563 Psnet_reliable_overwrite_count = 0;
2569 // shutdown the reliable system (free up buffers, etc)
2570 void psnet_reliable_close()
2572 // if the system is not initialized, don't do anything
2573 if(!Psnet_reliable_inited){
2577 // free up all buffers
2578 psnet_reliable_free_all_buffers();
2580 // blast all addresses clean
2581 memset(Psnet_reliable_addr,0,sizeof(net_addr) * MAX_PLAYERS);
2582 Psnet_reliable_addr_flags = 0;
2584 // set the system as being uninitialized
2585 Psnet_reliable_inited = 0;
2588 // notify the reliable system of a new address at index N
2589 void psnet_reliable_notify_new_addr(net_addr *addr,int index)
2591 // copy in the address
2592 memcpy(&Psnet_reliable_addr[index],addr,sizeof(net_addr));
2594 // set the bit indicating its validity
2595 Psnet_reliable_addr_flags |= (1<<index);
2598 // notify the reliable system of a drop at index N
2599 void psnet_reliable_notify_drop_addr(net_addr *addr)
2603 // do a lookup for the address
2604 index = psnet_reliable_addr_index(addr);
2606 // clear out all packets belonging exclusively to this address
2607 psnet_reliable_kill_outgoing(index);
2609 // clear the address and its existence bit
2610 memset(&Psnet_reliable_addr[index],0,sizeof(net_addr));
2611 Psnet_reliable_addr_flags &= ~(1<<index);
2615 // send a reliable data packet
2616 int psnet_reliable_send(ubyte *data,int packet_size,net_addr *addr)
2620 reliable_packet_out *p;
2622 // if the system is not initialized, don't do anything
2623 if(!Psnet_reliable_inited){
2627 // try and find a guy to send to
2629 to_index = psnet_reliable_addr_index(addr);
2631 nprintf(("Network","PSNET RELIABLE : could not find player for outgoing packet!\n"));
2635 // attempt to get a free buffer
2637 free_buffer = psnet_reliable_get_free_outgoing();
2638 if(free_buffer == -1){
2639 Int3(); // should never happen - we should always overwrite the oldest buffer
2642 // setup the data for the outgoing packet
2643 p = Psnet_reliable_out[free_buffer];
2644 memcpy(p->data,data,packet_size);
2645 p->packet_size = (ushort)packet_size;
2646 p->player_flags |= (1<<to_index);
2647 memset(p->player_stamps,0xf,sizeof(int) * MAX_PLAYERS);
2648 p->player_stamps[to_index] = timestamp(PSNET_RELIABLE_TIMEOUT);
2649 p->out_id = psnet_reliable_get_next_id();
2650 p->age = timer_get_fixed_seconds();
2653 return psnet_reliable_send_packet(p);
2656 // process frame for all reliable stuff (call once per frame)
2657 void psnet_reliable_process()
2660 reliable_packet_out *p;
2662 // if the system is not initialized, don't do anything
2663 if(!Psnet_reliable_inited){
2667 // go through all active packets
2668 for(idx=0;idx<PSNET_RELIABLE_NUM_OUT_BUFFERS;idx++){
2669 // if the packet is active
2670 if(Psnet_reliable_out[idx]->out_id != PSNET_RELIABLE_INVALID){
2671 p = Psnet_reliable_out[idx];
2673 // loop through all active players and see if we need to resend
2674 for(s_idx=0;s_idx<MAX_PLAYERS;s_idx++){
2675 // if the packet is active for this player and he hasn't acked
2676 if(p->player_flags & (1<<s_idx) && !(p->player_acks & (1<<s_idx))){
2677 // if the timestamp has elapsed
2678 if((p->player_stamps[s_idx] != -1) && timestamp_elapsed(p->player_stamps[s_idx])){
2679 // if we are at max resends, bomb!
2680 if(p->num_resends[s_idx] >= PSNET_RELIABLE_REPEAT){
2681 #ifdef PSNET_RELIABLE_VERBOSE
2682 nprintf(("Network","PSNET RELIABLE : packet failed to be delivered (%d retries) !!\n",PSNET_RELIABLE_REPEAT));
2684 p->player_flags &= ~(1<<s_idx);
2686 // increment the fail count
2687 Psnet_reliable_fail_count++;
2689 // otherwise resend the packet
2691 // actually send the data contained with the reliable_packet_out
2692 psnet_reliable_send_packet(p,s_idx);
2694 // increment the resend count
2695 Psnet_reliable_resend_count++;
2697 #ifdef PSNET_RELIABLE_VERBOSE
2698 nprintf(("Network","PSNET RELIABLE : resending packet\n"));
2705 // evaluate if this packet has completed
2706 psnet_reliable_evaluate_status(p);
2711 // determine if the passed in reliable data should be processed, and sends an ack if necessary
2712 // return # of bytes which should be stripped off the data (reliable data header)
2713 int psnet_reliable_should_process(net_addr *addr,ubyte *data,int packet_size)
2715 ushort id_num,ack_code,unique_id;
2719 reliable_packet_in *p;
2721 // get the reliable packet id #
2722 memcpy(&id_num,data,sizeof(ushort));
2724 // if the id# is an ack, get the id# do a lookup
2725 if(id_num == PSNET_RELIABLE_ACK){
2726 #ifdef PSNET_RELIABLE_VERBOSE
2727 nprintf(("Network","PSNET RELIABLE : ACK 1\n"));
2733 memcpy(&id_num,data+2,sizeof(ushort));
2735 // get the packet index
2736 // unique_id = psnet_reliable_get_unique_id(id_num,addr);
2738 packet_index = psnet_reliable_find_out_id(unique_id);
2739 player_index = psnet_reliable_addr_index(addr);
2740 if((packet_index != -1) && (player_index != -1)){
2741 #ifdef PSNET_RELIABLE_VERBOSE
2742 nprintf(("Network","PSNET RELIABLE : ACK 2\n"));
2745 Psnet_reliable_out[packet_index]->player_acks |= (1<<player_index);
2747 // check to see if this packet is _done_
2748 psnet_reliable_evaluate_status(Psnet_reliable_out[packet_index]);
2751 // return 4 bytes processed
2755 // otherwise - see if this is a new packet
2756 packet_index = psnet_reliable_find_in_id(id_num);
2757 if(packet_index == -1){
2760 free_index = psnet_reliable_get_free_incoming();
2761 if(free_index == -1){
2765 // setup the incoming packet
2766 p = Psnet_reliable_in[free_index];
2767 p->age = timer_get_fixed_seconds();
2768 p->in_id = psnet_reliable_get_unique_id(id_num,addr);
2771 psnet_reliable_send_ack(addr,id_num);
2773 // return 2 bytes processed
2776 // send another ack for good measure
2778 psnet_reliable_send_ack(addr,id_num);
2784 // forward definitions --------------------------------------------
2786 // free up all used buffers
2787 void psnet_reliable_free_all_buffers()
2791 // free all outgoing buffers
2792 for(idx=0;idx<PSNET_RELIABLE_NUM_OUT_BUFFERS;idx++){
2793 // if the buffer is not null, free it
2794 if(Psnet_reliable_out[idx] != NULL){
2795 free(Psnet_reliable_out[idx]);
2796 Psnet_reliable_out[idx] = NULL;
2800 // free all incoming buffers
2801 for(idx=0;idx<PSNET_RELIABLE_NUM_IN_BUFFERS;idx++){
2802 // if the buffer is not null, free it
2803 if(Psnet_reliable_in[idx] != NULL){
2804 free(Psnet_reliable_in[idx]);
2805 Psnet_reliable_in[idx] = NULL;
2810 // get the index of the passed address or -1 if it doesn't exist
2811 int psnet_reliable_addr_index(net_addr *addr)
2815 // look through all valid addresses
2816 for(idx=0;idx<MAX_PLAYERS;idx++){
2817 if((Psnet_reliable_addr_flags & (1<<idx)) && psnet_same(addr,&Psnet_reliable_addr[idx])){
2822 // couldn't find the address
2826 // get the index into the address array from the passed bitflag
2827 int psnet_reliable_index_addr(int flags)
2831 // look through all the bits in the flags
2832 for(idx=0;idx<MAX_PLAYERS;idx++){
2833 if(flags & (1<<idx)){
2838 // couldn't find any
2842 // kill all outgoing packets belonging to the passed index
2843 void psnet_reliable_kill_outgoing(int index)
2847 // go through all buffers
2848 for(idx=0;idx<PSNET_RELIABLE_NUM_OUT_BUFFERS;idx++){
2849 if(Psnet_reliable_out[idx]->out_id != PSNET_RELIABLE_INVALID){
2850 // if it is exclusively his, kill the whole packet
2851 if(Psnet_reliable_out[idx]->player_flags == (1<<index)){
2852 memset(Psnet_reliable_out[idx],0,sizeof(reliable_packet_out));
2855 // if it belongs to him and other players, kill his entry
2856 else if(Psnet_reliable_out[idx]->player_flags & (1<<index)){
2857 Psnet_reliable_out[idx]->player_flags &= ~(1<<index);
2858 Psnet_reliable_out[idx]->num_resends[index] = 0;
2864 // generate a unique id #, given the passed in value and the address it came from, return PSNET_RELIABLE_INVALID
2865 // upper 12 bytes should be valid # and the lower 4 should be free for this function to fill in
2866 ushort psnet_reliable_get_unique_id(ushort id_num,net_addr *from)
2871 // if the lower 4 bits are not clear, we've got a problem
2876 // lookup through the Psnet_reliable_addr[] list and try and find the index
2877 for(idx=0;idx<MAX_PLAYERS;idx++){
2878 if((idx == 1) /*(Psnet_reliable_addr_flags & (1<<idx)) && (psnet_same(from,&Psnet_reliable_addr[idx]))*/){
2879 // fill in the lower 4 bits
2881 id_num |= (cast & 0xf);
2886 // couldn't find an id#
2887 return PSNET_RELIABLE_INVALID;
2890 // get the upper 12 bit version of my id# and increment the original
2891 ushort psnet_reliable_get_next_id()
2893 ushort id_num = Psnet_reliable_local_id;
2895 // clear out the upper 4 bits
2898 // shift 4 bits to the left
2901 // increment the local id #
2902 if(Psnet_reliable_local_id == 0x0fff){
2903 Psnet_reliable_local_id = 0x1;
2905 Psnet_reliable_local_id++;
2908 // return the shifted value
2912 // get the index of a free outgoing relible packet buffer, killing the oldest if necessary
2913 int psnet_reliable_get_free_outgoing()
2916 int oldest_index = -1;
2919 // search through all buffers
2920 for(idx=0;idx<PSNET_RELIABLE_NUM_OUT_BUFFERS;idx++){
2921 if(Psnet_reliable_out[idx]->out_id == PSNET_RELIABLE_INVALID){
2925 // keep track of the oldest packet
2926 if((oldest_index == -1) || (Psnet_reliable_out[idx]->age < oldest)){
2928 oldest = Psnet_reliable_out[idx]->age;
2932 // if we got here, all of our buffers are full, so we should kill the oldest one
2933 memset(Psnet_reliable_out[oldest_index],0,sizeof(reliable_packet_out));
2934 Psnet_reliable_overwrite_count = 0;
2935 #ifdef PSNET_RELIABLE_VERBOSE
2936 nprintf(("Network","PSNET RELIABLE : overwriting old send buffer\n"));
2938 return oldest_index;
2941 // get the index of a free incoming relible packet buffer, killing the oldest if necessary
2942 int psnet_reliable_get_free_incoming()
2945 int oldest_index = -1;
2948 // search through all buffers
2949 for(idx=0;idx<PSNET_RELIABLE_NUM_IN_BUFFERS;idx++){
2950 if(Psnet_reliable_in[idx]->in_id == PSNET_RELIABLE_INVALID){
2954 // keep track of the oldest packet
2955 if((oldest_index == -1) || (Psnet_reliable_in[idx]->age < oldest)){
2957 oldest = Psnet_reliable_in[idx]->age;
2961 // if we got here, all of our buffers are full, so we should kill the oldest one
2962 memset(Psnet_reliable_in[oldest_index],0,sizeof(reliable_packet_in));
2963 #ifdef PSNET_RELIABLE_VERBOSE
2964 nprintf(("Network","PSNET RELIABLE : overwriting old recv buffer\n"));
2966 return oldest_index;
2969 // actually send the data contained with the reliable_packet_out
2970 int psnet_reliable_send_packet(reliable_packet_out *out,int force_index)
2972 ubyte data[MAX_PACKET_SIZE];
2974 int packet_size = 2 + out->packet_size;
2976 // stick the identifier on the front of the packet
2977 memcpy(data,&out->out_id,sizeof(ushort));
2979 // copy in the actual data
2980 memcpy(data+2,out->data,out->packet_size);
2982 // send the packet and update the timestamp for all players
2984 // send to one specified player in the packet
2986 if(force_index != 0){
2987 if(out->player_flags & (1<<force_index) && !(out->player_acks & (1<<force_index))){
2988 bytes_sent = psnet_send(&Psnet_reliable_addr[force_index],data,packet_size,PSNET_FLAG_RAW,1);
2989 out->player_stamps[force_index] = timestamp(PSNET_RELIABLE_TIMEOUT);
2990 out->num_resends[force_index]++;
2993 // send to all players contained in the packet
2995 for(idx=0;idx<MAX_PLAYERS;idx++){
2996 // if this guy is flagged and he exists and he hasn't already acked
2997 if(out->player_flags & (1<<idx) && (Psnet_reliable_addr_flags & (1<<idx)) && !(out->player_acks & (1<<idx))){
2998 bytes_sent = psnet_send(&Psnet_reliable_addr[idx],data,packet_size,PSNET_FLAG_RAW,1);
2999 out->player_stamps[idx] = timestamp(PSNET_RELIABLE_TIMEOUT);
3000 out->num_resends[idx]++;
3009 // evaluate the status of a reliable packet - and free it up if the thing is _done_
3010 void psnet_reliable_evaluate_status(reliable_packet_out *out)
3014 // check to see if all players have acked or failed miserably
3016 for(idx=0;idx<MAX_PLAYERS;idx++){
3017 // if the packet is active for this guy but he has not acked yet
3018 if((out->player_flags & (1<<idx)) && !(out->player_acks & (1<<idx))){
3024 // if its ok to annihilate this packet, do so
3026 memset(out,0,sizeof(reliable_packet_out));
3030 // determine if the passed id# exists in the in-packet list, return the instance where it exists, or -1 if it doesn't
3031 int psnet_reliable_find_in_id(ushort id_num)
3035 // if the id is 0, its invalid
3036 if(id_num == PSNET_RELIABLE_INVALID){
3040 // look through all in packets
3041 for(idx=0;idx<PSNET_RELIABLE_NUM_IN_BUFFERS;idx++){
3042 if(id_num == Psnet_reliable_in[idx]->in_id){
3051 // determine if the passed id# exists in the out-packet list, return the instance where it exists, or -1 if it doesn't
3052 int psnet_reliable_find_out_id(ushort id_num)
3056 // if the id is 0, its invalid
3057 if(id_num == PSNET_RELIABLE_INVALID){
3061 // look through all in packets
3062 for(idx=0;idx<PSNET_RELIABLE_NUM_OUT_BUFFERS;idx++){
3063 if(id_num == Psnet_reliable_out[idx]->out_id){
3072 // send an ack to the specified address
3073 void psnet_reliable_send_ack(net_addr *addr,ushort id_num)
3079 val = PSNET_RELIABLE_ACK;
3080 memcpy(data,&val,sizeof(ushort));
3083 memcpy(data+2,&id_num,sizeof(ushort));
3086 psnet_send(addr,data,4,PSNET_FLAG_RAW,Reliable_socket);
3089 #endif // #ifndef PSNET2