From afebc0d1d3cf123e99e0d8c227de601399c17a7e Mon Sep 17 00:00:00 2001 From: Shawn Hoffman Date: Wed, 19 Jan 2011 02:17:11 +0000 Subject: [PATCH] Allow usbgecko to try a range of ports, such that multiple dolphin instances will setup their servers on consecutive ports starting at 55020. git-svn-id: https://dolphin-emu.googlecode.com/svn/trunk@6877 8ced0084-cf51-0410-be5f-012b33b47a6e --- Externals/SFML/src/SFML/Network/SocketTCP.cpp | 6 +++++- Source/Core/Core/Src/HW/EXI_DeviceGecko.cpp | 18 ++++++++++++++---- Source/Core/Core/Src/HW/EXI_DeviceGecko.h | 1 + 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Externals/SFML/src/SFML/Network/SocketTCP.cpp b/Externals/SFML/src/SFML/Network/SocketTCP.cpp index d4318555ac..8e9aa33f81 100644 --- a/Externals/SFML/src/SFML/Network/SocketTCP.cpp +++ b/Externals/SFML/src/SFML/Network/SocketTCP.cpp @@ -488,14 +488,18 @@ void SocketTCP::Create(SocketHelper::SocketType Descriptor) // Setup default options if (IsValid()) { + /* We must disable this in order to detect if ports are being used by other apps, or + other instances of dolphin. This is also disabled in SFML 2.0, see + http://www.sfml-dev.org/forum/viewtopic.php?t=3388 // To avoid the "Address already in use" error message when trying to bind to the same port - int Yes = 1; if (setsockopt(mySocket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&Yes), sizeof(Yes)) == -1) { std::cerr << "Failed to set socket option \"SO_REUSEADDR\" ; " << "binding to a same port may fail if too fast" << std::endl; } + */ + int Yes = 1; // Disable the Nagle algorithm (ie. removes buffering of TCP packets) if (setsockopt(mySocket, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast(&Yes), sizeof(Yes)) == -1) { diff --git a/Source/Core/Core/Src/HW/EXI_DeviceGecko.cpp b/Source/Core/Core/Src/HW/EXI_DeviceGecko.cpp index 828f8e41da..edc06f528e 100644 --- a/Source/Core/Core/Src/HW/EXI_DeviceGecko.cpp +++ b/Source/Core/Core/Src/HW/EXI_DeviceGecko.cpp @@ -17,6 +17,7 @@ #include "EXI_Device.h" #include "EXI_DeviceGecko.h" +#include "..\Core.h" THREAD_RETURN ClientThreadFunc(void *arg) { @@ -24,6 +25,7 @@ THREAD_RETURN ClientThreadFunc(void *arg) return 0; } +u16 GeckoSockServer::server_port; int GeckoSockServer::client_count; Common::Thread *GeckoSockServer::connectionThread = NULL; volatile bool GeckoSockServer::server_running; @@ -57,15 +59,23 @@ GeckoSockServer::~GeckoSockServer() THREAD_RETURN GeckoSockServer::GeckoConnectionWaiter(void*) { - server_running = true; - Common::SetCurrentThreadName("Gecko Connection Waiter"); sf::SocketTCP server; - // "dolphin gecko" - if (!server.Listen(0xd6ec)) + server_port = 0xd6ec; // "dolphin gecko" + for (int bind_tries = 0; bind_tries <= 10 && !server_running; bind_tries++) + { + if (!(server_running = server.Listen(server_port))) + server_port++; + } + + if (!server_running) return 0; + Core::DisplayMessage( + StringFromFormat("USBGecko: listening on TCP port %u", server_port), + 5000); + server.SetBlocking(false); sf::SocketTCP new_client; diff --git a/Source/Core/Core/Src/HW/EXI_DeviceGecko.h b/Source/Core/Core/Src/HW/EXI_DeviceGecko.h index 93c9f2fbda..7c62fc1dfa 100644 --- a/Source/Core/Core/Src/HW/EXI_DeviceGecko.h +++ b/Source/Core/Core/Src/HW/EXI_DeviceGecko.h @@ -46,6 +46,7 @@ private: // Only ever one server thread static THREAD_RETURN GeckoConnectionWaiter(void*); + static u16 server_port; static volatile bool server_running; static Common::Thread *connectionThread; static std::queue waiting_socks;