From 48b2383096bf5dfa39ce4e7bf904a2a849165813 Mon Sep 17 00:00:00 2001 From: willkuer Date: Sun, 14 Apr 2019 12:04:58 +0200 Subject: [PATCH] [skip ci] GSDumpGUI: Fixes multiinstance crashes. (#2925) - automatically find the next free TCP port yielded by OS - transfering currently used instance-fixed port to launched clients - connect each client via given port See #1637 --- tools/GSDumpGUI/Core/Program.cs | 6 +++--- tools/GSDumpGUI/Forms/frmMain.cs | 3 ++- .../Library/TCPLibrary/Base/Server.cs | 19 ++++++------------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/tools/GSDumpGUI/Core/Program.cs b/tools/GSDumpGUI/Core/Program.cs index a31274e8b9..5db3d08ed0 100644 --- a/tools/GSDumpGUI/Core/Program.cs +++ b/tools/GSDumpGUI/Core/Program.cs @@ -52,16 +52,17 @@ namespace GSDumpGUI [STAThread] static void Main(String[] args) { - if (args.Length == 4) + if (args.Length == 5) { // do this first, else racy mess ;) wrap = new GSDXWrapper(); + var port = Convert.ToInt32(args[4]); try { Client = new TCPLibrary.MessageBased.Core.BaseMessageClient(); Client.OnMessageReceived += new TCPLibrary.MessageBased.Core.BaseMessageClient.MessageReceivedHandler(Client_OnMessageReceived); - Client.Connect("localhost", 9999); + Client.Connect("localhost", port); } catch (Exception) { @@ -137,7 +138,6 @@ namespace GSDumpGUI Server.OnClientMessageReceived += new BaseMessageServer.MessageReceivedHandler(Server_OnClientMessageReceived); Server.OnClientAfterConnect += new TCPLibrary.Core.Server.ConnectedHandler(Server_OnClientAfterConnect); Server.OnClientAfterDisconnected += new TCPLibrary.Core.Server.DisconnectedHandler(Server_OnClientAfterDisconnected); - Server.Port = 9999; Server.Enabled = true; Application.EnableVisualStyles(); diff --git a/tools/GSDumpGUI/Forms/frmMain.cs b/tools/GSDumpGUI/Forms/frmMain.cs index 39ebb304a9..c488c089a4 100644 --- a/tools/GSDumpGUI/Forms/frmMain.cs +++ b/tools/GSDumpGUI/Forms/frmMain.cs @@ -238,6 +238,7 @@ namespace GSDumpGUI if (lstDumps.SelectedItem != null) DumpPath = Properties.Settings.Default.DumpDir + "\\" + lstDumps.SelectedItem.ToString().Split(new char[] { '|' })[0]; + var port = Program.Server.Port; // Start the child and link the events. ProcessStartInfo psi = new ProcessStartInfo(); @@ -246,7 +247,7 @@ namespace GSDumpGUI psi.RedirectStandardError = false; psi.CreateNoWindow = true; psi.FileName = Process.GetCurrentProcess().ProcessName; - psi.Arguments = "\"" + DLLPath + "\"" + " \"" + DumpPath + "\"" + " \"" + Function + "\"" + " " + SelectedRenderer; + psi.Arguments = "\"" + DLLPath + "\"" + " \"" + DumpPath + "\"" + " \"" + Function + "\"" + " " + SelectedRenderer + " " + port; Process p = Process.Start(psi); p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived); p.BeginOutputReadLine(); diff --git a/tools/GSDumpGUI/Library/TCPLibrary/Base/Server.cs b/tools/GSDumpGUI/Library/TCPLibrary/Base/Server.cs index 632bca4ab7..031c65f15c 100644 --- a/tools/GSDumpGUI/Library/TCPLibrary/Base/Server.cs +++ b/tools/GSDumpGUI/Library/TCPLibrary/Base/Server.cs @@ -42,10 +42,6 @@ namespace TCPLibrary.Core /// private TcpListener _socket; /// - /// Port to which the server will listen. - /// - private Int32 _port; - /// /// Whether the server is enabled or not. /// private Boolean _enabled; @@ -132,13 +128,11 @@ namespace TCPLibrary.Core /// public Int32 Port { - get { return _port; } - set + get { - if (Enabled == false) - _port = value; - else - throw new ArgumentException("Impossibile eseguire l'operazione a server attivo"); + if (Enabled) + return ((IPEndPoint) _socket.LocalEndpoint).Port; + throw new NotSupportedException("Server is not running and hence has no port."); } } @@ -212,10 +206,10 @@ namespace TCPLibrary.Core /// protected virtual void ActivateServer() { - _socket = new TcpListener(IPAddress.Any, Port); + _socket = new TcpListener(IPAddress.Any, 0); _socket.Start(ConnectionBackLog); Thread thd = new Thread(new ThreadStart(MainThread)); - thd.Name = "Server on port " + Port.ToString(); + thd.Name = "Server on port " + ((IPEndPoint) _socket.LocalEndpoint).Port; thd.IsBackground = true; thd.Start(); _enabled = true; @@ -249,7 +243,6 @@ namespace TCPLibrary.Core public Server() { _clients = new List(); - _port = 0; _connectionbacklog = 0; _enabled = false; }