From dbf83b373a5909b428133503f42111cdc95f62f9 Mon Sep 17 00:00:00 2001 From: p2r3 Date: Tue, 29 Oct 2024 23:16:50 +0200 Subject: [PATCH] revert to using static port for netcon --- setup/setup.iss | 2 +- tools/install.cpp | 12 +++------- tools/netcon.cpp | 56 ----------------------------------------------- tools/netcon.h | 1 - 4 files changed, 4 insertions(+), 67 deletions(-) diff --git a/setup/setup.iss b/setup/setup.iss index dca71c8..8fb42c4 100644 --- a/setup/setup.iss +++ b/setup/setup.iss @@ -2,7 +2,7 @@ ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES! #define MyAppName "Spplice" -#define MyAppVersion "0.5.4" +#define MyAppVersion "0.5.5" #define MyAppPublisher "PortalRunner" #define MyAppURL "https://p2r3.com/spplice" #define MyAppExeName "SppliceCPP.exe" diff --git a/tools/install.cpp b/tools/install.cpp index bccec98..be862a8 100644 --- a/tools/install.cpp +++ b/tools/install.cpp @@ -518,15 +518,9 @@ std::pair ToolsInstall::installPackageFile (const std::files // If the JS API is in use, enable the TCP console server if (jsEntryPointExists) { - // Find an open TCP port - SPPLICE_NETCON_PORT = ToolsNetCon::findOpenPort(1024, 49151); - if (SPPLICE_NETCON_PORT == -1) { - #ifndef TARGET_WINDOWS - return std::pair (false, "Failed to find an open port for the JS API."); - #else - return std::pair (false, L"Failed to find an open port for the JS API."); - #endif - } + // Use the default Source listen port, which is very likely to be free + // In Portal 2, only a UDP connection exists on this port by default + SPPLICE_NETCON_PORT = 27015; } // Start Portal 2 diff --git a/tools/netcon.cpp b/tools/netcon.cpp index 4a277c4..62deffc 100644 --- a/tools/netcon.cpp +++ b/tools/netcon.cpp @@ -34,62 +34,6 @@ void ToolsNetCon::disconnect (int sockfd) } #endif -// Finds an open TCP port in the given range (inclusive) -int ToolsNetCon::findOpenPort (const int start, const int end) { - -#ifdef TARGET_WINDOWS - // If no sockets are open, set up Winsock - if (++openSockets == 1) { - WSADATA wsaData; - if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) { - std::cerr << "WSAStartup failed." << std::endl; - return -1; - } - } -#endif - - for (int port = start; port <= end; port ++) { - - int sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) { - std::cerr << "Socket creation failed on port " << port << " when scanning for open ports." << std::endl; - break; - } - - struct sockaddr_in addr; - memset(&addr, 0, sizeof(addr)); - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = INADDR_ANY; - addr.sin_port = htons(port); - - int result = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr)); - if (result == 0) { - // Successfully bound to the port, close the socket and return the port - ToolsNetCon::disconnect(sockfd); - return port; - } - -#ifdef TARGET_WINDOWS - // Increment openSockets to prevent Winsock from being cleaned up - openSockets ++; -#endif - - // Port is in use, try the next one - ToolsNetCon::disconnect(sockfd); - - } - -#ifdef TARGET_WINDOWS - // Reset openSockets and clean up Winsock manually - openSockets --; - WSACleanup(); -#endif - - // No open port found in the range - return -1; - -} - // Attempts to connect to the game's TCP console on SPPLICE_NETCON_PORT int ToolsNetCon::attemptConnection () { diff --git a/tools/netcon.h b/tools/netcon.h index 206f5be..dfac5c9 100644 --- a/tools/netcon.h +++ b/tools/netcon.h @@ -7,7 +7,6 @@ class ToolsNetCon { public: static int attemptConnection (); static void disconnect (int sockfd); - static int findOpenPort (const int start, const int end); static bool sendCommand (int sockfd, std::string command); static std::string readConsole (int sockfd, size_t size); };