netutil.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  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. //
  19. // Filename: netutil.h
  20. // Project: wwnet
  21. // Author: Tom Spencer-Smith
  22. // Date: June 1998
  23. // Description:
  24. //
  25. //-----------------------------------------------------------------------------
  26. #if defined(_MSV_VER)
  27. #pragma once
  28. #endif
  29. #ifndef NETUTIL_H
  30. #define NETUTIL_H
  31. #include "win.h"
  32. #include <winsock.h>
  33. #include "bittype.h"
  34. class cPacket;
  35. #define WOULD_BLOCK(exp) cNetUtil::Would_Block(__FILE__, __LINE__, exp)
  36. #define SEND_RESOURCE_FAILURE(exp) cNetUtil::Send_Resource_Failure(__FILE__, __LINE__, exp)
  37. #define WSA_ERROR {cNetUtil::Wsa_Error(__FILE__, __LINE__);}
  38. #define WSA_CHECK(exp) {if ((exp) == SOCKET_ERROR) {cNetUtil::Wsa_Error(__FILE__, __LINE__);}}
  39. typedef void (*LanPacketHandlerCallback)(cPacket & packet);
  40. enum {
  41. MIN_SERVER_PORT = 1024, //1025,
  42. MAX_SERVER_PORT = 65535 //5000
  43. };
  44. class cNetUtil
  45. {
  46. public:
  47. static void Wsa_Init();
  48. static bool Protocol_Init(bool is_internet);
  49. static bool Get_Local_Address(LPSOCKADDR_IN p_local_address);
  50. static void Wsa_Error(LPCSTR sFile, unsigned uLine);
  51. static bool Is_Same_Address(LPSOCKADDR_IN p_address1, const SOCKADDR_IN* p_address2);
  52. static bool Would_Block(LPCSTR sFile, unsigned uLine, int ret_code);
  53. static bool Send_Resource_Failure(LPCSTR sFile, unsigned uLine, int ret_code);
  54. static void Address_To_String(LPSOCKADDR_IN p_address, char * str, UINT len,
  55. USHORT & port);
  56. static LPCSTR Address_To_String(ULONG ip_address);
  57. static void String_To_Address(LPSOCKADDR_IN p_address, LPCSTR str, USHORT port);
  58. static void Create_Unbound_Socket(SOCKET & sock);
  59. static bool Create_Bound_Socket(SOCKET & sock, USHORT port, SOCKADDR_IN & local_address);
  60. static void Close_Socket(SOCKET & sock);
  61. static void Create_Broadcast_Address(LPSOCKADDR_IN p_broadcast_address, USHORT port);
  62. static void Create_Local_Address(LPSOCKADDR_IN p_local_address, USHORT port);
  63. static void Broadcast(SOCKET & sock, USHORT port, cPacket & packet);
  64. static bool Is_Tcpip_Present();
  65. static void Lan_Servicing(SOCKET & sock, LanPacketHandlerCallback p_callback);
  66. static bool Is_Internet() {return IsInternet;}
  67. static void Set_Socket_Buffer_Sizes(SOCKET sock, int new_size = 10000);
  68. static UINT Get_Default_Resend_Timeout_Ms() {return DefaultResendTimeoutMs;}
  69. static const USHORT NETSTATS_SAMPLE_TIME_MS;
  70. static const USHORT KEEPALIVE_TIMEOUT_MS;
  71. static const USHORT MAX_RESENDS;
  72. static const USHORT MULTI_SENDS;
  73. static const USHORT RESEND_TIMEOUT_LAN_MS;
  74. static const USHORT RESEND_TIMEOUT_INTERNET_MS;
  75. static const ULONG CLIENT_CONNECTION_LOSS_TIMEOUT;
  76. static const ULONG SERVER_CONNECTION_LOSS_TIMEOUT;
  77. static const ULONG SERVER_CONNECTION_LOSS_TIMEOUT_LOADING_ALLOWANCE;
  78. static const char *Winsock_Error_Text(int error_code);
  79. private:
  80. static int Get_Local_Tcpip_Addresses(SOCKADDR_IN ip_address[], USHORT max_addresses);
  81. static bool IsInternet;
  82. static UINT DefaultResendTimeoutMs;
  83. static char WorkingAddressBuffer[300];
  84. };
  85. #endif // NETUTIL_H
  86. //static USHORT Get_Header_Bytes() {return HeaderBytes;}
  87. //static USHORT Get_Max_Packet_App_Data_Size() {return MaxPacketAppDataSize;}
  88. //static USHORT HeaderBytes;
  89. //static USHORT MaxPacketAppDataSize;
  90. //static const WORD WS_VERSION_REQD;
  91. //static int Get_Net_Stats_Sample_Time_Ms() {return NETSTATS_SAMPLE_TIME_MS;}
  92. //static int Get_Default_Multi_Sends() {return DefaultMultiSends;}
  93. //static int Get_Default_Max_Resends() {return DefaultMaxResends;}
  94. //static int Get_Default_Keepalive_Timeout_Ms() {return DefaultKeepaliveTimeoutMs;}
  95. //static int Get_Desired_Send_Buffer_Size_Bytes() {return DesiredSendBufferSizeBytes;}
  96. //static int Get_Desired_Receive_Buffer_Size_Bytes() {return DesiredReceiveBufferSizeBytes;}
  97. //static int Get_Default_Server_Port() {return DefaultServerPort;}
  98. //static int Get_Max_Receive_Time_Ms() {return MaxReceiveTimeMs;}
  99. //static float Get_Priority_Tolerance_Downwards() {return PriorityToleranceDownwards;}
  100. //static float Get_Priority_Tolerance_Upwards() {return PriorityToleranceUpwards;}
  101. //static float Get_Max_TP_Correction_Downwards() {return MaxTPCorrectionDownwards;}
  102. //static float Get_Max_TP_Correction_Upwards() {return MaxTPCorrectionUpwards;}
  103. //static float Get_Priority_Noise_Factor() {return PriorityNoiseFactor;}
  104. //static float Get_Initial_Threshold_Priority() {return InitialThresholdPriority;}
  105. //static float Get_Priority_Growth_Per_Second() {return PriorityGrowthPerSecond;}
  106. //static float Compute_Priority_Noise();
  107. //static int DefaultMultiSends; // Number of sends in a SEND_MULTI
  108. //static int DefaultMaxResends; // If you exceed this then the connection is regarded as broken
  109. //static int DefaultKeepaliveTimeoutMs;
  110. //static int DesiredSendBufferSizeBytes;
  111. //static int DesiredReceiveBufferSizeBytes;
  112. //static int DefaultServerPort;
  113. //static int MaxReceiveTimeMs;
  114. //static float PriorityToleranceDownwards;
  115. //static float PriorityToleranceUpwards;
  116. //static float MaxTPCorrectionDownwards;
  117. //static float MaxTPCorrectionUpwards;
  118. //static float PriorityNoiseFactor;
  119. //static float InitialThresholdPriority;
  120. //static float PriorityGrowthPerSecond;
  121. //static int Get_Default_Resend_Timeout_Lan_Ms() {return RESEND_TIMEOUT_LAN_MS;}
  122. //static int Get_Default_Resend_Timeout_Internet_Ms() {return RESEND_TIMEOUT_INTERNET_MS;}
  123. //static void Onetime_Init();