| 1 |
/* |
| 2 |
* Descent 3 |
| 3 |
* Copyright (C) 2024 Parallax Software |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
--- HISTORICAL COMMENTS FOLLOW --- |
| 19 |
|
| 20 |
* $Logfile: /DescentIII/Main/networking/networking.cpp $ |
| 21 |
* $Revision: 1.7 $ |
| 22 |
* $Date: 2004/12/05 04:38:23 $ |
| 23 |
* $Author: ryan $ |
| 24 |
* |
| 25 |
* |
| 26 |
* |
| 27 |
* $Log: networking.cpp,v $ |
| 28 |
* Revision 1.7 2004/12/05 04:38:23 ryan |
| 29 |
* Disable IPX support. |
| 30 |
* |
| 31 |
* Revision 1.6 2004/12/05 04:00:20 ryan |
| 32 |
* MacOS X patches. |
| 33 |
* |
| 34 |
* Revision 1.5 2004/02/25 00:04:06 ryan |
| 35 |
* Removed loki_utils dependency and ported to MacOS X (runs, but incomplete). |
| 36 |
* |
| 37 |
* Revision 1.4 2001/01/13 21:48:46 icculus |
| 38 |
* patched to (re)compile on win32. |
| 39 |
* |
| 40 |
* Revision 1.3 2000/06/29 06:41:24 icculus |
| 41 |
* mad commits. |
| 42 |
* |
| 43 |
* Revision 1.2 2000/06/24 01:15:15 icculus |
| 44 |
* patched to compile. |
| 45 |
* |
| 46 |
* Revision 1.1.1.1 2000/04/18 00:00:41 icculus |
| 47 |
* initial checkin |
| 48 |
* |
| 49 |
* |
| 50 |
* 116 1/30/00 3:33p Jason |
| 51 |
* Expanded network receive buffer |
| 52 |
* |
| 53 |
* 115 10/21/99 3:01p Kevin |
| 54 |
* Macintosh willdial fixes |
| 55 |
* |
| 56 |
* 113 9/17/99 2:44p Kevin |
| 57 |
* preferred net buffer size == 16k |
| 58 |
* |
| 59 |
* 112 8/31/99 5:30p Jason |
| 60 |
* network statistics code |
| 61 |
* |
| 62 |
* 111 8/24/99 2:10p Kevin |
| 63 |
* Some debugging & Macintosh fixes |
| 64 |
* |
| 65 |
* 110 8/23/99 5:15p Kevin |
| 66 |
* Macintosh Byte ordering fixes |
| 67 |
* |
| 68 |
* 109 8/21/99 3:14a Jeff |
| 69 |
* real async lookup in Linux |
| 70 |
* |
| 71 |
* 108 8/10/99 11:00a Kevin |
| 72 |
* decreased receive buffer size |
| 73 |
* |
| 74 |
* 107 8/02/99 10:24p Kevin |
| 75 |
* fixed some mac stuff |
| 76 |
* |
| 77 |
* 106 7/29/99 10:33a Kevin |
| 78 |
* More Mac |
| 79 |
* |
| 80 |
* 105 7/26/99 12:05p Kevin |
| 81 |
* Macintosh stuff |
| 82 |
* |
| 83 |
* 104 5/23/99 5:55p Jeff |
| 84 |
* mprintf for easy debugging |
| 85 |
* |
| 86 |
* 103 5/19/99 5:21p Kevin |
| 87 |
* changed return value of nw_GetHostAddressFromNumbers() |
| 88 |
* |
| 89 |
* 102 5/19/99 1:12p Kevin |
| 90 |
* fixed an mprintf and added a saftey check |
| 91 |
* |
| 92 |
* 101 5/06/99 3:27a Kevin |
| 93 |
* directplay fixes |
| 94 |
* |
| 95 |
* 100 4/30/99 5:07p Kevin |
| 96 |
* misc dedicated server, networking and low memory enhancements |
| 97 |
* |
| 98 |
* 99 4/25/99 3:19p Kevin |
| 99 |
* removed really bad call to Error() when the reliable buffer overflows. |
| 100 |
* It is possible for this to happen when one client disconnects and |
| 101 |
* shouldn't cause the server to shutdown. |
| 102 |
* |
| 103 |
* 98 4/22/99 6:32p Kevin |
| 104 |
* Hopefully fixed linux gehostbyname problem |
| 105 |
* |
| 106 |
* 97 4/19/99 7:57p Jeff |
| 107 |
* pack structures for Linux version |
| 108 |
* |
| 109 |
* 96 4/19/99 4:02a Jeff |
| 110 |
* fixed select calls for Linux |
| 111 |
* |
| 112 |
* 95 4/17/99 5:56a Jeff |
| 113 |
* ported to Linux |
| 114 |
* |
| 115 |
* 94 4/15/99 2:58a Jeff |
| 116 |
* added some defines and includes needed for linux ipx |
| 117 |
* |
| 118 |
* 93 4/14/99 1:38a Jeff |
| 119 |
* fixed case mismatched #includes |
| 120 |
* |
| 121 |
* 92 4/12/99 3:16p Kevin |
| 122 |
* Added a bunch of stuff for the linux version |
| 123 |
* |
| 124 |
* 91 3/08/99 3:59p Kevin |
| 125 |
* Idle processing of network packets fixed |
| 126 |
* |
| 127 |
* 90 2/24/99 12:48p Kevin |
| 128 |
* Added urgent flag to nw_SendReliable. It causes the packet to go out |
| 129 |
* that frame. |
| 130 |
* |
| 131 |
* 89 2/23/99 3:09p Jason |
| 132 |
* changed queue time to tenth of a second, as a test. 4 tenths of a |
| 133 |
* second might be too long, especially on a LAN. |
| 134 |
* |
| 135 |
* 88 2/08/99 11:12p Kevin |
| 136 |
* Fixed ip selection & RAS code |
| 137 |
* |
| 138 |
* 87 2/08/99 4:08p Kevin |
| 139 |
* Fixed a stupid bug I introduced last week |
| 140 |
* |
| 141 |
* 86 2/05/99 7:24p Kevin |
| 142 |
* Added NAGLE type packet buffering to reliable networking code |
| 143 |
* |
| 144 |
* 85 1/26/99 9:45p Jeff |
| 145 |
* moved tcplog from networking to mono lib |
| 146 |
* |
| 147 |
* 84 1/08/99 4:51p Jeff |
| 148 |
* don't put the null char at the end of the buffer when sending the |
| 149 |
* TCPPrintf |
| 150 |
* |
| 151 |
* 83 1/08/99 2:58p Kevin |
| 152 |
* Added TCP mprintf support so you can log to a remote machine. |
| 153 |
* |
| 154 |
* 82 1/07/99 11:51a Kevin |
| 155 |
* Added support for joining servers on alternate ports and hosting behind |
| 156 |
* a proxy/firewall |
| 157 |
* |
| 158 |
* 81 1/06/99 3:08p Jason |
| 159 |
* changed recommended pps |
| 160 |
* |
| 161 |
* 80 1/04/99 12:22p Jeff |
| 162 |
* commented out assert no longer needed (nw_GetHostAddressFromNumbers) |
| 163 |
* |
| 164 |
* 79 12/28/98 10:02a Kevin |
| 165 |
* removed mprintfs |
| 166 |
* |
| 167 |
* 78 12/23/98 6:38p Kevin |
| 168 |
* All UDP data (except gamespy) now uses one (registered) port number |
| 169 |
* |
| 170 |
* 77 12/14/98 3:07p Kevin |
| 171 |
* Added +ip to specify IP to bind to (gamespy command line arg) |
| 172 |
* |
| 173 |
* 76 12/03/98 9:29a Kevin |
| 174 |
* Added better ip selection code |
| 175 |
* |
| 176 |
* 75 11/06/98 4:46p Jason |
| 177 |
* added runtime error for being unable to send reliable packet |
| 178 |
* |
| 179 |
* 74 10/29/98 5:08p Jason |
| 180 |
* added an Int3 for when the unthinkable actually happens |
| 181 |
* |
| 182 |
* 73 10/19/98 7:51p Kevin |
| 183 |
* performance testing |
| 184 |
* |
| 185 |
* 72 10/16/98 11:55a Kevin |
| 186 |
* Made dlls loadable in a hog |
| 187 |
* |
| 188 |
* 71 10/14/98 7:20p Kevin |
| 189 |
* More dsp changes... |
| 190 |
* |
| 191 |
* 70 10/12/98 8:39p Kevin |
| 192 |
* removed mprintf's and fixed some smallish bugs |
| 193 |
* |
| 194 |
* 69 10/12/98 4:10p Kevin |
| 195 |
* Made sockets reuasable so if the game locks you don't have to reboot |
| 196 |
* |
| 197 |
* 68 10/08/98 12:30p Nate |
| 198 |
* For Kevin. Fixed stupidity bug I just introduced |
| 199 |
* |
| 200 |
* 67 10/08/98 9:59a Kevin |
| 201 |
* made change to async gethostbyname |
| 202 |
* |
| 203 |
* 66 10/06/98 6:07p Jason |
| 204 |
* added protective layer for reliable packets |
| 205 |
* |
| 206 |
* 65 9/30/98 10:36a Kevin |
| 207 |
* Added command line launching of URLs and command line directory |
| 208 |
* specification |
| 209 |
* |
| 210 |
* 64 9/28/98 4:22p Kevin |
| 211 |
* Redesigned game list menus |
| 212 |
* |
| 213 |
* 63 9/28/98 11:02a Kevin |
| 214 |
* added Networking defer, and fixed some UI issues |
| 215 |
* |
| 216 |
* 62 9/28/98 9:53a Kevin |
| 217 |
* Fixing misc UI problems, and fixed some bugs that VC 6 found |
| 218 |
* |
| 219 |
* 61 9/22/98 2:29p Kevin |
| 220 |
* moved ships allowed code out of dll and into main app. Also added |
| 221 |
* powerup exclusions |
| 222 |
* |
| 223 |
* 60 9/04/98 1:51p Kevin |
| 224 |
* implemented asyncronous gethostbyname |
| 225 |
* |
| 226 |
* 59 9/03/98 11:12a Kevin |
| 227 |
* fixed bug related to the size of reliable packet header... |
| 228 |
* |
| 229 |
* 58 8/31/98 10:15a Kevin |
| 230 |
* Misc. multi-UI fixes |
| 231 |
* |
| 232 |
* 57 8/24/98 5:04p Kevin |
| 233 |
* Made msn files have the option to not be playable in multiplayer |
| 234 |
* |
| 235 |
* 56 8/24/98 10:55a Kevin |
| 236 |
* new directplay stuff |
| 237 |
* |
| 238 |
* 55 8/19/98 11:50a Kevin |
| 239 |
* Got DirectPlay IPX working, and localized connection DLLs |
| 240 |
* |
| 241 |
* 54 8/17/98 11:00a Kevin |
| 242 |
* Moved DLLs into subdirectories |
| 243 |
* |
| 244 |
* 53 8/14/98 4:54p Kevin |
| 245 |
* More directplay stuff |
| 246 |
* |
| 247 |
* 52 8/13/98 6:32p Kevin |
| 248 |
* Initial implementation of directplay API |
| 249 |
* |
| 250 |
* 51 8/11/98 11:18a Kevin |
| 251 |
* Cleaned up unused code and unused function |
| 252 |
* |
| 253 |
* 50 8/11/98 10:46a Kevin |
| 254 |
* removed TCPIP only code from reliable code |
| 255 |
* |
| 256 |
* 49 8/11/98 10:28a Kevin |
| 257 |
* Added a bunch of return code checking to help throttle bandwidth |
| 258 |
* |
| 259 |
* 48 8/10/98 4:32p Kevin |
| 260 |
* added RAS code to correctly Identify IP address |
| 261 |
* |
| 262 |
* 47 8/10/98 12:56p Kevin |
| 263 |
* Removed blocking writes |
| 264 |
* |
| 265 |
* 46 8/07/98 7:02p Kevin |
| 266 |
* |
| 267 |
* 45 8/07/98 7:02p Kevin |
| 268 |
* dumb bug fix for below |
| 269 |
* |
| 270 |
* 44 8/07/98 6:44p Kevin |
| 271 |
* made unreliable sockets non blocking |
| 272 |
* |
| 273 |
* 43 8/05/98 2:25p Kevin |
| 274 |
* improved network address reporting |
| 275 |
* |
| 276 |
* 42 8/05/98 12:07p Kevin |
| 277 |
* minor fixes to reliable |
| 278 |
* |
| 279 |
* 41 7/29/98 6:11p Kevin |
| 280 |
* took out sequence hack |
| 281 |
* |
| 282 |
* 40 7/29/98 5:31p Kevin |
| 283 |
* added rcs header |
| 284 |
* |
| 285 |
*/ |
| 286 |
|
| 287 |
#ifdef WIN32 |
| 288 |
#include <windows.h> |
| 289 |
#include <winsock.h> |
| 290 |
#include <process.h> |
| 291 |
#include <wsipx.h> |
| 292 |
#include <ras.h> |
| 293 |
typedef int socklen_t; |
| 294 |
#endif |
| 295 |
#include <stdlib.h> |
| 296 |
#include <string.h> |
| 297 |
#if defined(POSIX) |
| 298 |
#if defined(__LINUX__) |
| 299 |
#include <netinet/in.h> |
| 300 |
#endif |
| 301 |
#include <arpa/inet.h> |
| 302 |
#include <sys/socket.h> |
| 303 |
#include <sys/stat.h> |
| 304 |
#include <sys/types.h> |
| 305 |
#include <sys/time.h> |
| 306 |
#include <stdlib.h> |
| 307 |
#include <unistd.h> |
| 308 |
|
| 309 |
#define TRUE true |
| 310 |
#define FALSE false |
| 311 |
#define LPSTR char * |
| 312 |
#endif |
| 313 |
|
| 314 |
#ifdef WIN32 |
| 315 |
#include "dplay.h" |
| 316 |
#include "dplobby.h" |
| 317 |
#endif |
| 318 |
|
| 319 |
#include "descent.h" |
| 320 |
// #include "../manage/shippage.h" |
| 321 |
#include "appdatabase.h" |
| 322 |
|
| 323 |
#include "pstypes.h" |
| 324 |
#include "pserror.h" |
| 325 |
#include "mono.h" |
| 326 |
#include "networking.h" |
| 327 |
#include "ddio.h" |
| 328 |
#include "mem.h" |
| 329 |
#include "game.h" |
| 330 |
#include "args.h" |
| 331 |
#include "byteswap.h" |
| 332 |
|
| 333 |
#ifdef WIN32 |
| 334 |
#include "directplay.h" |
| 335 |
#endif |
| 336 |
|
| 337 |
#include "pstring.h" |
| 338 |
|
| 339 |
#ifndef WIN32 |
| 340 |
bool Use_DirectPlay = false; |
| 341 |
#endif |
| 342 |
|
| 343 |
#include "module.h" //for some nice defines to use below |
| 344 |
|
| 345 |
#define MAX_CONNECT_TRIES 50 |
| 346 |
#define MAX_RECEIVE_BUFSIZE 32768 |
| 347 |
//(1<<16) // 16 K, eh? |
| 348 |
|
| 349 |
int iMaxReceiveBufsize = MAX_RECEIVE_BUFSIZE; |
| 350 |
|
| 351 |
#ifdef WIN32 |
| 352 |
DWORD(DLLFUNCCALL *pRasEnumConnections)(LPRASCONN lprasconn, LPDWORD lpcb, LPDWORD lpcConnections) = NULL; |
| 353 |
DWORD(DLLFUNCCALL *pRasGetConnectStatus)(HRASCONN hrasconn, LPRASCONNSTATUS lprasconnstatus) = NULL; |
| 354 |
DWORD(DLLFUNCCALL *pRasGetProjectionInfo) |
| 355 |
(HRASCONN hrasconn, RASPROJECTION rasprojection, LPVOID lpprojection, LPDWORD lpcb) = NULL; |
| 356 |
#endif |
| 357 |
|
| 358 |
int Dialup_connection = 0; |
| 359 |
|
| 360 |
int nw_ServerSocket = -1; |
| 361 |
int nw_ClientSocket = -1; |
| 362 |
|
| 363 |
network_protocol NetworkProtocol = NP_NONE; |
| 364 |
|
| 365 |
int Sockets_initted = 0; |
| 366 |
int Network_initted = 0; |
| 367 |
|
| 368 |
uint32_t Net_fixed_ip = INADDR_NONE; |
| 369 |
// sockets for IPX and TCP |
| 370 |
|
| 371 |
SOCKET TCP_socket; |
| 372 |
SOCKET TCP_reliable_socket; |
| 373 |
SOCKET TCP_listen_socket; |
| 374 |
|
| 375 |
// the sockets that the game will use when selecting network type |
| 376 |
static SOCKET *Unreliable_socket; |
| 377 |
static SOCKET *Reliable_socket; |
| 378 |
static SOCKET *Listen_socket; |
| 379 |
|
| 380 |
// our current running statistics |
| 381 |
static tNetworkStatus NetStatistics; |
| 382 |
|
| 383 |
BOOL TCP_active = FALSE; |
| 384 |
BOOL IPX_active = FALSE; |
| 385 |
BOOL DP_active = FALSE; // Direct Play active |
| 386 |
// BOOL TCP_MT_active = FALSE; |
| 387 |
|
| 388 |
// This structure contains the local computer info |
| 389 |
network_address My_addr; |
| 390 |
|
| 391 |
struct network_checksum_packet { |
| 392 |
int sequence_number; |
| 393 |
uint16_t flags; |
| 394 |
uint16_t checksum; |
| 395 |
uint8_t data[MAX_PACKET_SIZE]; |
| 396 |
}; |
| 397 |
|
| 398 |
// definition for a non-checksum packet |
| 399 |
struct network_naked_packet { |
| 400 |
int sequence_number; |
| 401 |
uint16_t flags; |
| 402 |
uint8_t data[MAX_PACKET_SIZE]; |
| 403 |
}; |
| 404 |
|
| 405 |
// structure definition for our packet buffers |
| 406 |
struct network_packet_buffer { |
| 407 |
int sequence_number; |
| 408 |
int len; |
| 409 |
network_address from_addr; |
| 410 |
uint8_t data[MAX_PACKET_SIZE]; |
| 411 |
}; |
| 412 |
|
| 413 |
#define MAX_PACKET_BUFFERS 96 |
| 414 |
|
| 415 |
static network_packet_buffer Packet_buffers[MAX_PACKET_BUFFERS]; // buffer to hold packets sent to us |
| 416 |
static int16_t Packet_free_list[MAX_PACKET_BUFFERS]; // contains id's of free packet buffers |
| 417 |
static int Num_packet_buffers; |
| 418 |
static int Largest_packet_index = 0; |
| 419 |
|
| 420 |
static int Uncompressed_outgoing_data_len = 0; |
| 421 |
static int Compressed_outgoing_data_len = 0; |
| 422 |
|
| 423 |
int Next_packet_id; |
| 424 |
int Last_packet_id; |
| 425 |
|
| 426 |
static CFILE *NetDebugFile = NULL; |
| 427 |
|
| 428 |
// An array of callbacks |
| 429 |
static NetworkReceiveCallback Netcallbacks[16]; |
| 430 |
|
| 431 |
#define R_NET_SEQUENCE_NONE 0 |
| 432 |
#define R_NET_SEQUENCE_CONNECTING 1 |
| 433 |
#define R_NET_SEQUENCE_CONNECTED 2 |
| 434 |
#define R_NET_SEQUENCE_FAILED 3 |
| 435 |
|
| 436 |
#define R_NET_PACKET_QUEUE_TIME .1f |
| 437 |
|
| 438 |
static int Net_connect_socket_id = INVALID_SOCKET; |
| 439 |
static int Net_connect_sequence = R_NET_SEQUENCE_NONE; |
| 440 |
|
| 441 |
// ------------------------------------------------------------------------------------------------------ |
| 442 |
// PACKET BUFFERING FUNCTIONS |
| 443 |
// |
| 444 |
|
| 445 |
// a sequence number of -1 will indicate that this packet is not valid |
| 446 |
static network_packet_buffer Psnet_buffers[MAX_PACKET_BUFFERS]; |
| 447 |
static int Psnet_seq_number = 0; |
| 448 |
static int Psnet_lowest_id = 0; |
| 449 |
static int Psnet_highest_id = 0; |
| 450 |
|
| 451 |
// Reliable UDP stuff |
| 452 |
//******************************* |
| 453 |
#ifdef WIN32 |
| 454 |
#pragma pack(push, r_udp) |
| 455 |
#endif |
| 456 |
#pragma pack(1) |
| 457 |
struct reliable_header { |
| 458 |
uint8_t type; // packet type |
| 459 |
uint8_t compressed; // |
| 460 |
uint16_t seq; // sequence packet 0-65535 used for ACKing also |
| 461 |
uint16_t data_len; // length of data |
| 462 |
float send_time; // Time the packet was sent, if an ACK the time the packet being ACK'd was sent. |
| 463 |
uint8_t data[NETBUFFERSIZE]; // Packet data |
| 464 |
}; |
| 465 |
|
| 466 |
#define RELIABLE_PACKET_HEADER_ONLY_SIZE (sizeof(reliable_header) - NETBUFFERSIZE) |
| 467 |
#define MAX_PING_HISTORY 10 |
| 468 |
|
| 469 |
struct reliable_net_sendbuffer { |
| 470 |
uint8_t buffer[NETBUFFERSIZE]; |
| 471 |
|
| 472 |
}; |
| 473 |
|
| 474 |
struct reliable_net_rcvbuffer { |
| 475 |
uint8_t buffer[NETBUFFERSIZE]; |
| 476 |
}; |
| 477 |
|
| 478 |
static SOCKET Reliable_UDP_socket = INVALID_SOCKET; |
| 479 |
|
| 480 |
static float first_sent_iamhere = 0; |
| 481 |
static float last_sent_iamhere = 0; |
| 482 |
|
| 483 |
static uint32_t serverconn = 0xFFFFFFFF; |
| 484 |
|
| 485 |
#ifdef WIN32 |
| 486 |
#pragma pack(pop, r_udp) |
| 487 |
#else |
| 488 |
#pragma pack() |
| 489 |
#endif |
| 490 |
|
| 491 |
struct reliable_socket { |
| 492 |
|
| 493 |
float timesent[MAXNETBUFFERS]; |
| 494 |
int16_t send_len[MAXNETBUFFERS]; |
| 495 |
int16_t recv_len[MAXNETBUFFERS]; |
| 496 |
float last_packet_received; // For a given connection, this is the last packet we received |
| 497 |
float last_packet_sent; |
| 498 |
float pings[MAX_PING_HISTORY]; |
| 499 |
uint32_t num_ping_samples; |
| 500 |
float mean_ping; |
| 501 |
float last_sent; // The last time we sent a packet (used for NAGLE emulation) |
| 502 |
int waiting_packet_number; // Which packet has data in it that is waiting for the interval to send |
| 503 |
|
| 504 |
uint16_t status; // Status of this connection |
| 505 |
uint16_t oursequence; // This is the next sequence number the application is expecting |
| 506 |
uint16_t theirsequence; // This is the next sequence number the peer is expecting |
| 507 |
uint16_t rsequence[MAXNETBUFFERS]; // This is the sequence number of the given packet |
| 508 |
|
| 509 |
uint8_t ping_pos; |
| 510 |
|
| 511 |
network_address net_addr; // A D3 network address structure |
| 512 |
network_protocol connection_type; // IPX, IP, modem, etc. |
| 513 |
reliable_net_rcvbuffer *rbuffers[MAXNETBUFFERS]; |
| 514 |
SOCKADDR addr; // SOCKADDR of our peer |
| 515 |
reliable_net_sendbuffer *sbuffers[MAXNETBUFFERS]; // This is an array of pointers for quick sorting |
| 516 |
uint16_t ssequence[MAXNETBUFFERS]; // This is the sequence number of the given packet |
| 517 |
uint8_t send_urgent; |
| 518 |
}; |
| 519 |
|
| 520 |
static reliable_socket reliable_sockets[MAXRELIABLESOCKETS]; |
| 521 |
//******************************* |
| 522 |
#if defined(POSIX) |
| 523 |
#include <fcntl.h> |
| 524 |
#endif |
| 525 |
|
| 526 |
int make_nonblocking(SOCKET sock) |
| 527 |
{ |
| 528 |
#ifdef WIN32 |
| 529 |
unsigned long arg = 1; |
| 530 |
return ioctlsocket(sock, FIONBIO, &arg); |
| 531 |
#else // WIN32 |
| 532 |
return fcntl(sock, F_SETFL, fcntl(sock, F_GETFL, 0) | O_NONBLOCK); |
| 533 |
#endif |
| 534 |
} |
| 535 |
|
| 536 |
void CloseNetworking() { |
| 537 |
if (Sockets_initted != 1) |
| 538 |
return; |
| 539 |
|
| 540 |
if (NetDebugFile) { |
| 541 |
cfprintf(NetDebugFile, "NetDebug.log closed at %f seconds.\n", timer_GetTime()); |
| 542 |
cfclose(NetDebugFile); |
| 543 |
NetDebugFile = NULL; |
| 544 |
} |
| 545 |
|
| 546 |
mprintf(0, "Shutting down networking...\n"); |
| 547 |
#ifdef WIN32 |
| 548 |
WSACancelBlockingCall(); |
| 549 |
|
| 550 |
dp_ShutdownDirectPlay(); |
| 551 |
#endif |
| 552 |
|
| 553 |
if (TCP_socket != INVALID_SOCKET) { |
| 554 |
shutdown(TCP_socket, 1); |
| 555 |
#ifdef WIN32 |
| 556 |
closesocket(TCP_socket); |
| 557 |
#else |
| 558 |
close(TCP_socket); |
| 559 |
#endif |
| 560 |
} |
| 561 |
#ifdef WIN32 |
| 562 |
if (WSACleanup()) { |
| 563 |
mprintf(0, "Error closing wsock!\n"); |
| 564 |
} |
| 565 |
#endif |
| 566 |
|
| 567 |
Network_initted = 0; |
| 568 |
Sockets_initted = 0; |
| 569 |
|
| 570 |
NetworkProtocol = NP_NONE; |
| 571 |
} |
| 572 |
|
| 573 |
// Inits the sockets layer to activity |
| 574 |
void nw_InitNetworking(int iReadBufSizeOverride) { |
| 575 |
if (iReadBufSizeOverride == -1) { |
| 576 |
iMaxReceiveBufsize = MAX_RECEIVE_BUFSIZE; |
| 577 |
} else { |
| 578 |
iMaxReceiveBufsize = iReadBufSizeOverride; |
| 579 |
} |
| 580 |
|
| 581 |
// reset our stats so we are ready to use them |
| 582 |
nw_GetNetworkStats(NULL); |
| 583 |
|
| 584 |
#ifdef WIN32 |
| 585 |
WSADATA ws_data; |
| 586 |
WORD ver = MAKEWORD(1, 1); |
| 587 |
#endif |
| 588 |
|
| 589 |
static char exewithpath[_MAX_PATH * 2]; |
| 590 |
static char exefile[_MAX_PATH * 2]; |
| 591 |
static char ourargs[_MAX_PATH * 2]; |
| 592 |
static char exedir[_MAX_PATH * 2]; |
| 593 |
static char exeext[_MAX_PATH]; |
| 594 |
static char *fixdir; |
| 595 |
static char szconntype[100]; |
| 596 |
int parmlen; |
| 597 |
int len = 99; |
| 598 |
Database->read("NetworkConnection", szconntype, &len); |
| 599 |
if (stricmp(szconntype, "DIALUP") == 0) { |
| 600 |
Dialup_connection = 1; |
| 601 |
} else { |
| 602 |
Dialup_connection = 0; |
| 603 |
} |
| 604 |
int iparg; |
| 605 |
|
| 606 |
if (FindArg("-netdebug")) { |
| 607 |
NetDebugFile = (CFILE *)cfopen("netdebug.log", "wa"); |
| 608 |
ASSERT(NetDebugFile); |
| 609 |
cfprintf(NetDebugFile, "NetDebug.log opened at %f seconds\n", timer_GetTime()); |
| 610 |
} |
| 611 |
|
| 612 |
iparg = FindArg("-useip"); |
| 613 |
if (!iparg) { |
| 614 |
iparg = FindArg("+ip"); |
| 615 |
} |
| 616 |
if (iparg) { |
| 617 |
Net_fixed_ip = inet_addr(GameArgs[iparg + 1]); |
| 618 |
if (Net_fixed_ip == INADDR_NONE) { |
| 619 |
Net_fixed_ip = INADDR_NONE; |
| 620 |
} |
| 621 |
} |
| 622 |
#ifdef WIN32 |
| 623 |
if (!dp_DidLobbyLaunchGame()) { |
| 624 |
// Tell direct play about this game |
| 625 |
char *p = GetCommandLine(); |
| 626 |
mprintf(0, "Command line: %s\n", p); |
| 627 |
parmlen = strlen(p); |
| 628 |
|
| 629 |
int a; |
| 630 |
for (a = 0; a < parmlen; ++a) { |
| 631 |
if (p[a] == ' ') { |
| 632 |
break; |
| 633 |
} |
| 634 |
} |
| 635 |
if (a < parmlen) { |
| 636 |
strcpy(ourargs, p + a + 1); |
| 637 |
} else { |
| 638 |
strcpy(ourargs, ""); |
| 639 |
} |
| 640 |
strncpy(exewithpath, p, a); |
| 641 |
exewithpath[a] = NULL; |
| 642 |
ddio_SplitPath(exewithpath, exedir, exefile, exeext); |
| 643 |
if (exedir[0] == '\"') { |
| 644 |
fixdir = exedir + 1; |
| 645 |
} else { |
| 646 |
fixdir = exedir; |
| 647 |
} |
| 648 |
if (exeext[strlen(exeext) - 1] == '\"') { |
| 649 |
exeext[strlen(exeext) - 1] = NULL; |
| 650 |
} |
| 651 |
strcat(exefile, exeext); |
| 652 |
// dp_RegisterLobbyApplication("Descent 3",exefile,fixdir,ourargs,Base_directory,"Descent 3"); |
| 653 |
} |
| 654 |
#endif |
| 655 |
|
| 656 |
#ifdef WIN32 |
| 657 |
int error = WSAStartup(ver, &ws_data); |
| 658 |
#else |
| 659 |
int error = 0; |
| 660 |
#endif |
| 661 |
if (error != 0) { |
| 662 |
mprintf(0, "There was an error initializing networking! Error=%d\n", error); |
| 663 |
return; |
| 664 |
} else { |
| 665 |
mprintf(0, "Network initted successfully!\n"); |
| 666 |
Network_initted = 1; |
| 667 |
atexit(CloseNetworking); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
// Sets the size of buffers |
| 672 |
void nw_SetSocketOptions(SOCKET sock) { |
| 673 |
int broadcast; |
| 674 |
int ret, cursize, bufsize, trysize; |
| 675 |
socklen_t cursizesize; |
| 676 |
|
| 677 |
// Set the mode of the socket to allow broadcasting. We need to be able to broadcast |
| 678 |
// when a game is searched for in IPX mode. |
| 679 |
broadcast = 1; |
| 680 |
setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (LPSTR)&broadcast, sizeof(broadcast)); |
| 681 |
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (LPSTR)&broadcast, sizeof(broadcast)); |
| 682 |
|
| 683 |
int error = make_nonblocking(sock); |
| 684 |
if (error == SOCKET_ERROR) { |
| 685 |
mprintf(0, "Unable to make socket non-blocking -- %d", WSAGetLastError()); |
| 686 |
} |
| 687 |
|
| 688 |
// try and increase the size of my receive buffer |
| 689 |
bufsize = iMaxReceiveBufsize; |
| 690 |
|
| 691 |
// set the current size of the receive buffer |
| 692 |
cursizesize = sizeof(int); |
| 693 |
getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (LPSTR)&cursize, &cursizesize); |
| 694 |
for (trysize = bufsize; trysize >= cursize; trysize >>= 1) { |
| 695 |
ret = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (LPSTR)&trysize, sizeof(trysize)); |
| 696 |
if (ret == SOCKET_ERROR) { |
| 697 |
int wserr; |
| 698 |
|
| 699 |
wserr = WSAGetLastError(); |
| 700 |
if ((wserr == WSAENOPROTOOPT) || (wserr == WSAEINVAL)) |
| 701 |
break; |
| 702 |
|
| 703 |
} else |
| 704 |
break; |
| 705 |
} |
| 706 |
getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (LPSTR)&cursize, &cursizesize); |
| 707 |
mprintf(0, "Receive buffer set to %d\n", cursize); |
| 708 |
/* |
| 709 |
// set the current size of the send buffer |
| 710 |
bufsize = MAX_RECEIVE_BUFSIZE/4; |
| 711 |
cursizesize = sizeof(int); |
| 712 |
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (LPSTR)&cursize, &cursizesize); |
| 713 |
for ( trysize = bufsize; trysize >= cursize; trysize >>= 1 ) |
| 714 |
{ |
| 715 |
ret = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (LPSTR)&trysize, sizeof(trysize)); |
| 716 |
if ( ret == SOCKET_ERROR ) |
| 717 |
{ |
| 718 |
int wserr; |
| 719 |
|
| 720 |
wserr = WSAGetLastError(); |
| 721 |
if ( (wserr == WSAENOPROTOOPT) || (wserr == WSAEINVAL) ) |
| 722 |
break; |
| 723 |
} |
| 724 |
else |
| 725 |
break; |
| 726 |
} |
| 727 |
getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (LPSTR)&cursize, &cursizesize); |
| 728 |
mprintf(0, "Send buffer set to %d\n", cursize); |
| 729 |
*/ |
| 730 |
} |
| 731 |
|
| 732 |
uint16_t nw_ListenPort = 0; |
| 733 |
|
| 734 |
// Inits the sockets that the application will be using |
| 735 |
void nw_InitSockets(uint16_t port) { |
| 736 |
|
| 737 |
nw_ListenPort = port; |
| 738 |
// UDP/TCP socket structure |
| 739 |
SOCKADDR_IN sock_addr; |
| 740 |
|
| 741 |
// Now do tcp! |
| 742 |
TCP_active = 0; |
| 743 |
TCP_socket = INVALID_SOCKET; |
| 744 |
// TCP_reliable_socket = INVALID_SOCKET; |
| 745 |
TCP_listen_socket = INVALID_SOCKET; |
| 746 |
|
| 747 |
// Initialize the UDP socket |
| 748 |
TCP_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 749 |
|
| 750 |
// Initialize all reliable sockets, IPX and TCP |
| 751 |
nw_InitReliableSocket(); |
| 752 |
|
| 753 |
if (TCP_socket != INVALID_SOCKET) { |
| 754 |
// bind the socket |
| 755 |
memset(&sock_addr, 0, sizeof(SOCKADDR_IN)); |
| 756 |
sock_addr.sin_family = AF_INET; |
| 757 |
|
| 758 |
uint32_t my_ip; |
| 759 |
|
| 760 |
my_ip = nw_GetThisIP(); |
| 761 |
|
| 762 |
memcpy(&sock_addr.sin_addr.s_addr, &my_ip, sizeof(uint32_t)); |
| 763 |
|
| 764 |
sock_addr.sin_port = htons(port); |
| 765 |
if (bind(TCP_socket, (SOCKADDR *)&sock_addr, sizeof(sock_addr)) == SOCKET_ERROR) { |
| 766 |
mprintf(0, "Couldn't bind TCP socket (%d)! Invalidating TCP\n", WSAGetLastError()); |
| 767 |
goto tcp_done; |
| 768 |
} |
| 769 |
|
| 770 |
nw_SetSocketOptions(TCP_socket); |
| 771 |
TCP_active = 1; |
| 772 |
} else { |
| 773 |
mprintf(0, "Cannot create TCP socket (%d)!\n", WSAGetLastError()); |
| 774 |
} |
| 775 |
|
| 776 |
tcp_done: |
| 777 |
int ret; |
| 778 |
uint32_t isocktrue = 1; |
| 779 |
|
| 780 |
setsockopt(TCP_socket, SOL_SOCKET, SO_REUSEADDR, (LPSTR)&isocktrue, sizeof(isocktrue)); |
| 781 |
ret = setsockopt(TCP_socket, SOL_SOCKET, SO_BROADCAST, (LPSTR)&isocktrue, sizeof(uint32_t)); |
| 782 |
if (ret == SOCKET_ERROR) { |
| 783 |
int wserr; |
| 784 |
wserr = WSAGetLastError(); |
| 785 |
if ((wserr == WSAENOPROTOOPT) || (wserr == WSAEINVAL)) { |
| 786 |
mprintf(0, "Unable to make socket broadcastable!"); |
| 787 |
|
| 788 |
Int3(); // Get Kevin |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
Sockets_initted = 1; |
| 793 |
|
| 794 |
if (TCP_active) |
| 795 |
mprintf(0, "TCP Initialized\n"); |
| 796 |
|
| 797 |
nw_psnet_buffer_init(); |
| 798 |
nw_RegisterCallback((NetworkReceiveCallback)nw_HandleUnreliableData, NWT_UNRELIABLE); |
| 799 |
} |
| 800 |
|
| 801 |
// Copies my address into the passed argument |
| 802 |
void nw_GetMyAddress(network_address *addr) { |
| 803 |
socklen_t len; |
| 804 |
SOCKADDR_IN in_addr; |
| 805 |
|
| 806 |
memset(&My_addr, 0, sizeof(network_address)); |
| 807 |
if (TCP_active) { |
| 808 |
// assign the TCP_* sockets to the socket values used elsewhere |
| 809 |
Unreliable_socket = &TCP_socket; |
| 810 |
Reliable_socket = &TCP_reliable_socket; |
| 811 |
Listen_socket = &TCP_listen_socket; |
| 812 |
|
| 813 |
// get the socket name for the TCP_socket, and put it into My_addr |
| 814 |
len = sizeof(SOCKADDR_IN); |
| 815 |
if (getsockname(*Unreliable_socket, (SOCKADDR *)&in_addr, &len) == SOCKET_ERROR) { |
| 816 |
mprintf(0, "Unable to get sock name for TCP unreliable socket (%s)\n", WSAGetLastError()); |
| 817 |
return; |
| 818 |
} |
| 819 |
|
| 820 |
memcpy(My_addr.address, &in_addr.sin_addr, 4); |
| 821 |
|
| 822 |
// My_addr.port = in_addr.sin_port; |
| 823 |
My_addr.port = ntohs(in_addr.sin_port); |
| 824 |
} |
| 825 |
|
| 826 |
*addr = My_addr; |
| 827 |
} |
| 828 |
|
| 829 |
// Returns internet address format from string address format...ie "204.243.217.14" |
| 830 |
// turns into 1414829242 |
| 831 |
uint32_t nw_GetHostAddressFromNumbers(char *str) { |
| 832 |
// ASSERT (NetworkProtocol==NP_TCP); |
| 833 |
|
| 834 |
return inet_addr(str); |
| 835 |
} |
| 836 |
|
| 837 |
// Fills in the string with the string address from the internet address |
| 838 |
void nw_GetNumbersFromHostAddress(network_address *address, char *str) { |
| 839 |
// ASSERT(NetworkProtocol==NP_TCP); |
| 840 |
ASSERT(str); |
| 841 |
struct in_addr addr; |
| 842 |
|
| 843 |
if (address->connection_type == NP_TCP) { |
| 844 |
memcpy(&addr, address->address, sizeof(struct in_addr)); |
| 845 |
sprintf(str, "IP: %s:%d", inet_ntoa(addr), address->port); |
| 846 |
} |
| 847 |
|
| 848 |
#ifdef WIN32 |
| 849 |
else if (Use_DirectPlay && (address->connection_type == NP_DIRECTPLAY)) { |
| 850 |
DPID id; |
| 851 |
memcpy(&id, address->address, sizeof(DPID)); |
| 852 |
sprintf(str, "DirectPlay: 0x%x", id); |
| 853 |
} |
| 854 |
#endif |
| 855 |
} |
| 856 |
|
| 857 |
#define CLOSE_TIMEOUT_TIME 3 // 3 seconds |
| 858 |
|
| 859 |
// returns the ip address of this computer |
| 860 |
uint32_t nw_GetThisIP() { |
| 861 |
SOCKADDR_IN local_address; |
| 862 |
int address_size = sizeof(SOCKADDR); |
| 863 |
|
| 864 |
if (Net_fixed_ip != INADDR_NONE) { |
| 865 |
return Net_fixed_ip; |
| 866 |
} |
| 867 |
// Init local address to zero |
| 868 |
local_address.sin_addr.s_addr = INADDR_ANY; |
| 869 |
if (Dialup_connection) { |
| 870 |
#ifdef WIN32 |
| 871 |
local_address.sin_addr.s_addr = psnet_ras_status(); |
| 872 |
#else |
| 873 |
local_address.sin_addr.s_addr = INADDR_ANY; |
| 874 |
#endif |
| 875 |
} |
| 876 |
if ((!Dialup_connection) || (!local_address.sin_addr.s_addr)) { |
| 877 |
// Removed the fancy way, it worked worse than the easy way (some adsl/cable people had problems. |
| 878 |
/* |
| 879 |
// Get the local host name |
| 880 |
ret = gethostname(local, 255 ); |
| 881 |
if (ret != SOCKET_ERROR ) |
| 882 |
{ |
| 883 |
// Resolve host name for local address |
| 884 |
hostent = gethostbyname((LPSTR)local); |
| 885 |
if ( hostent ) |
| 886 |
local_address.sin_addr.s_addr = *((u_long FAR *)(hostent->h_addr)); |
| 887 |
} |
| 888 |
*/ |
| 889 |
local_address.sin_addr.s_addr = INADDR_ANY; |
| 890 |
} |
| 891 |
return local_address.sin_addr.s_addr; |
| 892 |
} |
| 893 |
|
| 894 |
// Calculates a unique uint16_t checksum for a stream of data |
| 895 |
uint16_t nw_CalculateChecksum(void *vptr, int len) { |
| 896 |
uint8_t *ptr = (uint8_t *)vptr; |
| 897 |
uint32_t sum1, sum2; |
| 898 |
|
| 899 |
sum1 = sum2 = 0; |
| 900 |
|
| 901 |
while (len--) { |
| 902 |
sum1 += *ptr++; |
| 903 |
if (sum1 >= 255) |
| 904 |
sum1 -= 255; |
| 905 |
sum2 += sum1; |
| 906 |
} |
| 907 |
sum2 %= 255; |
| 908 |
|
| 909 |
return (uint16_t)((sum1 << 8) + sum2); |
| 910 |
} |
| 911 |
|
| 912 |
// Sends data on an unreliable socket |
| 913 |
int nw_Send(network_address *who_to, void *data, int len, int flags) { |
| 914 |
if (len == 0) { |
| 915 |
mprintf(0, "Attempting to send 0 byte network packet in nw_Send()\n"); |
| 916 |
Int3(); |
| 917 |
} |
| 918 |
if (NetDebugFile) { |
| 919 |
uint8_t *ptr = (uint8_t *)data; |
| 920 |
cfprintf(NetDebugFile, "nw_Send packet of type %d at %f seconds.\n", ptr[0], timer_GetTime()); |
| 921 |
} |
| 922 |
return nw_SendWithID(NWT_UNRELIABLE, (uint8_t *)data, len, who_to); |
| 923 |
} |
| 924 |
|
| 925 |
// MTS: only used in this file? |
| 926 |
void nw_HandleUnreliableData(uint8_t *data, int len, network_address *from_addr) { |
| 927 |
nw_psnet_buffer_packet((uint8_t *)data, len, from_addr); |
| 928 |
} |
| 929 |
|
| 930 |
// MTS: unused? |
| 931 |
// routine to "free" a packet buffer |
| 932 |
void nw_FreePacket(int id) { |
| 933 |
Packet_buffers[id].sequence_number = -1; |
| 934 |
Packet_free_list[--Num_packet_buffers] = (int16_t)id; |
| 935 |
if (Largest_packet_index == id) |
| 936 |
while ((--Largest_packet_index > 0) && (Packet_buffers[Largest_packet_index].sequence_number == -1)) |
| 937 |
; |
| 938 |
} |
| 939 |
|
| 940 |
// nw_Recieve will call the above function to read data out of the socket. It will then determine |
| 941 |
// which of the buffers we should use and pass to the routine which called us |
| 942 |
int nw_Receive(void *data, network_address *from_addr) { |
| 943 |
// call the routine to read data out of the socket (which stuffs it into the packet buffers) |
| 944 |
|
| 945 |
if (Use_DirectPlay) { |
| 946 |
#ifdef WIN32 |
| 947 |
dp_DirectPlayDispatch(); |
| 948 |
#endif |
| 949 |
} else { |
| 950 |
// nw_ReceiveFromSocket(); |
| 951 |
nw_DoReceiveCallbacks(); |
| 952 |
} |
| 953 |
|
| 954 |
int buffer_size; |
| 955 |
|
| 956 |
// try and get a free buffer and return its size |
| 957 |
if (nw_psnet_buffer_get_next((uint8_t *)data, &buffer_size, from_addr)) { |
| 958 |
return buffer_size; |
| 959 |
} |
| 960 |
return 0; |
| 961 |
} |
| 962 |
|
| 963 |
int ExtraBufferTempHack = 0; |
| 964 |
// Temp hack to figure out this buffer overflow thing |
| 965 |
|
| 966 |
// Return codes: |
| 967 |
//-1 socket not connected |
| 968 |
// 0 No packet ready to receive |
| 969 |
// >0 Buffer filled with the number of bytes recieved |
| 970 |
|
| 971 |
int nw_ReceiveReliable(SOCKET socketid, uint8_t *buffer, int max_len) { |
| 972 |
|
| 973 |
int i; |
| 974 |
if (Use_DirectPlay) { |
| 975 |
#ifdef WIN32 |
| 976 |
dp_DirectPlayDispatch(); |
| 977 |
|
| 978 |
// try and get a free buffer and return its size |
| 979 |
if (nw_psnet_buffer_get_next_by_packet_id((uint8_t *)buffer, &max_len, socketid)) { |
| 980 |
return max_len; |
| 981 |
} |
| 982 |
return 0; |
| 983 |
#endif |
| 984 |
} |
| 985 |
reliable_socket *rsocket = NULL; |
| 986 |
// nw_WorkReliable(); |
| 987 |
nw_DoReceiveCallbacks(); |
| 988 |
if (socketid >= MAXRELIABLESOCKETS) { |
| 989 |
mprintf(0, "Invalid socket id passed to nw_NewReceiveReliable() -- %d\n", socketid); |
| 990 |
return -1; |
| 991 |
} |
| 992 |
rsocket = &reliable_sockets[socketid]; |
| 993 |
if ((RNF_CONNECTED != rsocket->status) && (RNF_LIMBO != rsocket->status)) { |
| 994 |
mprintf(0, "Can't receive packet because it isn't connected in nw_ReceiveReliable(). socket = %d\n", socketid); |
| 995 |
return 0; |
| 996 |
} |
| 997 |
// If the buffer position is the position we are waiting for, fill in |
| 998 |
// the buffer we received in the call to this function and return true |
| 999 |
|
| 1000 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 1001 |
if ((rsocket->rsequence[i] == rsocket->oursequence) && (rsocket->rbuffers[i])) { |
| 1002 |
memcpy(buffer, rsocket->rbuffers[i]->buffer, rsocket->recv_len[i]); |
| 1003 |
mem_free(rsocket->rbuffers[i]); |
| 1004 |
rsocket->rbuffers[i] = NULL; |
| 1005 |
rsocket->rsequence[i] = 0; |
| 1006 |
/* |
| 1007 |
mprintf(0,"Found packet for upper layer in nw_ReceiveReliable() %d bytes. seq:%d.\n", |
| 1008 |
rsocket->recv_len[i], |
| 1009 |
rsocket->oursequence); |
| 1010 |
*/ |
| 1011 |
rsocket->oursequence++; |
| 1012 |
return rsocket->recv_len[i]; |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
return 0; |
| 1017 |
} |
| 1018 |
|
| 1019 |
// This function will look for a control packet, indicating a |
| 1020 |
// desire to establish a reliable link. |
| 1021 |
// When is makes a link, it will return a SOCKET, and fill in from_addr. |
| 1022 |
// if there is no waiting line, it returns -1 |
| 1023 |
int nw_CheckListenSocket(network_address *from_addr) { |
| 1024 |
SOCKADDR_IN *ip_addr; // UDP/TCP socket structure |
| 1025 |
|
| 1026 |
#ifdef WIN32 |
| 1027 |
DPID id; |
| 1028 |
#endif |
| 1029 |
|
| 1030 |
if (Use_DirectPlay) { |
| 1031 |
#ifdef WIN32 |
| 1032 |
// look for a pending connection |
| 1033 |
for (int i = 0; i < MAX_PENDING_NEW_CONNECTIONS; i++) { |
| 1034 |
if (Pending_dp_conn[i] != DPID_UNKNOWN) { |
| 1035 |
memset(from_addr, 0, sizeof(network_address)); |
| 1036 |
memcpy(from_addr->address, &Pending_dp_conn[i], sizeof(DPID)); |
| 1037 |
from_addr->connection_type = NP_DIRECTPLAY; |
| 1038 |
id = Pending_dp_conn[i]; |
| 1039 |
Pending_dp_conn[i] = DPID_UNKNOWN; |
| 1040 |
mprintf(0, "New DirectPlay connection in nw_CheckListenSocket().\n"); |
| 1041 |
return id; |
| 1042 |
} |
| 1043 |
} |
| 1044 |
return -1; |
| 1045 |
#endif |
| 1046 |
} |
| 1047 |
|
| 1048 |
// nw_WorkReliable(); |
| 1049 |
nw_DoReceiveCallbacks(); |
| 1050 |
int i; |
| 1051 |
for (i = 1; i < MAXRELIABLESOCKETS; i++) { |
| 1052 |
if (reliable_sockets[i].status == RNF_CONNECTING) { |
| 1053 |
reliable_sockets[i].status = RNF_CONNECTED; |
| 1054 |
// memcpy(from_addr,&reliable_sockets[i].addr,sizeof(SOCKADDR)); |
| 1055 |
mprintf(0, "New reliable connection in nw_CheckListenSocket().\n"); |
| 1056 |
|
| 1057 |
switch (reliable_sockets[i].connection_type) { |
| 1058 |
case NP_TCP: |
| 1059 |
ip_addr = (SOCKADDR_IN *)&reliable_sockets[i].addr; |
| 1060 |
memset(from_addr, 0x00, sizeof(network_address)); |
| 1061 |
from_addr->port = ntohs(ip_addr->sin_port); |
| 1062 |
from_addr->connection_type = NP_TCP; |
| 1063 |
#ifdef WIN32 |
| 1064 |
memcpy(from_addr->address, &ip_addr->sin_addr.S_un.S_addr, 4); |
| 1065 |
#else |
| 1066 |
memcpy(from_addr->address, &ip_addr->sin_addr.s_addr, 4); |
| 1067 |
#endif |
| 1068 |
break; |
| 1069 |
|
| 1070 |
default: |
| 1071 |
Int3(); |
| 1072 |
break; |
| 1073 |
} |
| 1074 |
char dbg_output[50]; |
| 1075 |
nw_GetNumbersFromHostAddress(from_addr, dbg_output); |
| 1076 |
mprintf(0, "Got address from: %s\n", dbg_output); |
| 1077 |
return i; |
| 1078 |
} |
| 1079 |
} |
| 1080 |
return INVALID_SOCKET; |
| 1081 |
} |
| 1082 |
|
| 1083 |
int nw_SendReliable(uint32_t socketid, uint8_t *data, int length, bool urgent) { |
| 1084 |
int i; |
| 1085 |
int bytesout; |
| 1086 |
int use_buffer = -1; |
| 1087 |
reliable_socket *rsocket; |
| 1088 |
reliable_header send_header; |
| 1089 |
int send_this_packet = 1; |
| 1090 |
|
| 1091 |
if (length == 0) { |
| 1092 |
mprintf(0, "Attempting to send 0 byte network packet in nw_SendReliable()\n"); |
| 1093 |
Int3(); |
| 1094 |
} |
| 1095 |
// mprintf(0,"Socket id passed to nw_NewSendReliable() -- %d\n",socketid); |
| 1096 |
|
| 1097 |
if (NetDebugFile) { |
| 1098 |
cfprintf(NetDebugFile, "nw_SendReliable packet of type %d at %f seconds.\n", data[0], timer_GetTime()); |
| 1099 |
} |
| 1100 |
|
| 1101 |
if (Use_DirectPlay) { |
| 1102 |
#ifdef WIN32 |
| 1103 |
network_address who_to; |
| 1104 |
who_to.connection_type = NP_DIRECTPLAY; |
| 1105 |
memcpy(&who_to.address, &socketid, sizeof(DPID)); |
| 1106 |
return dp_DirectPlaySend(&who_to, data, length, true); |
| 1107 |
#endif |
| 1108 |
} |
| 1109 |
|
| 1110 |
ASSERT(length < sizeof(reliable_header)); |
| 1111 |
// nw_WorkReliable(); |
| 1112 |
nw_DoReceiveCallbacks(); |
| 1113 |
|
| 1114 |
if (socketid >= MAXRELIABLESOCKETS) { |
| 1115 |
mprintf(0, "Invalid socket id passed to nw_NewSendReliable() -- %d\n", socketid); |
| 1116 |
return -1; |
| 1117 |
} |
| 1118 |
|
| 1119 |
rsocket = &reliable_sockets[socketid]; |
| 1120 |
if (rsocket->status != RNF_CONNECTED) { |
| 1121 |
// We can't send because this isn't a connected reliable socket. |
| 1122 |
mprintf(0, "Can't send packet because of status %d in nw_SendReliable(). socket = %d\n", rsocket->status, socketid); |
| 1123 |
return -1; |
| 1124 |
} |
| 1125 |
if (urgent) |
| 1126 |
rsocket->send_urgent = 1; |
| 1127 |
// See if there is a packet waiting to be sent |
| 1128 |
if (-1 != rsocket->waiting_packet_number) { |
| 1129 |
int pnum = rsocket->waiting_packet_number; |
| 1130 |
ASSERT(rsocket->sbuffers[pnum]); |
| 1131 |
// See if there's room for this data |
| 1132 |
if (sizeof(reliable_net_sendbuffer) < (rsocket->send_len[pnum] + length)) { |
| 1133 |
// Send the previous packet, then use the normal code to generate a new packet |
| 1134 |
mprintf(0, "Pending reliable packet buffer full, sending packet now.\n"); |
| 1135 |
rsocket->waiting_packet_number = -1; |
| 1136 |
|
| 1137 |
use_buffer = pnum; |
| 1138 |
network_address send_address; |
| 1139 |
memset(&send_address, 0, sizeof(network_address)); |
| 1140 |
|
| 1141 |
memcpy(send_header.data, rsocket->sbuffers[pnum], rsocket->send_len[pnum]); |
| 1142 |
send_header.data_len = INTEL_SHORT(rsocket->send_len[pnum]); |
| 1143 |
send_header.type = RNT_DATA; |
| 1144 |
send_header.send_time = INTEL_FLOAT(timer_GetTime()); |
| 1145 |
|
| 1146 |
send_address.connection_type = rsocket->connection_type; |
| 1147 |
|
| 1148 |
if (NP_TCP == rsocket->connection_type) { |
| 1149 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&rsocket->addr; |
| 1150 |
memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 1151 |
send_address.port = htons(inaddr->sin_port); |
| 1152 |
send_address.connection_type = NP_TCP; |
| 1153 |
} |
| 1154 |
|
| 1155 |
// mprintf(0,"Sending reliable packet! Sequence %d\n",send_header.seq); |
| 1156 |
bytesout = nw_SendWithID(NWT_RELIABLE, (uint8_t *)&send_header, |
| 1157 |
RELIABLE_PACKET_HEADER_ONLY_SIZE + rsocket->send_len[use_buffer], &send_address); |
| 1158 |
|
| 1159 |
if ((bytesout == SOCKET_ERROR) && (WSAEWOULDBLOCK == WSAGetLastError())) { |
| 1160 |
// This will cause it to try to send again next frame. (or sooner) |
| 1161 |
rsocket->timesent[use_buffer] = timer_GetTime() - (NETRETRYTIME * 4); |
| 1162 |
} else { |
| 1163 |
rsocket->timesent[use_buffer] = timer_GetTime(); |
| 1164 |
} |
| 1165 |
} else { |
| 1166 |
// tack this data on the end of the previous packet |
| 1167 |
// mprintf(0,"Appending to delayed packet...\n"); |
| 1168 |
ASSERT(rsocket->sbuffers[pnum]); |
| 1169 |
memcpy(rsocket->sbuffers[pnum]->buffer + rsocket->send_len[pnum], data, length); |
| 1170 |
// int msize = mem_size(rsocket->sbuffers[pnum]); |
| 1171 |
rsocket->send_len[pnum] += length; |
| 1172 |
return length; |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
// Add the new packet to the sending list and send it. |
| 1177 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 1178 |
use_buffer = -1; |
| 1179 |
if (NULL == rsocket->sbuffers[i]) { |
| 1180 |
// mprintf(0,"Sending in nw_SendReliable() %d bytes seq=%d.\n",length,rsocket->theirsequence); |
| 1181 |
|
| 1182 |
rsocket->send_len[i] = length; |
| 1183 |
rsocket->sbuffers[i] = (reliable_net_sendbuffer *)mem_malloc(sizeof(reliable_net_sendbuffer)); |
| 1184 |
|
| 1185 |
memcpy(rsocket->sbuffers[i]->buffer, data, length); |
| 1186 |
|
| 1187 |
send_header.seq = INTEL_SHORT(rsocket->theirsequence); |
| 1188 |
rsocket->ssequence[i] = rsocket->theirsequence; |
| 1189 |
|
| 1190 |
use_buffer = i; |
| 1191 |
rsocket->waiting_packet_number = i; |
| 1192 |
|
| 1193 |
rsocket->theirsequence++; |
| 1194 |
return length; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
mprintf(0, "Can't send packet because a buffer overflow nw_SendReliable(). socket = %d\n", socketid); |
| 1198 |
rsocket->status = RNF_BROKEN; |
| 1199 |
|
| 1200 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 1201 |
if (rsocket->sbuffers[i]) { |
| 1202 |
mprintf(0, "Buffer %d: %d,%d,%d,%d,%d,%d\n", |
| 1203 |
i, |
| 1204 |
rsocket->sbuffers[i]->buffer[0], |
| 1205 |
rsocket->sbuffers[i]->buffer[1], |
| 1206 |
rsocket->sbuffers[i]->buffer[2], |
| 1207 |
rsocket->sbuffers[i]->buffer[3], |
| 1208 |
rsocket->sbuffers[i]->buffer[4], |
| 1209 |
rsocket->sbuffers[i]->buffer[5]); |
| 1210 |
} |
| 1211 |
} |
| 1212 |
|
| 1213 |
// Error ("Couldn't send packet because of buffer overflow!"); |
| 1214 |
|
| 1215 |
// Int3(); |
| 1216 |
return 0; |
| 1217 |
} |
| 1218 |
|
| 1219 |
// MTS: only used in this file |
| 1220 |
int nw_InitReliableSocket() { |
| 1221 |
nw_RegisterCallback((NetworkReceiveCallback)nw_WorkReliable, NWT_RELIABLE); |
| 1222 |
return 1; |
| 1223 |
} |
| 1224 |
// MTS: only used in this file |
| 1225 |
void nw_SendReliableAck(SOCKADDR *raddr, uint32_t sig, network_protocol link_type, float time_sent) { |
| 1226 |
int ret; |
| 1227 |
reliable_header ack_header; |
| 1228 |
ack_header.type = RNT_ACK; |
| 1229 |
// mprintf(0,"Sending ACK for sig %d.\n",sig); |
| 1230 |
ack_header.data_len = INTEL_SHORT((int16_t)sizeof(uint32_t)); |
| 1231 |
ack_header.send_time = INTEL_FLOAT(time_sent); |
| 1232 |
sig = INTEL_INT(sig); |
| 1233 |
memcpy(&ack_header.data, &sig, sizeof(uint32_t)); |
| 1234 |
|
| 1235 |
network_address send_address; |
| 1236 |
memset(&send_address, 0, sizeof(network_address)); |
| 1237 |
|
| 1238 |
send_address.connection_type = link_type; |
| 1239 |
|
| 1240 |
if (NP_TCP == link_type) { |
| 1241 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)raddr; |
| 1242 |
memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 1243 |
send_address.port = htons(inaddr->sin_port); |
| 1244 |
send_address.connection_type = NP_TCP; |
| 1245 |
} |
| 1246 |
|
| 1247 |
ret = nw_SendWithID(NWT_RELIABLE, (uint8_t *)&ack_header, RELIABLE_PACKET_HEADER_ONLY_SIZE + sizeof(uint32_t), |
| 1248 |
&send_address); |
| 1249 |
} |
| 1250 |
|
| 1251 |
void nw_DoNetworkIdle(void) { |
| 1252 |
if (!Use_DirectPlay) { |
| 1253 |
nw_DoReceiveCallbacks(); |
| 1254 |
nw_ReliableResend(); |
| 1255 |
} |
| 1256 |
} |
| 1257 |
|
| 1258 |
#define CONNECTSEQ 0x142 // Magic number for starting a connection, just so it isn't 0 |
| 1259 |
|
| 1260 |
void nw_WorkReliable(uint8_t *data, int len, network_address *naddr) { |
| 1261 |
int i; |
| 1262 |
int rcode = -1; |
| 1263 |
int16_t max_len = NETBUFFERSIZE; |
| 1264 |
static reliable_header rcv_buff; |
| 1265 |
static SOCKADDR rcv_addr; |
| 1266 |
int bytesin = 0; |
| 1267 |
int addrlen = sizeof(SOCKADDR); |
| 1268 |
uint32_t rcvid; // The id of who we actually received a packet from, as opposed to socketid parm |
| 1269 |
|
| 1270 |
if (NP_TCP == naddr->connection_type) { |
| 1271 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&rcv_addr; |
| 1272 |
memcpy(&inaddr->sin_addr, &naddr->address, 4); |
| 1273 |
inaddr->sin_port = htons(naddr->port); |
| 1274 |
} |
| 1275 |
|
| 1276 |
if (Net_connect_sequence == R_NET_SEQUENCE_CONNECTING) { |
| 1277 |
nw_HandleConnectResponse(data, len, naddr); |
| 1278 |
return; |
| 1279 |
} |
| 1280 |
// Check for incoming data |
| 1281 |
|
| 1282 |
reliable_socket *rsocket = NULL; |
| 1283 |
// Check to see if we need to send a packet out. |
| 1284 |
if ((serverconn != -1) && (reliable_sockets[serverconn].status == RNF_LIMBO) && |
| 1285 |
((timer_GetTime() - last_sent_iamhere) > NETRETRYTIME)) { |
| 1286 |
reliable_header conn_header; |
| 1287 |
// Now send I_AM_HERE packet |
| 1288 |
conn_header.type = RNT_I_AM_HERE; |
| 1289 |
conn_header.seq = INTEL_SHORT((int16_t)(~CONNECTSEQ)); |
| 1290 |
conn_header.data_len = INTEL_SHORT((int16_t)0); |
| 1291 |
last_sent_iamhere = timer_GetTime(); |
| 1292 |
network_address send_address; |
| 1293 |
memset(&send_address, 0, sizeof(network_address)); |
| 1294 |
|
| 1295 |
send_address.connection_type = reliable_sockets[serverconn].connection_type; |
| 1296 |
if (NP_TCP == send_address.connection_type) { |
| 1297 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&reliable_sockets[serverconn].addr; |
| 1298 |
memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 1299 |
send_address.port = htons(inaddr->sin_port); |
| 1300 |
send_address.connection_type = NP_TCP; |
| 1301 |
} |
| 1302 |
|
| 1303 |
int ret = nw_SendWithID(NWT_RELIABLE, (uint8_t *)&conn_header, RELIABLE_PACKET_HEADER_ONLY_SIZE, &send_address); |
| 1304 |
|
| 1305 |
if ((ret == SOCKET_ERROR) && (WSAEWOULDBLOCK == WSAGetLastError())) { |
| 1306 |
reliable_sockets[serverconn].last_packet_sent = timer_GetTime() - NETRETRYTIME; |
| 1307 |
} else { |
| 1308 |
reliable_sockets[serverconn].last_packet_sent = timer_GetTime(); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
network_protocol link_type = naddr->connection_type; |
| 1312 |
network_address d3_rcv_addr; |
| 1313 |
memcpy(&d3_rcv_addr, naddr, sizeof(network_address)); |
| 1314 |
memcpy((uint8_t *)&rcv_buff, data, len); |
| 1315 |
SOCKADDR_IN *rcvaddr, *rsockaddr; |
| 1316 |
|
| 1317 |
do { |
| 1318 |
|
| 1319 |
rsocket = NULL; |
| 1320 |
if (len) { |
| 1321 |
// Someone wants to connect, so find a slot |
| 1322 |
if (rcv_buff.type == RNT_REQ_CONN) { |
| 1323 |
for (i = 1; i < MAXRELIABLESOCKETS; i++) { |
| 1324 |
if ((reliable_sockets[i].status == RNF_CONNECTED) || (reliable_sockets[i].status == RNF_LIMBO)) |
| 1325 |
// if(memcmp(&rcv_addr,&reliable_sockets[i].addr,sizeof(SOCKADDR))==0) |
| 1326 |
if (memcmp(&d3_rcv_addr, &reliable_sockets[i].net_addr, sizeof(network_address)) == 0) |
| 1327 |
// d3_rcv_addr |
| 1328 |
{ |
| 1329 |
// We already have a reliable link to this user, so we will ignore it... |
| 1330 |
mprintf(0, "Received duplicate connection request. %d\n", i); |
| 1331 |
// reliable_sockets[i].last_packet_received = timer_GetTime(); |
| 1332 |
nw_SendReliableAck(&reliable_sockets[i].addr, INTEL_SHORT(rcv_buff.seq), link_type, |
| 1333 |
INTEL_FLOAT(rcv_buff.send_time)); |
| 1334 |
// We will change this as a hack to prevent later code from hooking us up |
| 1335 |
rcv_buff.type = 0xff; |
| 1336 |
continue; |
| 1337 |
} |
| 1338 |
} |
| 1339 |
for (i = 1; i < MAXRELIABLESOCKETS; i++) { |
| 1340 |
if (reliable_sockets[i].status == RNF_UNUSED) { |
| 1341 |
// Add the new connection here. |
| 1342 |
reliable_sockets[i].connection_type = link_type; |
| 1343 |
memcpy(&reliable_sockets[i].net_addr, naddr, sizeof(network_address)); |
| 1344 |
memcpy(&reliable_sockets[i].addr, &rcv_addr, sizeof(SOCKADDR)); |
| 1345 |
reliable_sockets[i].ping_pos = 0; |
| 1346 |
reliable_sockets[i].num_ping_samples = 0; |
| 1347 |
reliable_sockets[i].status = RNF_LIMBO; |
| 1348 |
reliable_sockets[i].last_packet_received = timer_GetTime(); |
| 1349 |
reliable_sockets[i].last_sent = timer_GetTime(); |
| 1350 |
reliable_sockets[i].waiting_packet_number = -1; |
| 1351 |
reliable_sockets[i].send_urgent = 0; |
| 1352 |
|
| 1353 |
rsocket = &reliable_sockets[i]; |
| 1354 |
rcvaddr = (SOCKADDR_IN *)&rcv_addr; |
| 1355 |
// Int3(); |
| 1356 |
mprintf(0, "Connect from %s:%d\n", inet_ntoa(rcvaddr->sin_addr), htons(rcvaddr->sin_port)); |
| 1357 |
break; |
| 1358 |
} |
| 1359 |
} |
| 1360 |
if (i == MAXRELIABLESOCKETS) { |
| 1361 |
// No more connections! |
| 1362 |
mprintf(0, "Out of incoming reliable connection sockets\n"); |
| 1363 |
// Int3();//See Kevin |
| 1364 |
continue; |
| 1365 |
} |
| 1366 |
nw_SendReliableAck(&rsocket->addr, INTEL_SHORT(rcv_buff.seq), link_type, INTEL_FLOAT(rcv_buff.send_time)); |
| 1367 |
} |
| 1368 |
|
| 1369 |
// Find out if this is a packet from someone we were expecting a packet. |
| 1370 |
rcvaddr = (SOCKADDR_IN *)&rcv_addr; |
| 1371 |
for (i = 1; i < MAXRELIABLESOCKETS; i++) { |
| 1372 |
|
| 1373 |
rsockaddr = (SOCKADDR_IN *)&reliable_sockets[i].addr; |
| 1374 |
if (memcmp(&d3_rcv_addr, &reliable_sockets[i].net_addr, sizeof(network_address)) == 0) { |
| 1375 |
rsocket = &reliable_sockets[i]; |
| 1376 |
rcvid = i; |
| 1377 |
break; |
| 1378 |
} |
| 1379 |
} |
| 1380 |
if (NULL == rsocket) { |
| 1381 |
mprintf(0, "Received reliable data from unconnected client.\n"); |
| 1382 |
char addrstr[200]; |
| 1383 |
nw_GetNumbersFromHostAddress(&d3_rcv_addr, addrstr); |
| 1384 |
mprintf(0, "Received from %s\n", addrstr); |
| 1385 |
continue; |
| 1386 |
} |
| 1387 |
rsocket->last_packet_received = timer_GetTime(); |
| 1388 |
|
| 1389 |
if (rsocket->status != RNF_CONNECTED) { |
| 1390 |
// Get out of limbo |
| 1391 |
if (rsocket->status == RNF_LIMBO) { |
| 1392 |
// this is our connection to the server |
| 1393 |
if ((serverconn != -1)) { |
| 1394 |
if (rcv_buff.type == RNT_ACK) { |
| 1395 |
int *acknum = (int *)&rcv_buff.data; |
| 1396 |
*acknum = INTEL_INT(*acknum); |
| 1397 |
if (*acknum == (~CONNECTSEQ & 0xffff)) { |
| 1398 |
rsocket->status = RNF_CONNECTED; |
| 1399 |
mprintf(0, "Got ACK for IAMHERE!\n"); |
| 1400 |
} |
| 1401 |
continue; |
| 1402 |
} |
| 1403 |
} else if (rcv_buff.type == RNT_I_AM_HERE) { |
| 1404 |
rsocket->status = RNF_CONNECTING; |
| 1405 |
nw_SendReliableAck(&rsocket->addr, INTEL_SHORT(rcv_buff.seq), link_type, INTEL_FLOAT(rcv_buff.send_time)); |
| 1406 |
mprintf(0, "Got IAMHERE!\n"); |
| 1407 |
continue; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
if ((rcv_buff.type == RNT_DATA) && (serverconn != -1)) { |
| 1411 |
rsocket->status = RNF_CONNECTED; |
| 1412 |
} else { |
| 1413 |
// mprintf(0,"Packet from nonconnected socket -- seq: %d status: %d\n",rcv_buff.seq,rsocket->status); |
| 1414 |
rsocket->last_packet_received = timer_GetTime(); |
| 1415 |
continue; |
| 1416 |
} |
| 1417 |
} |
| 1418 |
// Update the last recv variable so we don't need a heartbeat |
| 1419 |
rsocket->last_packet_received = timer_GetTime(); |
| 1420 |
|
| 1421 |
if (rcv_buff.type == RNT_HEARTBEAT) { |
| 1422 |
continue; |
| 1423 |
} |
| 1424 |
if (rcv_buff.type == RNT_ACK) { |
| 1425 |
// Update ping time |
| 1426 |
rsocket->num_ping_samples++; |
| 1427 |
|
| 1428 |
rsocket->pings[rsocket->ping_pos] = rsocket->last_packet_received - INTEL_FLOAT(rcv_buff.send_time); |
| 1429 |
// mprintf(0,"ping time: %f\n",rsocket->pings[rsocket->ping_pos]); |
| 1430 |
if (rsocket->num_ping_samples >= MAX_PING_HISTORY) { |
| 1431 |
float sort_ping[MAX_PING_HISTORY]; |
| 1432 |
for (int a = 0; a < MAX_PING_HISTORY; a++) |
| 1433 |
sort_ping[a] = rsocket->pings[a]; |
| 1434 |
|
| 1435 |
qsort(sort_ping, MAX_PING_HISTORY, sizeof(float), nw_PingCompare); |
| 1436 |
rsocket->mean_ping = ((sort_ping[MAX_PING_HISTORY / 2] + sort_ping[(MAX_PING_HISTORY / 2) + 1])) / 2; |
| 1437 |
// mprintf_at(2,i+1,0,"Ping: %f ",rsocket->mean_ping); |
| 1438 |
} |
| 1439 |
rsocket->ping_pos++; |
| 1440 |
if (rsocket->ping_pos >= MAX_PING_HISTORY) { |
| 1441 |
rsocket->ping_pos = 0; |
| 1442 |
} |
| 1443 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 1444 |
uint32_t *acksig = (uint32_t *)&rcv_buff.data; |
| 1445 |
*acksig = INTEL_INT(*acksig); |
| 1446 |
if (rsocket) |
| 1447 |
if (rsocket->sbuffers[i]) |
| 1448 |
if (rsocket->ssequence[i] == *acksig) { |
| 1449 |
// mprintf(0,"Received ACK %d\n",*acksig); |
| 1450 |
mem_free(rsocket->sbuffers[i]); |
| 1451 |
rsocket->sbuffers[i] = NULL; |
| 1452 |
rsocket->ssequence[i] = 0; |
| 1453 |
} |
| 1454 |
} |
| 1455 |
// remove that packet from the send buffer |
| 1456 |
rsocket->last_packet_received = timer_GetTime(); |
| 1457 |
continue; |
| 1458 |
} |
| 1459 |
|
| 1460 |
if (rcv_buff.type == RNT_DATA_COMP) { |
| 1461 |
// More2Come |
| 1462 |
// Decompress it. Put it back in the buffer. Process it as RNT_DATA |
| 1463 |
rcv_buff.type = RNT_DATA; |
| 1464 |
} |
| 1465 |
if (rcv_buff.type == RNT_DATA) { |
| 1466 |
|
| 1467 |
// If the data is out of order by >= MAXNETBUFFERS-1 ignore that packet for now |
| 1468 |
int seqdelta; |
| 1469 |
seqdelta = INTEL_SHORT(rcv_buff.seq) - rsocket->oursequence; |
| 1470 |
if (seqdelta < 0) |
| 1471 |
seqdelta = seqdelta * -1; |
| 1472 |
if (seqdelta >= MAXNETBUFFERS - 1) { |
| 1473 |
mprintf(0, "Received reliable packet out of order!\n"); |
| 1474 |
// It's out of order, so we won't ack it, which will mean we will get it again soon. |
| 1475 |
continue; |
| 1476 |
} |
| 1477 |
// else move data into the proper buffer position |
| 1478 |
int savepacket = 1; |
| 1479 |
|
| 1480 |
if (rsocket->oursequence < (0xffff - (MAXNETBUFFERS - 1))) { |
| 1481 |
if (rsocket->oursequence > INTEL_SHORT(rcv_buff.seq)) { |
| 1482 |
mprintf(0, "Received old packet with seq of %d\n", INTEL_SHORT(rcv_buff.seq)); |
| 1483 |
savepacket = 0; |
| 1484 |
} |
| 1485 |
|
| 1486 |
} else { |
| 1487 |
// Sequence is high, so prepare for wrap around |
| 1488 |
if (((uint16_t)(INTEL_SHORT(rcv_buff.seq) + rsocket->oursequence)) > (MAXNETBUFFERS - 1)) { |
| 1489 |
mprintf(0, "Received old packet with seq of %d\n", INTEL_SHORT(rcv_buff.seq)); |
| 1490 |
savepacket = 0; |
| 1491 |
} |
| 1492 |
} |
| 1493 |
|
| 1494 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 1495 |
if ((NULL != rsocket->rbuffers[i]) && (rsocket->rsequence[i] == INTEL_SHORT(rcv_buff.seq))) { |
| 1496 |
// Received duplicate packet! |
| 1497 |
mprintf(0, "Received duplicate packet!\n"); |
| 1498 |
savepacket = 0; |
| 1499 |
} |
| 1500 |
} |
| 1501 |
if (savepacket) { |
| 1502 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 1503 |
if (NULL == rsocket->rbuffers[i]) { |
| 1504 |
// mprintf(0,"Got good data seq: %d\n",rcv_buff.seq); |
| 1505 |
if (INTEL_SHORT(rcv_buff.data_len) > max_len) |
| 1506 |
rsocket->recv_len[i] = max_len; // INTEL_SHORT(rcv_buff.data_len); |
| 1507 |
else |
| 1508 |
rsocket->recv_len[i] = INTEL_SHORT(rcv_buff.data_len); |
| 1509 |
rsocket->rbuffers[i] = (reliable_net_rcvbuffer *)mem_malloc(sizeof(reliable_net_rcvbuffer)); |
| 1510 |
memcpy(rsocket->rbuffers[i]->buffer, rcv_buff.data, rsocket->recv_len[i]); |
| 1511 |
rsocket->rsequence[i] = INTEL_SHORT(rcv_buff.seq); |
| 1512 |
// mprintf(0,"Adding packet to receive buffer in nw_ReceiveReliable().\n"); |
| 1513 |
break; |
| 1514 |
} |
| 1515 |
} |
| 1516 |
} |
| 1517 |
nw_SendReliableAck(&rsocket->addr, INTEL_SHORT(rcv_buff.seq), link_type, INTEL_FLOAT(rcv_buff.send_time)); |
| 1518 |
} |
| 1519 |
} |
| 1520 |
} while (0); // while((IPX_has_data>0) || (UDP_has_data>0)); |
| 1521 |
} |
| 1522 |
|
| 1523 |
// MTS: only used in this file |
| 1524 |
void nw_HandleConnectResponse(uint8_t *data, int len, network_address *server_addr) { |
| 1525 |
|
| 1526 |
int i; |
| 1527 |
static reliable_header ack_header; |
| 1528 |
static reliable_header conn_header; |
| 1529 |
SOCKADDR rcv_addr; |
| 1530 |
|
| 1531 |
memcpy(&ack_header, data, len); |
| 1532 |
|
| 1533 |
if (NP_TCP == server_addr->connection_type) { |
| 1534 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&rcv_addr; |
| 1535 |
memcpy(&inaddr->sin_addr, &server_addr->address, 4); |
| 1536 |
inaddr->sin_port = htons(server_addr->port); |
| 1537 |
} |
| 1538 |
|
| 1539 |
mprintf(0, "Got a connect response!\n"); |
| 1540 |
if (ack_header.type == RNT_ACK) { |
| 1541 |
int *acknum = (int *)&ack_header.data; |
| 1542 |
if (INTEL_INT(*acknum) == CONNECTSEQ) { |
| 1543 |
// if(memcmp(&rcv_addr,&sockaddr,sizeof(SOCKADDR))==0) |
| 1544 |
{ |
| 1545 |
for (i = 1; i < MAXRELIABLESOCKETS; i++) { |
| 1546 |
if (reliable_sockets[i].status == RNF_UNUSED) { |
| 1547 |
// Add the new connection here. |
| 1548 |
memset(&reliable_sockets[i], 0, sizeof(reliable_socket)); |
| 1549 |
reliable_sockets[i].connection_type = server_addr->connection_type; |
| 1550 |
memcpy(&reliable_sockets[i].net_addr, server_addr, sizeof(network_address)); |
| 1551 |
reliable_sockets[i].last_packet_received = timer_GetTime(); |
| 1552 |
memcpy(&reliable_sockets[i].addr, &rcv_addr, sizeof(SOCKADDR)); |
| 1553 |
reliable_sockets[i].status = RNF_LIMBO; |
| 1554 |
Net_connect_socket_id = i; |
| 1555 |
reliable_sockets[i].last_sent = timer_GetTime(); |
| 1556 |
reliable_sockets[i].waiting_packet_number = -1; |
| 1557 |
mprintf(0, "Succesfully connected to server in nw_ConnectToServer().\n"); |
| 1558 |
// Now send I_AM_HERE packet |
| 1559 |
conn_header.type = RNT_I_AM_HERE; |
| 1560 |
conn_header.seq = INTEL_SHORT((int16_t)(~CONNECTSEQ)); |
| 1561 |
conn_header.data_len = INTEL_SHORT((int16_t)0); |
| 1562 |
serverconn = i; |
| 1563 |
first_sent_iamhere = timer_GetTime(); |
| 1564 |
last_sent_iamhere = timer_GetTime(); |
| 1565 |
|
| 1566 |
int rcode = |
| 1567 |
nw_SendWithID(NWT_RELIABLE, (uint8_t *)&conn_header, RELIABLE_PACKET_HEADER_ONLY_SIZE, server_addr); |
| 1568 |
// int rcode = sendto(typeless_sock,(char |
| 1569 |
// *)&conn_header,RELIABLE_PACKET_HEADER_ONLY_SIZE,0,addr,sizeof(SOCKADDR)); |
| 1570 |
if (rcode == SOCKET_ERROR) { |
| 1571 |
Net_connect_socket_id = INVALID_SOCKET; |
| 1572 |
reliable_sockets[i].status = RNF_UNUSED; |
| 1573 |
memset(&reliable_sockets[i], 0, sizeof(reliable_socket)); |
| 1574 |
mprintf(0, "Unable to send packet in nw_ConnectToServer()\n"); |
| 1575 |
|
| 1576 |
Net_connect_sequence = R_NET_SEQUENCE_FAILED; |
| 1577 |
return; |
| 1578 |
} |
| 1579 |
reliable_sockets[i].last_packet_sent = timer_GetTime(); |
| 1580 |
/* |
| 1581 |
float f; |
| 1582 |
f = timer_GetTime(); |
| 1583 |
while(((timer_GetTime() - f)<2) && (reliable_sockets[i].status!=RNF_CONNECTING)) |
| 1584 |
{ |
| 1585 |
//nw_WorkReliable(); |
| 1586 |
nw_DoReceiveCallbacks(); |
| 1587 |
} |
| 1588 |
*/ |
| 1589 |
Net_connect_sequence = R_NET_SEQUENCE_CONNECTED; |
| 1590 |
return; |
| 1591 |
} |
| 1592 |
} |
| 1593 |
mprintf(0, "Out of reliable socket space in nw_ConnectToServer().\n"); |
| 1594 |
Net_connect_sequence = R_NET_SEQUENCE_FAILED; |
| 1595 |
return; |
| 1596 |
} |
| 1597 |
// else |
| 1598 |
//{ |
| 1599 |
// mprintf(0,"Received a reliable packet from a server other than the current server\n"); |
| 1600 |
//} |
| 1601 |
} else { |
| 1602 |
mprintf(0, "Received out of sequence ACK in nw_ConnectToServer().\n"); |
| 1603 |
} |
| 1604 |
} else { |
| 1605 |
mprintf(0, "Received something that isn't an ACK in nw_ConnectToServer().\n"); |
| 1606 |
} |
| 1607 |
} |
| 1608 |
|
| 1609 |
void nw_ConnectToServer(SOCKET *socket, network_address *server_addr) { |
| 1610 |
|
| 1611 |
// Send out a RNT_REQ_CONN packet, and wait for it to be acked. |
| 1612 |
|
| 1613 |
float time_sent_req = 0; |
| 1614 |
float first_sent_req = 0; |
| 1615 |
static reliable_header conn_header; |
| 1616 |
static reliable_header ack_header; |
| 1617 |
int bytesin; |
| 1618 |
struct timeval timeout; |
| 1619 |
*socket = INVALID_SOCKET; |
| 1620 |
|
| 1621 |
if (Use_DirectPlay) { |
| 1622 |
// We need a session description to do this, so we don't use this function |
| 1623 |
return; |
| 1624 |
} |
| 1625 |
|
| 1626 |
conn_header.type = RNT_REQ_CONN; |
| 1627 |
conn_header.seq = INTEL_SHORT((int16_t)CONNECTSEQ); |
| 1628 |
conn_header.data_len = 0; |
| 1629 |
|
| 1630 |
timeout.tv_sec = 0; |
| 1631 |
timeout.tv_usec = 0; |
| 1632 |
|
| 1633 |
if ((server_addr->connection_type == NP_TCP) && (!TCP_active)) { |
| 1634 |
return; |
| 1635 |
} |
| 1636 |
|
| 1637 |
Net_connect_sequence = R_NET_SEQUENCE_CONNECTING; |
| 1638 |
|
| 1639 |
memset(&ack_header, 0, sizeof(reliable_header)); |
| 1640 |
bytesin = 0; |
| 1641 |
|
| 1642 |
network_address d3_rcv_addr; |
| 1643 |
memset(&d3_rcv_addr, 0, sizeof(network_address)); |
| 1644 |
|
| 1645 |
int ret = nw_SendWithID(NWT_RELIABLE, (uint8_t *)&conn_header, RELIABLE_PACKET_HEADER_ONLY_SIZE, server_addr); |
| 1646 |
if (SOCKET_ERROR == ret) { |
| 1647 |
mprintf(0, "Unable to send packet in nw_ConnectToServer()! -- %d\n", WSAGetLastError()); |
| 1648 |
return; |
| 1649 |
} |
| 1650 |
|
| 1651 |
first_sent_req = timer_GetTime(); |
| 1652 |
time_sent_req = timer_GetTime(); |
| 1653 |
|
| 1654 |
// Wait until we get a response from the server or we timeout |
| 1655 |
|
| 1656 |
do { |
| 1657 |
nw_DoReceiveCallbacks(); |
| 1658 |
// Now we wait for the connection to be made.... |
| 1659 |
if (Net_connect_sequence == R_NET_SEQUENCE_CONNECTED) { |
| 1660 |
*socket = Net_connect_socket_id; |
| 1661 |
return; |
| 1662 |
} |
| 1663 |
if ((timer_GetTime() - time_sent_req) > 2) { |
| 1664 |
mprintf(0, "Resending connect request.\n"); |
| 1665 |
int ret = nw_SendWithID(NWT_RELIABLE, (uint8_t *)&conn_header, RELIABLE_PACKET_HEADER_ONLY_SIZE, server_addr); |
| 1666 |
if (ret != SOCKET_ERROR) { |
| 1667 |
time_sent_req = timer_GetTime(); |
| 1668 |
} else { |
| 1669 |
mprintf(0, "Error sending connection request! -- %d\n", WSAGetLastError()); |
| 1670 |
} |
| 1671 |
} |
| 1672 |
|
| 1673 |
} while ((timer_GetTime() - first_sent_req) < NETTIMEOUT); |
| 1674 |
return; |
| 1675 |
} |
| 1676 |
|
| 1677 |
void nw_CloseSocket(SOCKET *sockp) { |
| 1678 |
reliable_header diss_conn_header; |
| 1679 |
#ifdef WIN32 |
| 1680 |
if (DP_active) { |
| 1681 |
dp_DirectPlayDestroyPlayer(*sockp); |
| 1682 |
return; |
| 1683 |
} |
| 1684 |
#endif |
| 1685 |
|
| 1686 |
if (*sockp >= MAXRELIABLESOCKETS) { |
| 1687 |
mprintf(0, "Invalid socket id passed to nw_NewCloseSocket() -- %d\n", *sockp); |
| 1688 |
return; |
| 1689 |
} |
| 1690 |
|
| 1691 |
if (reliable_sockets[*sockp].status == RNF_UNUSED) { |
| 1692 |
mprintf(0, "Trying to close an unused socket (%d) -- ignoring request.\n", *sockp); |
| 1693 |
} |
| 1694 |
mprintf(0, "Closing socket %d\n", *sockp); |
| 1695 |
// Go through every buffer and "free it up(tm)" |
| 1696 |
int i; |
| 1697 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 1698 |
if (reliable_sockets[*sockp].rbuffers[i]) { |
| 1699 |
mem_free(reliable_sockets[*sockp].rbuffers[i]); |
| 1700 |
reliable_sockets[*sockp].rbuffers[i] = NULL; |
| 1701 |
reliable_sockets[*sockp].rsequence[i] = 0; |
| 1702 |
} |
| 1703 |
if (reliable_sockets[*sockp].sbuffers[i]) { |
| 1704 |
mem_free(reliable_sockets[*sockp].sbuffers[i]); |
| 1705 |
reliable_sockets[*sockp].sbuffers[i] = NULL; |
| 1706 |
reliable_sockets[*sockp].rsequence[i] = 0; |
| 1707 |
} |
| 1708 |
} |
| 1709 |
diss_conn_header.type = RNT_DISCONNECT; |
| 1710 |
diss_conn_header.seq = INTEL_SHORT((int16_t)(CONNECTSEQ)); |
| 1711 |
diss_conn_header.data_len = 0; |
| 1712 |
if (*sockp == serverconn) |
| 1713 |
serverconn = -1; |
| 1714 |
|
| 1715 |
network_address send_address; |
| 1716 |
memset(&send_address, 0, sizeof(network_address)); |
| 1717 |
|
| 1718 |
send_address.connection_type = reliable_sockets[*sockp].connection_type; |
| 1719 |
if (NP_TCP == reliable_sockets[*sockp].connection_type) { |
| 1720 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&reliable_sockets[*sockp].addr; |
| 1721 |
memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 1722 |
send_address.port = htons(inaddr->sin_port); |
| 1723 |
send_address.connection_type = NP_TCP; |
| 1724 |
} |
| 1725 |
|
| 1726 |
nw_SendWithID(NWT_RELIABLE, (uint8_t *)&diss_conn_header, RELIABLE_PACKET_HEADER_ONLY_SIZE, &send_address); |
| 1727 |
|
| 1728 |
memset(&reliable_sockets[*sockp], 0, sizeof(reliable_socket)); |
| 1729 |
reliable_sockets[*sockp].status = RNF_UNUSED; |
| 1730 |
} |
| 1731 |
|
| 1732 |
int nw_CheckReliableSocket(int socknum) { |
| 1733 |
// Checks to see if a socket is connected or not. |
| 1734 |
if (Use_DirectPlay) { |
| 1735 |
return true; |
| 1736 |
} |
| 1737 |
if (socknum >= MAXRELIABLESOCKETS) { |
| 1738 |
mprintf(0, "Invalid socket id passed to nw_CheckReliableSocket() -- %d\n", socknum); |
| 1739 |
return 0; |
| 1740 |
} |
| 1741 |
switch (reliable_sockets[socknum].status) { |
| 1742 |
case RNF_UNUSED: |
| 1743 |
case RNF_BROKEN: |
| 1744 |
case RNF_DISCONNECTED: |
| 1745 |
return 0; |
| 1746 |
default: |
| 1747 |
return 1; |
| 1748 |
} |
| 1749 |
} |
| 1750 |
|
| 1751 |
// MTS: only used in this file. |
| 1752 |
int nw_PingCompare(const void *arg1, const void *arg2) { |
| 1753 |
float *ping1 = (float *)arg1; |
| 1754 |
float *ping2 = (float *)arg2; |
| 1755 |
|
| 1756 |
if (*ping1 == *ping2) |
| 1757 |
return 0; |
| 1758 |
else if (*ping1 > *ping2) |
| 1759 |
return 1; |
| 1760 |
else if (*ping1 < *ping2) |
| 1761 |
return -1; |
| 1762 |
|
| 1763 |
return 0; |
| 1764 |
} |
| 1765 |
|
| 1766 |
// MTS: only used in this file |
| 1767 |
// Warning, experimental compression below, if you want to use it, talk to Kevin. Doesn't do much currently, only |
| 1768 |
// reduces 0's |
| 1769 |
#define COMPRESS_KEY 0xfd |
| 1770 |
int nw_Compress(void *srcdata, void *destdata, int count) { |
| 1771 |
int i; |
| 1772 |
uint8_t *curr_src = (uint8_t *)srcdata; |
| 1773 |
uint8_t *currp = (uint8_t *)destdata; |
| 1774 |
for (i = 0; i < count; i++) { |
| 1775 |
// Woops, we have a char that matches our compress key, so add it as it's own type |
| 1776 |
if (curr_src[i] == COMPRESS_KEY) { |
| 1777 |
*currp = COMPRESS_KEY; |
| 1778 |
currp++; |
| 1779 |
//*currp = 1; |
| 1780 |
// currp++; |
| 1781 |
*currp = COMPRESS_KEY; |
| 1782 |
currp++; |
| 1783 |
} |
| 1784 |
// Look for 3 in a row |
| 1785 |
else if ((curr_src[i] == 0) && (curr_src[i + 1] == 0) && (curr_src[i + 2] == 0) && (i + 3 < count)) { |
| 1786 |
int repeat_count = 3; |
| 1787 |
*currp = COMPRESS_KEY; |
| 1788 |
currp++; |
| 1789 |
while ((curr_src[i] == curr_src[i + repeat_count]) && (repeat_count < 250) && (i + repeat_count < count)) { |
| 1790 |
repeat_count++; |
| 1791 |
} |
| 1792 |
*currp = repeat_count; |
| 1793 |
currp++; |
| 1794 |
//*currp = curr_src[i]; |
| 1795 |
// currp++; |
| 1796 |
i += (repeat_count - 1); |
| 1797 |
} else { |
| 1798 |
*currp = curr_src[i]; |
| 1799 |
currp++; |
| 1800 |
} |
| 1801 |
} |
| 1802 |
return currp - (uint8_t *)destdata; |
| 1803 |
} |
| 1804 |
|
| 1805 |
// MTS: only used in this file |
| 1806 |
int nw_Uncompress(void *compdata, void *uncompdata, int count) { |
| 1807 |
int i; |
| 1808 |
int destlen = 0; |
| 1809 |
uint8_t *comp_src = (uint8_t *)compdata; |
| 1810 |
uint8_t *currp = (uint8_t *)uncompdata; |
| 1811 |
for (i = 0; i < count; i++) { |
| 1812 |
if (*comp_src == COMPRESS_KEY) { |
| 1813 |
comp_src++; |
| 1814 |
if (*comp_src == COMPRESS_KEY) { |
| 1815 |
currp[destlen] = COMPRESS_KEY; |
| 1816 |
destlen++; |
| 1817 |
} else { |
| 1818 |
for (int a = 0; a < (*comp_src); a++) { |
| 1819 |
currp[destlen] = 0; //*(comp_src+1); |
| 1820 |
destlen++; |
| 1821 |
} |
| 1822 |
} |
| 1823 |
i++; |
| 1824 |
// comp_src++; |
| 1825 |
comp_src++; |
| 1826 |
} else { |
| 1827 |
currp[destlen] = *comp_src; |
| 1828 |
destlen++; |
| 1829 |
comp_src++; |
| 1830 |
} |
| 1831 |
} |
| 1832 |
return destlen; |
| 1833 |
} |
| 1834 |
|
| 1835 |
// MTS: only used in this file |
| 1836 |
// initialize the buffering system |
| 1837 |
void nw_psnet_buffer_init() { |
| 1838 |
int idx; |
| 1839 |
|
| 1840 |
// blast the buffer clean |
| 1841 |
memset(Psnet_buffers, 0, sizeof(network_packet_buffer) * MAX_PACKET_BUFFERS); |
| 1842 |
|
| 1843 |
// set all buffer sequence #'s to -1 |
| 1844 |
for (idx = 0; idx < MAX_PACKET_BUFFERS; idx++) { |
| 1845 |
Psnet_buffers[idx].sequence_number = -1; |
| 1846 |
} |
| 1847 |
|
| 1848 |
// initialize the sequence # |
| 1849 |
Psnet_seq_number = 0; |
| 1850 |
Psnet_lowest_id = -1; |
| 1851 |
Psnet_highest_id = -1; |
| 1852 |
} |
| 1853 |
|
| 1854 |
// MTS: only used in this file |
| 1855 |
// buffer a packet (maintain order!) |
| 1856 |
void nw_psnet_buffer_packet(uint8_t *data, int length, network_address *from) { |
| 1857 |
int idx; |
| 1858 |
int found_buf = 0; |
| 1859 |
|
| 1860 |
// find the first empty packet |
| 1861 |
for (idx = 0; idx < MAX_PACKET_BUFFERS; idx++) { |
| 1862 |
if (Psnet_buffers[idx].sequence_number == -1) { |
| 1863 |
found_buf = 1; |
| 1864 |
break; |
| 1865 |
} |
| 1866 |
} |
| 1867 |
|
| 1868 |
// if we didn't find the buffer, report an overrun |
| 1869 |
if (!found_buf) { |
| 1870 |
mprintf(0, "WARNING - Buffer overrun in psnet\n"); |
| 1871 |
} else { |
| 1872 |
// copy in the data |
| 1873 |
memcpy(Psnet_buffers[idx].data, data, length); |
| 1874 |
Psnet_buffers[idx].len = length; |
| 1875 |
memcpy(&Psnet_buffers[idx].from_addr, from, sizeof(network_address)); |
| 1876 |
Psnet_buffers[idx].sequence_number = Psnet_seq_number; |
| 1877 |
|
| 1878 |
// keep track of the highest id# |
| 1879 |
Psnet_highest_id = Psnet_seq_number++; |
| 1880 |
|
| 1881 |
// set the lowest id# for the first time |
| 1882 |
if (Psnet_lowest_id == -1) { |
| 1883 |
Psnet_lowest_id = Psnet_highest_id; |
| 1884 |
} |
| 1885 |
} |
| 1886 |
} |
| 1887 |
|
| 1888 |
// MTS: only used in this file |
| 1889 |
// get the index of the next packet in order! |
| 1890 |
int nw_psnet_buffer_get_next_by_packet_id(uint8_t *data, int *length, uint32_t packet_id) { |
| 1891 |
int idx; |
| 1892 |
int found_buf = 0; |
| 1893 |
|
| 1894 |
// if there are no buffers, do nothing |
| 1895 |
if ((Psnet_lowest_id == -1) || (Psnet_lowest_id > Psnet_highest_id)) { |
| 1896 |
return 0; |
| 1897 |
} |
| 1898 |
|
| 1899 |
// search until we find the lowest packet index id# |
| 1900 |
for (idx = 0; idx < MAX_PACKET_BUFFERS; idx++) { |
| 1901 |
uint32_t *thisid; |
| 1902 |
thisid = (uint32_t *)&Psnet_buffers[idx].from_addr.address; |
| 1903 |
// if we found the buffer |
| 1904 |
if ((Psnet_buffers[idx].sequence_number == Psnet_lowest_id) && (packet_id == *thisid)) { |
| 1905 |
found_buf = 1; |
| 1906 |
break; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
if (!found_buf) |
| 1911 |
return 0; |
| 1912 |
|
| 1913 |
// copy out the buffer data |
| 1914 |
memcpy(data, Psnet_buffers[idx].data, Psnet_buffers[idx].len); |
| 1915 |
*length = Psnet_buffers[idx].len; |
| 1916 |
|
| 1917 |
// now we need to cleanup the packet list |
| 1918 |
|
| 1919 |
// mark the buffer as free |
| 1920 |
Psnet_buffers[idx].sequence_number = -1; |
| 1921 |
Psnet_lowest_id++; |
| 1922 |
|
| 1923 |
return 1; |
| 1924 |
} |
| 1925 |
|
| 1926 |
// MTS: only used in this file |
| 1927 |
// get the index of the next packet in order! |
| 1928 |
int nw_psnet_buffer_get_next(uint8_t *data, int *length, network_address *from) { |
| 1929 |
int idx; |
| 1930 |
int found_buf = 0; |
| 1931 |
|
| 1932 |
// if there are no buffers, do nothing |
| 1933 |
if ((Psnet_lowest_id == -1) || (Psnet_lowest_id > Psnet_highest_id)) { |
| 1934 |
return 0; |
| 1935 |
} |
| 1936 |
|
| 1937 |
// search until we find the lowest packet index id# |
| 1938 |
for (idx = 0; idx < MAX_PACKET_BUFFERS; idx++) { |
| 1939 |
// if we found the buffer |
| 1940 |
if (Psnet_buffers[idx].sequence_number == Psnet_lowest_id) { |
| 1941 |
found_buf = 1; |
| 1942 |
break; |
| 1943 |
} |
| 1944 |
} |
| 1945 |
|
| 1946 |
// at this point, we should _always_ have found the buffer |
| 1947 |
ASSERT(found_buf); |
| 1948 |
|
| 1949 |
// copy out the buffer data |
| 1950 |
memcpy(data, Psnet_buffers[idx].data, Psnet_buffers[idx].len); |
| 1951 |
*length = Psnet_buffers[idx].len; |
| 1952 |
memcpy(from, &Psnet_buffers[idx].from_addr, sizeof(network_address)); |
| 1953 |
|
| 1954 |
// now we need to cleanup the packet list |
| 1955 |
|
| 1956 |
// mark the buffer as free |
| 1957 |
Psnet_buffers[idx].sequence_number = -1; |
| 1958 |
Psnet_lowest_id++; |
| 1959 |
|
| 1960 |
return 1; |
| 1961 |
} |
| 1962 |
|
| 1963 |
#ifdef WIN32 |
| 1964 |
// MTS: only used in this file |
| 1965 |
// functions to get the status of a RAS connection |
| 1966 |
uint32_t psnet_ras_status() { |
| 1967 |
int rval; |
| 1968 |
DWORD size, num_connections, i; |
| 1969 |
RASCONN rasbuffer[25]; |
| 1970 |
HINSTANCE ras_handle; |
| 1971 |
uint32_t rasip = 0; |
| 1972 |
RASPPPIP projection; |
| 1973 |
int Ras_connected; |
| 1974 |
|
| 1975 |
Ras_connected = 0; |
| 1976 |
|
| 1977 |
// first, call a LoadLibrary to load the RAS api |
| 1978 |
ras_handle = LoadLibrary("rasapi32.dll"); |
| 1979 |
if (ras_handle == NULL) { |
| 1980 |
return INADDR_ANY; |
| 1981 |
} |
| 1982 |
|
| 1983 |
pRasEnumConnections = |
| 1984 |
(DWORD(__stdcall *)(LPRASCONN, LPDWORD, LPDWORD))GetProcAddress(ras_handle, "RasEnumConnectionsA"); |
| 1985 |
if (!pRasEnumConnections) { |
| 1986 |
FreeLibrary(ras_handle); |
| 1987 |
return INADDR_ANY; |
| 1988 |
} |
| 1989 |
pRasGetConnectStatus = |
| 1990 |
(DWORD(__stdcall *)(HRASCONN, LPRASCONNSTATUS))GetProcAddress(ras_handle, "RasGetConnectStatusA"); |
| 1991 |
if (!pRasGetConnectStatus) { |
| 1992 |
FreeLibrary(ras_handle); |
| 1993 |
return INADDR_ANY; |
| 1994 |
} |
| 1995 |
pRasGetProjectionInfo = |
| 1996 |
(DWORD(__stdcall *)(HRASCONN, RASPROJECTION, LPVOID, LPDWORD))GetProcAddress(ras_handle, "RasGetProjectionInfoA"); |
| 1997 |
if (!pRasGetProjectionInfo) { |
| 1998 |
FreeLibrary(ras_handle); |
| 1999 |
return INADDR_ANY; |
| 2000 |
} |
| 2001 |
|
| 2002 |
size = sizeof(rasbuffer); |
| 2003 |
rasbuffer[0].dwSize = sizeof(RASCONN); |
| 2004 |
|
| 2005 |
rval = pRasEnumConnections(rasbuffer, &size, &num_connections); |
| 2006 |
if (rval) { |
| 2007 |
FreeLibrary(ras_handle); |
| 2008 |
return INADDR_ANY; |
| 2009 |
} |
| 2010 |
|
| 2011 |
// JAS: My computer gets to this point, but I have no RAS connections, |
| 2012 |
// so just exit |
| 2013 |
if (num_connections < 1) { |
| 2014 |
mprintf(0, "Found no RAS connections\n"); |
| 2015 |
FreeLibrary(ras_handle); |
| 2016 |
return INADDR_ANY; |
| 2017 |
} |
| 2018 |
|
| 2019 |
mprintf(0, "Found %d connections\n", num_connections); |
| 2020 |
|
| 2021 |
for (i = 0; i < num_connections; i++) { |
| 2022 |
RASCONNSTATUS status; |
| 2023 |
DWORD size; |
| 2024 |
|
| 2025 |
mprintf(0, "Connection %d:\n", i); |
| 2026 |
mprintf(0, "Entry Name: %s\n", rasbuffer[i].szEntryName); |
| 2027 |
mprintf(0, "Device Type: %s\n", rasbuffer[i].szDeviceType); |
| 2028 |
mprintf(0, "Device Name: %s\n", rasbuffer[i].szDeviceName); |
| 2029 |
|
| 2030 |
// get the connection status |
| 2031 |
status.dwSize = sizeof(RASCONNSTATUS); |
| 2032 |
rval = pRasGetConnectStatus(rasbuffer[i].hrasconn, &status); |
| 2033 |
if (rval != 0) { |
| 2034 |
FreeLibrary(ras_handle); |
| 2035 |
return INADDR_ANY; |
| 2036 |
} |
| 2037 |
|
| 2038 |
mprintf(0, "\tStatus: %s\n", (status.rasconnstate == RASCS_Connected) ? "Connected" : "Not Connected"); |
| 2039 |
|
| 2040 |
// get the projection informatiom |
| 2041 |
size = sizeof(projection); |
| 2042 |
projection.dwSize = size; |
| 2043 |
rval = pRasGetProjectionInfo(rasbuffer[i].hrasconn, RASP_PppIp, &projection, &size); |
| 2044 |
if (rval != 0) { |
| 2045 |
FreeLibrary(ras_handle); |
| 2046 |
return INADDR_ANY; |
| 2047 |
} |
| 2048 |
|
| 2049 |
mprintf(0, "\tIP Address: %s\n", projection.szIpAddress); |
| 2050 |
} |
| 2051 |
|
| 2052 |
Ras_connected = 1; |
| 2053 |
|
| 2054 |
FreeLibrary(ras_handle); |
| 2055 |
rasip = inet_addr(projection.szIpAddress); |
| 2056 |
if (rasip == INADDR_NONE) |
| 2057 |
return INADDR_ANY; |
| 2058 |
// The ip of the RAS connection |
| 2059 |
return rasip; |
| 2060 |
} |
| 2061 |
#endif |
| 2062 |
|
| 2063 |
static async_dns_lookup aslu; |
| 2064 |
static async_dns_lookup *lastaslu = NULL; |
| 2065 |
|
| 2066 |
#ifdef WIN32 |
| 2067 |
#define CDECLCALL __cdecl |
| 2068 |
#else |
| 2069 |
#define CDECLCALL |
| 2070 |
#endif |
| 2071 |
|
| 2072 |
#if defined(POSIX) |
| 2073 |
int CDECLCALL gethostbynameworker(void *parm); |
| 2074 |
#include "SDL.h" |
| 2075 |
#include "SDL_thread.h" |
| 2076 |
|
| 2077 |
// rcg06192000 use SDL threads. |
| 2078 |
// #include <pthread.h> |
| 2079 |
// #include <dlfcn.h> |
| 2080 |
// typedef int (*pthread_create_fp)(pthread_t *__thread,__const pthread_attr_t *__attr,void *(*__start_routine) (void |
| 2081 |
// *),void *__arg); typedef int (*pthread_detach_fp)(pthread_t __th); typedef pthread_t (*pthread_self_fp)(void); static |
| 2082 |
// pthread_create_fp dpthread_create = NULL; static pthread_detach_fp dpthread_detach = NULL; static pthread_self_fp |
| 2083 |
// dpthread_self = NULL; |
| 2084 |
|
| 2085 |
void nw_LoadThreadLibrary(void) { |
| 2086 |
// rcg06192000 use SDL threads. |
| 2087 |
/* |
| 2088 |
if(dpthread_create) |
| 2089 |
return; |
| 2090 |
|
| 2091 |
void *lib; |
| 2092 |
|
| 2093 |
lib = dlopen("libpthread.so",RTLD_GLOBAL|RTLD_NOW); |
| 2094 |
if(!lib) |
| 2095 |
{ |
| 2096 |
Int3(); |
| 2097 |
return; |
| 2098 |
} |
| 2099 |
|
| 2100 |
dpthread_create = (pthread_create_fp)dlsym(lib,"pthread_create"); |
| 2101 |
dpthread_detach = (pthread_detach_fp)dlsym(lib,"pthread_detach"); |
| 2102 |
dpthread_self = (pthread_self_fp)dlsym(lib,"pthread_self"); |
| 2103 |
|
| 2104 |
if(!dpthread_create || !dpthread_detach || !dpthread_self) |
| 2105 |
{ |
| 2106 |
dpthread_create = NULL; //this variable I use to see if the library is loaded |
| 2107 |
Int3(); |
| 2108 |
return; |
| 2109 |
} |
| 2110 |
|
| 2111 |
// don't close the library...we need it open for future use |
| 2112 |
// and we can't close it on atexit() because linux will segfault for some stupid reason |
| 2113 |
*/ |
| 2114 |
} |
| 2115 |
#else |
| 2116 |
void CDECLCALL gethostbynameworker(void *parm); |
| 2117 |
#endif |
| 2118 |
|
| 2119 |
int nw_Asyncgethostbyname(uint32_t *ip, int command, char *hostname) { |
| 2120 |
|
| 2121 |
if (command == NW_AGHBN_LOOKUP) { |
| 2122 |
if (lastaslu) { |
| 2123 |
lastaslu->abort = true; |
| 2124 |
|
| 2125 |
// rcg06212000 join the thread. |
| 2126 |
#if defined(POSIX) |
| 2127 |
SDL_WaitThread(lastaslu->threadId, NULL); |
| 2128 |
free(lastaslu); |
| 2129 |
#endif |
| 2130 |
} // if |
| 2131 |
|
| 2132 |
// rcg06262000 don't need all the rigamarole since we join the thread. |
| 2133 |
|
| 2134 |
#if (!defined(POSIX)) |
| 2135 |
async_dns_lookup *newaslu; |
| 2136 |
newaslu = (async_dns_lookup *)mem_malloc(sizeof(async_dns_lookup)); |
| 2137 |
memset(&newaslu->ip, 0, sizeof(uint32_t)); |
| 2138 |
newaslu->host = hostname; |
| 2139 |
newaslu->done = false; |
| 2140 |
newaslu->error = false; |
| 2141 |
newaslu->abort = false; |
| 2142 |
lastaslu = newaslu; |
| 2143 |
aslu.done = false; |
| 2144 |
#else |
| 2145 |
memset(&aslu.ip, 0, sizeof(uint32_t)); |
| 2146 |
aslu.host = hostname; |
| 2147 |
aslu.done = false; |
| 2148 |
aslu.error = false; |
| 2149 |
aslu.abort = false; |
| 2150 |
lastaslu = &aslu; |
| 2151 |
aslu.done = false; |
| 2152 |
#endif |
| 2153 |
|
| 2154 |
#ifdef WIN32 |
| 2155 |
_beginthread(gethostbynameworker, 0, newaslu); |
| 2156 |
#elif defined(POSIX) |
| 2157 |
// rcg06192000 use SDL threads. |
| 2158 |
/* |
| 2159 |
nw_LoadThreadLibrary(); |
| 2160 |
if(!dpthread_create) |
| 2161 |
{ |
| 2162 |
|
| 2163 |
HOSTENT *he = gethostbyname(lastaslu->host); |
| 2164 |
if(he==NULL) |
| 2165 |
{ |
| 2166 |
lastaslu->error = true; |
| 2167 |
} |
| 2168 |
else |
| 2169 |
{ |
| 2170 |
memcpy(&lastaslu->ip,he->h_addr_list[0],sizeof(uint32_t)); |
| 2171 |
lastaslu->done = true; |
| 2172 |
memcpy(&aslu,lastaslu,sizeof(async_dns_lookup)); |
| 2173 |
} |
| 2174 |
}else |
| 2175 |
*/ |
| 2176 |
|
| 2177 |
{ |
| 2178 |
// rcg06192000 SDLified. |
| 2179 |
// pthread_t thread; |
| 2180 |
// dpthread_create(&thread, NULL, gethostbynameworker,newaslu); |
| 2181 |
aslu.threadId = SDL_CreateThread(gethostbynameworker, "dnsworker", &aslu); |
| 2182 |
} |
| 2183 |
#else |
| 2184 |
HOSTENT *he = gethostbyname(lastaslu->host); |
| 2185 |
if (he == NULL) { |
| 2186 |
lastaslu->error = true; |
| 2187 |
} else { |
| 2188 |
memcpy(&lastaslu->ip, he->h_addr_list[0], sizeof(uint32_t)); |
| 2189 |
lastaslu->done = true; |
| 2190 |
memcpy(&aslu, lastaslu, sizeof(async_dns_lookup)); |
| 2191 |
} |
| 2192 |
#endif |
| 2193 |
return 1; |
| 2194 |
} else if (command == NW_AGHBN_CANCEL) { |
| 2195 |
if (lastaslu) { |
| 2196 |
lastaslu->abort = true; |
| 2197 |
|
| 2198 |
// rcg06212000 join the thread. |
| 2199 |
#if defined(POSIX) |
| 2200 |
SDL_WaitThread(lastaslu->threadId, NULL); |
| 2201 |
#endif |
| 2202 |
} // if |
| 2203 |
lastaslu = NULL; |
| 2204 |
} else if (command == NW_AGHBN_READ) { |
| 2205 |
if (!lastaslu) |
| 2206 |
return -1; |
| 2207 |
if (aslu.done) { |
| 2208 |
// rcg06212000 join the thread. |
| 2209 |
#if defined(POSIX) |
| 2210 |
SDL_WaitThread(aslu.threadId, NULL); |
| 2211 |
#endif |
| 2212 |
|
| 2213 |
lastaslu = NULL; |
| 2214 |
memcpy(ip, &aslu.ip, sizeof(uint32_t)); |
| 2215 |
return 1; |
| 2216 |
} else if (aslu.error) { |
| 2217 |
// rcg06212000 join the thread. |
| 2218 |
#if defined(POSIX) |
| 2219 |
SDL_WaitThread(aslu.threadId, NULL); |
| 2220 |
#else |
| 2221 |
mem_free(lastaslu); |
| 2222 |
#endif |
| 2223 |
|
| 2224 |
lastaslu = NULL; |
| 2225 |
return -1; |
| 2226 |
} else |
| 2227 |
return 0; |
| 2228 |
} |
| 2229 |
return -2; |
| 2230 |
} |
| 2231 |
|
| 2232 |
// This is the worker thread which does the lookup. |
| 2233 |
#if defined(POSIX) |
| 2234 |
int CDECLCALL gethostbynameworker(void *parm) |
| 2235 |
#else |
| 2236 |
void CDECLCALL gethostbynameworker(void *parm) |
| 2237 |
#endif |
| 2238 |
{ |
| 2239 |
// rcg06192000 nope. |
| 2240 |
// #if defined(POSIX) |
| 2241 |
// dpthread_detach(dpthread_self()); |
| 2242 |
// #endif |
| 2243 |
|
| 2244 |
async_dns_lookup *lookup = (async_dns_lookup *)parm; |
| 2245 |
|
| 2246 |
mprintf(0, "IPLOOKUP: Starting threaded lookup ..."); |
| 2247 |
HOSTENT *he = gethostbyname(lookup->host); |
| 2248 |
mprintf(0, "IPLOOKUP: Threaded lookup complete."); |
| 2249 |
|
| 2250 |
if (he == NULL) { |
| 2251 |
lookup->error = true; |
| 2252 |
#if defined(POSIX) |
| 2253 |
return 0; |
| 2254 |
#else |
| 2255 |
return; |
| 2256 |
#endif |
| 2257 |
} else if (!lookup->abort) { |
| 2258 |
memcpy(&lookup->ip, he->h_addr_list[0], sizeof(uint32_t)); |
| 2259 |
mprintf(0, "IPLOOKUP: [%s] is %d.%d.%d.%d ...", |
| 2260 |
lookup->host, |
| 2261 |
(lookup->ip & 0x000000FF), |
| 2262 |
(lookup->ip & 0x0000FF00) >> 8, |
| 2263 |
(lookup->ip & 0x00FF0000) >> 16, |
| 2264 |
(lookup->ip & 0xFF000000) >> 24); |
| 2265 |
// memcpy(&aslu,lookup,sizeof(async_dns_lookup)); |
| 2266 |
lookup->done = true; |
| 2267 |
} |
| 2268 |
|
| 2269 |
// rcg06252000 don't free this, 'cause we need the threadId. |
| 2270 |
#if !defined(POSIX) |
| 2271 |
mem_free(lookup); |
| 2272 |
#endif |
| 2273 |
|
| 2274 |
#if defined(POSIX) |
| 2275 |
return 0; |
| 2276 |
#endif |
| 2277 |
} |
| 2278 |
|
| 2279 |
int nw_ReccomendPPS() { |
| 2280 |
static char szconnspeed[100]; |
| 2281 |
int len = 99; |
| 2282 |
strcpy(szconnspeed, ""); |
| 2283 |
Database->read("ConnectionSpeed", szconnspeed, &len); |
| 2284 |
if (stricmp(szconnspeed, "28K") == 0) |
| 2285 |
return 5; |
| 2286 |
else if (stricmp(szconnspeed, "33K") == 0) |
| 2287 |
return 6; |
| 2288 |
else if (stricmp(szconnspeed, "56K") == 0) |
| 2289 |
return 7; |
| 2290 |
else if (stricmp(szconnspeed, "ISDN") == 0) |
| 2291 |
return 8; |
| 2292 |
else if (stricmp(szconnspeed, "Cable") == 0) |
| 2293 |
return 9; |
| 2294 |
else if (stricmp(szconnspeed, "Fast") == 0) |
| 2295 |
return 12; |
| 2296 |
else |
| 2297 |
return 7; |
| 2298 |
} |
| 2299 |
|
| 2300 |
// Register the networking library to call your function back |
| 2301 |
// When data containing your ID is found |
| 2302 |
// Returns non-zero if succesfull, Zero if this ID is already registered |
| 2303 |
int nw_RegisterCallback(NetworkReceiveCallback nfp, uint8_t id) { |
| 2304 |
ASSERT(id < 16); |
| 2305 |
|
| 2306 |
// rcg06212000 this happens all the time; let it slide. |
| 2307 |
// if(Netcallbacks[id]) |
| 2308 |
//{ |
| 2309 |
// mprintf(0,"Trying to reregister a callback!\n"); |
| 2310 |
// Int3(); // Get Kevin! |
| 2311 |
//} |
| 2312 |
|
| 2313 |
Netcallbacks[id] = nfp; |
| 2314 |
return 0; |
| 2315 |
} |
| 2316 |
|
| 2317 |
NetworkReceiveCallback nw_UnRegisterCallback(uint8_t id) { |
| 2318 |
NetworkReceiveCallback nfp; |
| 2319 |
ASSERT(id < 16); |
| 2320 |
nfp = Netcallbacks[id]; |
| 2321 |
Netcallbacks[id] = NULL; |
| 2322 |
return nfp; |
| 2323 |
} |
| 2324 |
|
| 2325 |
int nw_SendWithID(uint8_t id, uint8_t *data, int len, network_address *who_to) { |
| 2326 |
uint8_t packet_data[1500]; |
| 2327 |
int send_this_packet = 1; |
| 2328 |
SOCKET send_sock; |
| 2329 |
SOCKADDR_IN sock_addr; // UDP/TCP socket structure |
| 2330 |
|
| 2331 |
int ret, send_len; |
| 2332 |
uint8_t iaddr[6], *send_data; |
| 2333 |
int16_t port; |
| 2334 |
fd_set wfds; |
| 2335 |
|
| 2336 |
ASSERT(data); |
| 2337 |
ASSERT(len); |
| 2338 |
ASSERT(who_to); |
| 2339 |
|
| 2340 |
timeval timeout = {0, 0}; |
| 2341 |
|
| 2342 |
packet_data[0] = id; |
| 2343 |
memcpy(packet_data + 1, data, len); |
| 2344 |
len++; // Account for the added byte |
| 2345 |
|
| 2346 |
// if (NetDebugFile) |
| 2347 |
// cfprintf (NetDebugFile,"nw_SendWithID packet of id=%d type %d at %f |
| 2348 |
// seconds.\n",id,data[0],timer_GetTime()); |
| 2349 |
|
| 2350 |
// mprintf(0,"Sending packet for id %d.\n",id); |
| 2351 |
#ifdef WIN32 |
| 2352 |
if (Use_DirectPlay) |
| 2353 |
return dp_DirectPlaySend(who_to, (uint8_t *)data, len, false); |
| 2354 |
#endif |
| 2355 |
|
| 2356 |
// mprintf(1, "network: type %d\n", who_to->connection_type); |
| 2357 |
// send_sock = *Unreliable_socket; |
| 2358 |
switch (who_to->connection_type) { |
| 2359 |
case NP_TCP: |
| 2360 |
if (id == NWT_RELIABLE) { |
| 2361 |
NetStatistics.tcp_total_packets_sent++; |
| 2362 |
NetStatistics.tcp_total_bytes_sent += len; |
| 2363 |
} else if (id == NWT_UNRELIABLE) { |
| 2364 |
NetStatistics.udp_total_packets_sent++; |
| 2365 |
NetStatistics.udp_total_bytes_sent += len; |
| 2366 |
} |
| 2367 |
|
| 2368 |
send_sock = TCP_socket; |
| 2369 |
if (!TCP_active) |
| 2370 |
return 0; |
| 2371 |
break; |
| 2372 |
default: |
| 2373 |
mprintf(2, "Unknown protocol type in nw_Send()\n"); |
| 2374 |
Int3(); |
| 2375 |
return 0; |
| 2376 |
} |
| 2377 |
|
| 2378 |
/* |
| 2379 |
uint8_t compdata[MAX_PACKET_SIZE*3]; |
| 2380 |
uint8_t testdata[MAX_PACKET_SIZE*3]; |
| 2381 |
int uncompsize; |
| 2382 |
|
| 2383 |
////Int3(); |
| 2384 |
Uncompressed_outgoing_data_len += len; |
| 2385 |
int compsize = nw_Compress(data,compdata,len); |
| 2386 |
Compressed_outgoing_data_len += compsize; |
| 2387 |
uncompsize = nw_Uncompress(compdata,testdata,compsize); |
| 2388 |
|
| 2389 |
ASSERT(uncompsize==len); |
| 2390 |
ASSERT(memcmp(data,testdata,uncompsize)==0); |
| 2391 |
|
| 2392 |
int my_comp_ratio = (float) ((float)Uncompressed_outgoing_data_len/(float)Compressed_outgoing_data_len); |
| 2393 |
|
| 2394 |
mprintf_at(2,1,0,"Compression: %d%% ",my_comp_ratio); |
| 2395 |
*/ |
| 2396 |
if (!Sockets_initted) { |
| 2397 |
mprintf(0, "Network ==> Socket not inited in nw_Send\n"); |
| 2398 |
return 0; |
| 2399 |
} |
| 2400 |
|
| 2401 |
memset(iaddr, 0x00, 6); |
| 2402 |
memcpy(iaddr, who_to->address, 6); |
| 2403 |
|
| 2404 |
port = who_to->port; |
| 2405 |
|
| 2406 |
if (port == 0) { |
| 2407 |
mprintf(0, "Network ==> destination port %d invalid in psnet_send\n", port); |
| 2408 |
Int3(); |
| 2409 |
return 0; |
| 2410 |
} |
| 2411 |
|
| 2412 |
send_len = len; |
| 2413 |
send_data = (uint8_t *)packet_data; |
| 2414 |
|
| 2415 |
FD_ZERO(&wfds); |
| 2416 |
FD_SET(send_sock, &wfds); |
| 2417 |
|
| 2418 |
int sock_writable = select(send_sock + 1, NULL, &wfds, NULL, &timeout); |
| 2419 |
if (sock_writable == SOCKET_ERROR) { |
| 2420 |
mprintf(0, "Error on blocking select for write %d\n", WSAGetLastError()); |
| 2421 |
return 0; |
| 2422 |
} |
| 2423 |
|
| 2424 |
if (!sock_writable) { |
| 2425 |
// This packet gets dropped. |
| 2426 |
return 0; |
| 2427 |
} |
| 2428 |
|
| 2429 |
if (send_this_packet) { |
| 2430 |
switch (who_to->connection_type) { |
| 2431 |
case NP_TCP: |
| 2432 |
sock_addr.sin_family = AF_INET; |
| 2433 |
memcpy(&sock_addr.sin_addr.s_addr, iaddr, 4); |
| 2434 |
sock_addr.sin_port = htons(port); |
| 2435 |
|
| 2436 |
ret = sendto(TCP_socket, (char *)send_data, send_len, 0, (SOCKADDR *)&sock_addr, sizeof(sock_addr)); |
| 2437 |
break; |
| 2438 |
|
| 2439 |
default: |
| 2440 |
Int3(); // Unknown protocol |
| 2441 |
break; |
| 2442 |
|
| 2443 |
} // end switch |
| 2444 |
} |
| 2445 |
int lasterr; |
| 2446 |
if (ret != SOCKET_ERROR) { |
| 2447 |
return 1; |
| 2448 |
} |
| 2449 |
lasterr = WSAGetLastError(); |
| 2450 |
if (lasterr == WSAEWOULDBLOCK) { |
| 2451 |
return 0; |
| 2452 |
} |
| 2453 |
|
| 2454 |
mprintf(0, "Couldn't send data (%d)!\n", lasterr); |
| 2455 |
return 0; |
| 2456 |
} |
| 2457 |
|
| 2458 |
int nw_DoReceiveCallbacks(void) { |
| 2459 |
SOCKADDR_IN ip_addr; // UDP/TCP socket structure |
| 2460 |
socklen_t read_len, from_len; |
| 2461 |
network_address from_addr; |
| 2462 |
|
| 2463 |
uint8_t packet_data[1500]; |
| 2464 |
|
| 2465 |
nw_ReliableResend(); |
| 2466 |
|
| 2467 |
while (TCP_active) { |
| 2468 |
// check if there is any data on the socket to be read. The amount of data that can be |
| 2469 |
// atomically read is stored in len. |
| 2470 |
|
| 2471 |
#if 0 |
| 2472 |
fd_set rfds; |
| 2473 |
FD_ZERO(&rfds); |
| 2474 |
FD_SET( TCP_socket, &rfds ); |
| 2475 |
timeout.tv_sec = 0; |
| 2476 |
timeout.tv_usec = 0; |
| 2477 |
|
| 2478 |
timeval timeout; |
| 2479 |
if ( select( TCP_socket+1, &rfds, NULL, NULL, &timeout) == SOCKET_ERROR) |
| 2480 |
{ |
| 2481 |
mprintf(0, "Error %d doing a socket select on IP read\n", WSAGetLastError()); |
| 2482 |
break; |
| 2483 |
} |
| 2484 |
|
| 2485 |
// if the read file descriptor is not set, then bail! |
| 2486 |
if ( !FD_ISSET(TCP_socket, &rfds ) ) |
| 2487 |
break; |
| 2488 |
#endif |
| 2489 |
// get data off the socket and process |
| 2490 |
from_len = sizeof(SOCKADDR_IN); |
| 2491 |
read_len = recvfrom(TCP_socket, (char *)packet_data, 1500, 0, (SOCKADDR *)&ip_addr, &from_len); |
| 2492 |
|
| 2493 |
if (read_len == SOCKET_ERROR) { |
| 2494 |
int x = WSAGetLastError(); |
| 2495 |
if (x != WSAEWOULDBLOCK) { |
| 2496 |
mprintf(0, "Read error on IP socket. Winsock error %d \n", x); |
| 2497 |
} |
| 2498 |
break; |
| 2499 |
} |
| 2500 |
memset(&from_addr, 0x00, sizeof(network_address)); |
| 2501 |
from_addr.connection_type = NP_TCP; |
| 2502 |
from_addr.port = ntohs(ip_addr.sin_port); |
| 2503 |
|
| 2504 |
#ifdef WIN32 |
| 2505 |
memcpy(from_addr.address, &ip_addr.sin_addr.S_un.S_addr, 4); |
| 2506 |
#else |
| 2507 |
memcpy(from_addr.address, &ip_addr.sin_addr.s_addr, 4); |
| 2508 |
#endif |
| 2509 |
uint8_t packet_id = (packet_data[0] & 0x0f); |
| 2510 |
if (Netcallbacks[packet_id]) { |
| 2511 |
// mprintf(0,"Calling network callback for id %d.\n",packet_id); |
| 2512 |
int rlen = read_len - 1; |
| 2513 |
if (packet_id == NWT_UNRELIABLE) { |
| 2514 |
NetStatistics.udp_total_packets_rec++; |
| 2515 |
NetStatistics.udp_total_bytes_rec += rlen; |
| 2516 |
} else if (packet_id == NWT_RELIABLE) { |
| 2517 |
NetStatistics.tcp_total_packets_rec++; |
| 2518 |
NetStatistics.tcp_total_bytes_rec += rlen; |
| 2519 |
} |
| 2520 |
|
| 2521 |
Netcallbacks[packet_id](packet_data + 1, rlen, &from_addr); |
| 2522 |
} |
| 2523 |
} |
| 2524 |
|
| 2525 |
return 0; |
| 2526 |
} |
| 2527 |
|
| 2528 |
// MTS: only used in this file? |
| 2529 |
// Resend any unack'd packets and send any buffered packets, heartbeats, etc. |
| 2530 |
void nw_ReliableResend(void) { |
| 2531 |
int i, j; |
| 2532 |
int rcode = -1; |
| 2533 |
int16_t max_len = NETBUFFERSIZE; |
| 2534 |
static reliable_header rcv_buff; |
| 2535 |
static SOCKADDR rcv_addr; |
| 2536 |
int bytesin = 0; |
| 2537 |
int addrlen = sizeof(SOCKADDR); |
| 2538 |
reliable_socket *rsocket = NULL; |
| 2539 |
// Go through each reliable socket that is connected and do any needed work. |
| 2540 |
for (j = 0; j < MAXRELIABLESOCKETS; j++) { |
| 2541 |
rsocket = &reliable_sockets[j]; |
| 2542 |
|
| 2543 |
if (serverconn == -1) { |
| 2544 |
if (rsocket->status == RNF_LIMBO) |
| 2545 |
if ((timer_GetTime() - rsocket->last_packet_received) > NETTIMEOUT) { |
| 2546 |
mprintf(0, "Reliable (but in limbo) socket (%d) timed out in nw_WorkReliable().\n", j); |
| 2547 |
memset(rsocket, 0, sizeof(reliable_socket)); |
| 2548 |
rsocket->status = RNF_UNUSED; // Won't work if this is an outgoing connection. |
| 2549 |
} |
| 2550 |
} else { |
| 2551 |
if ((rsocket->status == RNF_LIMBO) && ((timer_GetTime() - first_sent_iamhere) > NETTIMEOUT)) { |
| 2552 |
rsocket->status = RNF_BROKEN; |
| 2553 |
mprintf(0, "Reliable socket (%d) timed out in nw_WorkReliable().\n", j); |
| 2554 |
} |
| 2555 |
} |
| 2556 |
|
| 2557 |
if (rsocket->status == RNF_CONNECTED) { |
| 2558 |
float retry_packet_time; |
| 2559 |
if ((rsocket->mean_ping == 0) || (rsocket->mean_ping > (NETRETRYTIME * 4))) { |
| 2560 |
retry_packet_time = NETRETRYTIME; |
| 2561 |
} else { |
| 2562 |
if (rsocket->mean_ping < MIN_NET_RETRYTIME) { |
| 2563 |
retry_packet_time = (float)MIN_NET_RETRYTIME; |
| 2564 |
// mprintf(0,"Using retransmission time of %f\n",retry_packet_time); |
| 2565 |
} else { |
| 2566 |
retry_packet_time = ((float)(float)rsocket->mean_ping * (float)1.25); |
| 2567 |
// mprintf(0,"Using retransmission time of %f\n",retry_packet_time); |
| 2568 |
} |
| 2569 |
} |
| 2570 |
// Iterate through send buffers. |
| 2571 |
for (i = 0; i < MAXNETBUFFERS; i++) { |
| 2572 |
if (((i == rsocket->waiting_packet_number) && |
| 2573 |
((((timer_GetTime() - rsocket->last_sent) > R_NET_PACKET_QUEUE_TIME)) || |
| 2574 |
((rsocket->mean_ping > 0) && (rsocket->mean_ping < R_NET_PACKET_QUEUE_TIME)) || rsocket->send_urgent)) || |
| 2575 |
((rsocket->sbuffers[i]) && ((timer_GetTime() - rsocket->timesent[i]) >= retry_packet_time))) // Send again |
| 2576 |
{ |
| 2577 |
|
| 2578 |
if (i == rsocket->waiting_packet_number) { |
| 2579 |
rsocket->waiting_packet_number = -1; |
| 2580 |
rsocket->last_sent = timer_GetTime(); |
| 2581 |
// mprintf(0,"Sending delayed packet...\n"); |
| 2582 |
} |
| 2583 |
reliable_header send_header; |
| 2584 |
// mprintf(0,"Resending reliable packet in nw_WorkReliable().\n"); |
| 2585 |
send_header.send_time = INTEL_FLOAT(timer_GetTime()); |
| 2586 |
send_header.seq = INTEL_SHORT(rsocket->ssequence[i]); |
| 2587 |
memcpy(send_header.data, rsocket->sbuffers[i]->buffer, rsocket->send_len[i]); |
| 2588 |
send_header.data_len = INTEL_SHORT(rsocket->send_len[i]); |
| 2589 |
send_header.type = RNT_DATA; |
| 2590 |
|
| 2591 |
network_address send_address; |
| 2592 |
memset(&send_address, 0, sizeof(network_address)); |
| 2593 |
|
| 2594 |
send_address.connection_type = rsocket->connection_type; |
| 2595 |
|
| 2596 |
if (NP_TCP == send_address.connection_type) { |
| 2597 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&rsocket->addr; |
| 2598 |
memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 2599 |
send_address.port = htons(inaddr->sin_port); |
| 2600 |
send_address.connection_type = NP_TCP; |
| 2601 |
|
| 2602 |
int len = RELIABLE_PACKET_HEADER_ONLY_SIZE + rsocket->send_len[i]; |
| 2603 |
NetStatistics.tcp_total_packets_sent--; // decrement because we are going to inc |
| 2604 |
// in nw_SendWithID |
| 2605 |
NetStatistics.tcp_total_bytes_sent -= len; // see above |
| 2606 |
NetStatistics.tcp_total_packets_resent++; |
| 2607 |
NetStatistics.tcp_total_bytes_resent += len; |
| 2608 |
} |
| 2609 |
|
| 2610 |
// mprintf(0,"Resending reliable packet! Sequence %d\n",send_header.seq); |
| 2611 |
rcode = nw_SendWithID(NWT_RELIABLE, (uint8_t *)&send_header, |
| 2612 |
RELIABLE_PACKET_HEADER_ONLY_SIZE + rsocket->send_len[i], &send_address); |
| 2613 |
|
| 2614 |
if ((rcode == SOCKET_ERROR) && (WSAEWOULDBLOCK == WSAGetLastError())) { |
| 2615 |
// The packet didn't get sent, flag it to try again next frame |
| 2616 |
rsocket->timesent[i] = timer_GetTime() - (NETRETRYTIME * 4); |
| 2617 |
} else { |
| 2618 |
rsocket->last_packet_sent = timer_GetTime(); |
| 2619 |
rsocket->timesent[i] = timer_GetTime(); |
| 2620 |
} |
| 2621 |
} |
| 2622 |
} |
| 2623 |
// We've sent all the packets, now we go out of urgent mode. |
| 2624 |
rsocket->send_urgent = 0; |
| 2625 |
if ((rsocket->status == RNF_CONNECTED) && ((timer_GetTime() - rsocket->last_packet_sent) > NETHEARTBEATTIME)) { |
| 2626 |
reliable_header send_header; |
| 2627 |
// mprintf(0,"Resending reliable packet in nw_WorkReliable().\n"); |
| 2628 |
send_header.send_time = INTEL_FLOAT(timer_GetTime()); |
| 2629 |
send_header.seq = INTEL_SHORT((int16_t)0); |
| 2630 |
send_header.data_len = INTEL_SHORT((int16_t)0); |
| 2631 |
send_header.type = RNT_HEARTBEAT; |
| 2632 |
|
| 2633 |
rcode = -1; |
| 2634 |
network_address send_address; |
| 2635 |
memset(&send_address, 0, sizeof(network_address)); |
| 2636 |
|
| 2637 |
send_address.connection_type = rsocket->connection_type; |
| 2638 |
|
| 2639 |
if (NP_TCP == send_address.connection_type) { |
| 2640 |
SOCKADDR_IN *inaddr = (SOCKADDR_IN *)&rsocket->addr; |
| 2641 |
memcpy(send_address.address, &inaddr->sin_addr, 4); |
| 2642 |
send_address.port = htons(inaddr->sin_port); |
| 2643 |
send_address.connection_type = NP_TCP; |
| 2644 |
|
| 2645 |
int len = RELIABLE_PACKET_HEADER_ONLY_SIZE + rsocket->send_len[i]; |
| 2646 |
NetStatistics.tcp_total_packets_sent--; // decrement because we are going to inc |
| 2647 |
// in nw_SendWithID |
| 2648 |
NetStatistics.tcp_total_bytes_sent -= len; // see above |
| 2649 |
NetStatistics.tcp_total_packets_resent++; |
| 2650 |
NetStatistics.tcp_total_bytes_resent += len; |
| 2651 |
} |
| 2652 |
|
| 2653 |
rcode = nw_SendWithID(NWT_RELIABLE, (uint8_t *)&send_header, RELIABLE_PACKET_HEADER_ONLY_SIZE, &send_address); |
| 2654 |
|
| 2655 |
if ((rcode != SOCKET_ERROR) && (WSAEWOULDBLOCK != WSAGetLastError())) { |
| 2656 |
// It must have been sent |
| 2657 |
rsocket->last_packet_sent = timer_GetTime(); |
| 2658 |
} |
| 2659 |
} |
| 2660 |
|
| 2661 |
if ((rsocket->status == RNF_CONNECTED) && ((timer_GetTime() - rsocket->last_packet_received) > NETTIMEOUT)) { |
| 2662 |
// This socket is hosed.....inform someone? |
| 2663 |
mprintf(0, "Reliable Socket (%d) timed out in nw_WorkReliable().\n", j); |
| 2664 |
rsocket->status = RNF_BROKEN; |
| 2665 |
} |
| 2666 |
} |
| 2667 |
} |
| 2668 |
} |
| 2669 |
|
| 2670 |
// fills in the buffer with network stats |
| 2671 |
// pass NULL to reset the stats |
| 2672 |
void nw_GetNetworkStats(tNetworkStatus *stats) { |
| 2673 |
if (!stats) { |
| 2674 |
// reset the stats |
| 2675 |
memset(&NetStatistics, 0, sizeof(NetStatistics)); |
| 2676 |
return; |
| 2677 |
} |
| 2678 |
|
| 2679 |
memcpy(stats, &NetStatistics, sizeof(NetStatistics)); |
| 2680 |
} |