Skip to content

Commit

Permalink
revert to using static port for netcon
Browse files Browse the repository at this point in the history
  • Loading branch information
p2r3 committed Oct 29, 2024
1 parent ac18046 commit dbf83b3
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 67 deletions.
2 changes: 1 addition & 1 deletion setup/setup.iss
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
12 changes: 3 additions & 9 deletions tools/install.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -518,15 +518,9 @@ std::pair<bool, std::wstring> 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<bool, std::string> (false, "Failed to find an open port for the JS API.");
#else
return std::pair<bool, std::wstring> (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
Expand Down
56 changes: 0 additions & 56 deletions tools/netcon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {

Expand Down
1 change: 0 additions & 1 deletion tools/netcon.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down

0 comments on commit dbf83b3

Please sign in to comment.