public static void ConnectToServer(IPEndPoint[] endpoints, string password) { if (MainSystem.NetworkState > ClientState.Disconnected || endpoints == null || endpoints.Length == 0) { return; } MainSystem.NetworkState = ClientState.Connecting; SystemBase.TaskFactory.StartNew(() => { while (!PartModuleRunner.Ready) { MainSystem.Singleton.Status = $"Patching part modules (runs on every restart). {PartModuleRunner.GetPercentage()}%"; Thread.Sleep(50); } foreach (var endpoint in endpoints) { if (endpoint == null) { continue; } MainSystem.Singleton.Status = $"Connecting to {endpoint.Address}:{endpoint.Port}"; LunaLog.Log($"[LMP]: Connecting to {endpoint.Address} port {endpoint.Port}"); try { var client = NetworkMain.ClientConnection; if (client.Status == NetPeerStatus.NotRunning) { LunaLog.Log("[LMP]: Starting client"); client.Start(); } while (client.Status != NetPeerStatus.Running) { // Still trying to start up Thread.Sleep(50); } var outMsg = client.CreateMessage(password.GetByteCount()); outMsg.Write(password); var conn = client.Connect(endpoint, outMsg); if (conn == null) { // Lidgren says we're already connected, that's not possible LunaLog.LogError($"[LMP]: Invalid connection state, connected without connection"); client.Disconnect("Invalid state"); break; } client.FlushSendQueue(); while (conn.Status == NetConnectionStatus.InitiatedConnect || conn.Status == NetConnectionStatus.None) { // Still trying to connect Thread.Sleep(50); } if (client.ConnectionStatus == NetConnectionStatus.Connected) { LunaLog.Log($"[LMP]: Connected to {endpoint.Address}:{endpoint.Port}"); MainSystem.NetworkState = ClientState.Connected; break; } else { LunaLog.Log($"[LMP]: Initial connection timeout to {endpoint.Address}:{endpoint.Port}"); client.Disconnect("Initial connection timeout"); } } catch (Exception e) { NetworkMain.HandleDisconnectException(e); } } if (MainSystem.NetworkState < ClientState.Connected) { Disconnect(MainSystem.NetworkState == ClientState.Connecting ? "Initial connection timeout" : "Cancelled connection"); } }); }
public static void ConnectToServer(IPEndPoint endpoint, string password) { if (MainSystem.NetworkState > ClientState.Disconnected || endpoint == null) { return; } MainSystem.NetworkState = ClientState.Connecting; if (NetworkMain.ClientConnection.Status == NetPeerStatus.NotRunning) { NetworkMain.ClientConnection.Start(); } SystemBase.TaskFactory.StartNew(() => { while (!PartModuleRunner.Ready) { MainSystem.Singleton.Status = $"Patching part modules (runs on every restart). {PartModuleRunner.GetPercentage()}%"; Thread.Sleep(50); } MainSystem.Singleton.Status = $"Connecting to {endpoint.Address}:{endpoint.Port}"; LunaLog.Log($"[LMP]: Connecting to {endpoint.Address} port {endpoint.Port}"); try { var outMsg = NetworkMain.ClientConnection.CreateMessage(password.GetByteCount()); outMsg.Write(password); NetworkMain.ClientConnection.Connect(endpoint, outMsg); NetworkMain.ClientConnection.FlushSendQueue(); Thread.Sleep(SettingsSystem.CurrentSettings.MsBetweenConnectionTries); var connectionTrials = 0; while (NetworkMain.ClientConnection.ConnectionStatus != NetConnectionStatus.Connected && connectionTrials < SettingsSystem.CurrentSettings.ConnectionTries && MainSystem.NetworkState == ClientState.Connecting) { connectionTrials++; MainSystem.Singleton.Status = $"Connection retry [{connectionTrials + 1}] ({endpoint.Address}:{endpoint.Port})"; LunaLog.Log($"[LMP]: Connection retry [{connectionTrials}] ({endpoint.Address}:{endpoint.Port})"); NetworkMain.ClientConnection.Connect(endpoint, outMsg); NetworkMain.ClientConnection.FlushSendQueue(); Thread.Sleep(SettingsSystem.CurrentSettings.MsBetweenConnectionTries); } if (NetworkMain.ClientConnection.ConnectionStatus == NetConnectionStatus.Connected) { LunaLog.Log($"[LMP]: Connected to {endpoint.Address}:{endpoint.Port}"); MainSystem.NetworkState = ClientState.Connected; } else { Disconnect(MainSystem.NetworkState == ClientState.Connecting ? "Initial connection timeout" : "Cancelled connection"); } } catch (Exception e) { NetworkMain.HandleDisconnectException(e); } }); }