public SDaysTDieServer(string folder, ServerSettings serverSettings) { Folder = folder; ServerSettings = serverSettings; TelnetCredentials = new TelnetCredentials("", serverSettings.TelnetPassword, serverSettings.TelnetPort); if (serverSettings.ServerConfigFilepath.EndsWith("serverconfig.xml")) { throw new ArgumentException("serverSettings.ServerConfigFilePath.EndsWith('serverconfig.xml')! Use of Default File is not allowed!"); } }
private async void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e) { while (!BackgroundWorker.CancellationPending) { try { TelnetCredentials telnetCredentials = (TelnetCredentials)e.Argument; while ((TelnetClient == null || TelnetClient.IsConnected == false) && !BackgroundWorker.CancellationPending) { try { Console.WriteLine("*** Start Telnet Connection ***"); IByteStream byteStream = new TcpByteStream("localhost", telnetCredentials.Port); TelnetClient = new Client(byteStream, new CancellationToken(), TimeSpan.FromSeconds(1)); //CancellationTokenSource ReceiveAsyncTokenSource = new CancellationTokenSource(); //ReceiveAsyncTokenSource.CancelAfter(TimeSpan.FromSeconds(1)); //ReceiveAsyncTokenSource.Token.Register(CancelNotification); //TelnetClient = new Client("localhost", telnetCredentials.Port, ReceiveAsyncTokenSource.Token); } catch (Exception ex) { if (ex.Message.Contains("Unable to connect to the host")) { Console.WriteLine("*** Unable to connect to the host - retry ***"); } else { Console.WriteLine("*** Unknown error on connection - closing ***"); return; } } if (BackgroundWorker.CancellationPending == true) { return; } } while (TelnetClient.IsConnected == true && !BackgroundWorker.CancellationPending) { await ReadAndSendEvent(); if (!ClientReadAsync.Result.Contains("Connected with 7DTD server")) { Console.WriteLine("*** Sending Password: '******'***"); await TelnetClient.WriteLine(telnetCredentials.Password); await Task.Delay(1000); await ReadAndSendEvent(); if (ClientReadAsync.Result.Contains("Password incorrect")) { Console.WriteLine("*** Password incorrect detected - closing connection ***"); return; } } int counterUntilPing = 3; while (TelnetClient.IsConnected == true && !BackgroundWorker.CancellationPending) { counterUntilPing--; if (counterUntilPing <= 0) { counterUntilPing = 30; await AsyncPingTelnetServer(); } await ReadAndSendEvent(); await Task.Delay(2000); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); Console.WriteLine("*** Exception occured while connected - wait 10 seconds before going on ***"); TelnetClient = null; await Task.Delay(10000); } } }
public TelnetHandler(TelnetCredentials telnetCredentials) { BackgroundWorker.WorkerSupportsCancellation = true; BackgroundWorker.DoWork += BackgroundWorker_DoWork; BackgroundWorker.RunWorkerAsync(telnetCredentials); }