From 6252db792838b43171ce0485fafbe041ac0dbeeb Mon Sep 17 00:00:00 2001 From: Regalis Date: Sat, 14 Jan 2017 16:57:47 +0200 Subject: [PATCH] - fixed server validating its own name instead of the name of a client who's logging in - clients automatically reconnect to the server when the connection is lost and return back to server list if they fail to reconnect - showing the error msg as a GUIMessageBox and returning to main menu if starting a server fails --- Subsurface/Source/Networking/GameClient.cs | 52 ++++++++++------ Subsurface/Source/Networking/GameServer.cs | 26 +++++++- .../Source/Networking/GameServerLogin.cs | 60 +++++++++---------- 3 files changed, 88 insertions(+), 50 deletions(-) diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index 659e7c8dd..7be0e01d0 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -163,10 +163,6 @@ namespace Barotrauma.Networking updateInterval = new TimeSpan(0, 0, 0, 0, 150); CoroutineManager.StartCoroutine(WaitForStartingInfo()); - - // Start the timer - //update.Start(); - } private bool RetryConnection(GUIButton button, object obj) @@ -176,19 +172,18 @@ namespace Barotrauma.Networking return true; } - private bool SelectMainMenu(GUIButton button, object obj) + private bool ReturnToServerList(GUIButton button, object obj) { Disconnect(); + + Submarine.Unload(); GameMain.NetworkMember = null; - GameMain.MainMenuScreen.Select(); - - GameMain.MainMenuScreen.SelectTab(MainMenuScreen.Tab.LoadGame); - + GameMain.ServerListScreen.Select(); + return true; } private bool connectCancelled; - private bool CancelConnect(GUIButton button, object obj) { connectCancelled = true; @@ -318,8 +313,9 @@ namespace Barotrauma.Networking if (connectionStatus == NetConnectionStatus.Disconnected) { string denyMessage = inc.ReadString(); - - new GUIMessageBox("Couldn't connect to server", denyMessage); + + var cantConnectMsg = new GUIMessageBox("Couldn't connect to the server", denyMessage); + cantConnectMsg.Buttons[0].OnClicked += ReturnToServerList; connectCancelled = true; } @@ -329,7 +325,7 @@ namespace Barotrauma.Networking catch (Exception e) { - DebugConsole.ThrowError("Error while connecting to server", e); + DebugConsole.ThrowError("Error while connecting to the server", e); break; } @@ -360,7 +356,8 @@ namespace Barotrauma.Networking { string denyMessage = inc.ReadString(); - new GUIMessageBox("Couldn't connect to server", denyMessage); + var cantConnectMsg = new GUIMessageBox("Couldn't connect to the server", denyMessage); + cantConnectMsg.Buttons[0].OnClicked += ReturnToServerList; msgBox.Close(null, null); connectCancelled = true; @@ -415,13 +412,13 @@ namespace Barotrauma.Networking if (client.ConnectionStatus != NetConnectionStatus.Connected) { - var reconnect = new GUIMessageBox("CONNECTION FAILED", "Failed to connect to server.", new string[] { "Retry", "Cancel" }); + var reconnect = new GUIMessageBox("CONNECTION FAILED", "Failed to connect to the server.", new string[] { "Retry", "Cancel" }); - DebugConsole.NewMessage("Failed to connect to server - connection status: "+client.ConnectionStatus.ToString(), Color.Orange); + DebugConsole.NewMessage("Failed to connect to the server - connection status: "+client.ConnectionStatus.ToString(), Color.Orange); reconnect.Buttons[0].OnClicked += RetryConnection; reconnect.Buttons[0].OnClicked += reconnect.Close; - reconnect.Buttons[1].OnClicked += SelectMainMenu; + reconnect.Buttons[1].OnClicked += ReturnToServerList; reconnect.Buttons[1].OnClicked += reconnect.Close; } else @@ -542,6 +539,27 @@ namespace Barotrauma.Networking break; } break; + case NetIncomingMessageType.StatusChanged: + NetConnectionStatus connectionStatus = (NetConnectionStatus)inc.ReadByte(); + DebugConsole.NewMessage("Connection status changed: " + connectionStatus.ToString(), Color.Orange); + + if (connectionStatus == NetConnectionStatus.Disconnected) + { + string disconnectMsg = inc.ReadString(); + if (disconnectMsg == "The server has been shut down") + { + var msgBox = new GUIMessageBox("CONNECTION LOST", "The server has been shut down"); + msgBox.Buttons[0].OnClicked += ReturnToServerList; + } + else if (reconnectBox == null) + { + reconnectBox = new GUIMessageBox("CONNECTION LOST", "You have been disconnected from the server. Reconnecting...", new string[0]); + connected = false; + ConnectToServer(serverIP); + } + + } + break; } } } diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index d9f19a33c..716c7e5b4 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -137,6 +137,7 @@ namespace Barotrauma.Networking private IEnumerable StartServer(bool isPublic) { + bool error = false; try { Log("Starting the server...", Color.Cyan); @@ -146,10 +147,29 @@ namespace Barotrauma.Networking } catch (Exception e) { - Log("Error while starting the server ("+e.Message+")", Color.Red); - DebugConsole.ThrowError("Couldn't start the server", e); + Log("Error while starting the server (" + e.Message + ")", Color.Red); + + System.Net.Sockets.SocketException socketException = e as System.Net.Sockets.SocketException; + + if (socketException != null && socketException.SocketErrorCode == System.Net.Sockets.SocketError.AddressAlreadyInUse) + { + new GUIMessageBox("Starting the server failed", e.Message + ". Are you trying to run multiple servers on the same port?"); + } + else + { + new GUIMessageBox("Starting the server failed", e.Message); + } + + error = true; + } + + if (error) + { + if (server != null) server.Shutdown("Error while starting the server"); + + GameMain.NetworkMember = null; + yield return CoroutineStatus.Success; } - if (config.EnableUPnP) { diff --git a/Subsurface/Source/Networking/GameServerLogin.cs b/Subsurface/Source/Networking/GameServerLogin.cs index 712650868..ae4ccdae3 100644 --- a/Subsurface/Source/Networking/GameServerLogin.cs +++ b/Subsurface/Source/Networking/GameServerLogin.cs @@ -124,31 +124,6 @@ namespace Barotrauma.Networking string clPackageName = inc.ReadString(); string clPackageHash = inc.ReadString(); - if (clVersion != GameMain.Version.ToString()) - { - inc.SenderConnection.Disconnect("Version " + GameMain.Version + " required to connect to the server (Your version: " + clVersion + ")"); - unauthenticatedClients.Remove(unauthClient); - unauthClient = null; - DebugConsole.NewMessage(name + " couldn't join the server (wrong game version)", Color.Red); - return; - } - if (clPackageName != GameMain.SelectedPackage.Name) - { - inc.SenderConnection.Disconnect("Your content package (" + clPackageName + ") doesn't match the server's version (" + GameMain.SelectedPackage.Name + ")"); - unauthenticatedClients.Remove(unauthClient); - unauthClient = null; - DebugConsole.NewMessage(name + " couldn't join the server (wrong content package name)", Color.Red); - return; - } - if (clPackageHash != GameMain.SelectedPackage.MD5hash.Hash) - { - unauthClient.Connection.Disconnect("Your content package (MD5: " + clPackageHash + ") doesn't match the server's version (MD5: " + GameMain.SelectedPackage.MD5hash.Hash + ")"); - unauthenticatedClients.Remove(unauthClient); - unauthClient = null; - DebugConsole.NewMessage(name + " couldn't join the server (wrong content package hash)", Color.Red); - return; - } - string clName = Client.SanitizeName(inc.ReadString()); if (string.IsNullOrWhiteSpace(clName)) { @@ -157,14 +132,39 @@ namespace Barotrauma.Networking unauthClient = null; return; } - - if (!whitelist.IsWhiteListed(name, inc.SenderConnection.RemoteEndPoint.Address.ToString())) + + if (clVersion != GameMain.Version.ToString()) { - inc.SenderConnection.Disconnect("You're not in this server's whitelist."); - DebugConsole.NewMessage(name + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (not in whitelist)", Color.Red); + inc.SenderConnection.Disconnect("Version " + GameMain.Version + " required to connect to the server (Your version: " + clVersion + ")"); + unauthenticatedClients.Remove(unauthClient); + unauthClient = null; + DebugConsole.NewMessage(clName + " couldn't join the server (wrong game version)", Color.Red); return; } - if (!Client.IsValidName(name)) + if (clPackageName != GameMain.SelectedPackage.Name) + { + inc.SenderConnection.Disconnect("Your content package (" + clPackageName + ") doesn't match the server's version (" + GameMain.SelectedPackage.Name + ")"); + unauthenticatedClients.Remove(unauthClient); + unauthClient = null; + DebugConsole.NewMessage(clName + " couldn't join the server (wrong content package name)", Color.Red); + return; + } + if (clPackageHash != GameMain.SelectedPackage.MD5hash.Hash) + { + unauthClient.Connection.Disconnect("Your content package (MD5: " + clPackageHash + ") doesn't match the server's version (MD5: " + GameMain.SelectedPackage.MD5hash.Hash + ")"); + unauthenticatedClients.Remove(unauthClient); + unauthClient = null; + DebugConsole.NewMessage(clName + " couldn't join the server (wrong content package hash)", Color.Red); + return; + } + + if (!whitelist.IsWhiteListed(clName, inc.SenderConnection.RemoteEndPoint.Address.ToString())) + { + inc.SenderConnection.Disconnect("You're not in this server's whitelist."); + DebugConsole.NewMessage(clName + " (" + inc.SenderConnection.RemoteEndPoint.Address.ToString() + ") couldn't join the server (not in whitelist)", Color.Red); + return; + } + if (!Client.IsValidName(clName)) { unauthClient.Connection.Disconnect("Your name contains illegal symbols."); unauthenticatedClients.Remove(unauthClient);