- Timestamp:
- Sep 9, 2014, 8:04:37 PM (10 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/Telnet.cs
r142 r182 9 9 public class Telnet : IServer 10 10 { 11 private staticThread telnetThread = null;12 private staticTcpListener listener = null;13 private staticbool closed = false;14 private staticbool authEnabled = false;15 private staticList<TelnetConnection> connections = new List<TelnetConnection> ();11 private Thread telnetThread = null; 12 private TcpListener listener = null; 13 private bool closed = false; 14 private bool authEnabled = false; 15 private List<TelnetConnection> connections = new List<TelnetConnection> (); 16 16 17 public Telnet ( int port)17 public Telnet () 18 18 { 19 19 try { 20 if (!GamePrefs.GetBool (EnumGamePrefs.TelnetEnabled)) { 21 return; 22 } 23 24 int port = GamePrefs.GetInt (EnumGamePrefs.TelnetPort); 25 20 26 authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0; 21 27 if (authEnabled) … … 25 31 telnetThread = ThreadMaster.Create ("thread TelnetListenThread", new ThreadStart (telnetListenThread)); 26 32 telnetThread.Start (); 27 Log.Out ("Started Telnet thread on " + port); 33 34 NetTelnetServer.RegisterServer (this); 35 36 Log.Out ("Started Telnet on " + port); 28 37 } catch (Exception e) { 29 38 Log.Out ("Error in Telnet.ctor: " + e); … … 34 43 { 35 44 try { 36 Log.Out ("Started thread_TelnetListenThread()");37 45 listener.Start (); 38 while (!closed) { 39 Thread.Sleep (10); 40 if (listener.Pending ()) { 46 try { 47 while (!closed) { 41 48 TelnetConnection c = new TelnetConnection (listener.AcceptTcpClient (), authEnabled); 42 49 connections.Add (c); 43 Log.Out ("Telnet connection from: " + c.GetEndPoint ());44 if (authEnabled) {45 c.WriteLine ("Please enter password:");46 } else {47 LoginMessage (c);48 }49 50 } 50 51 foreach (TelnetConnection c in connections) { 52 if (c.IsClosed ()) { 53 c.Close (); 54 connections.Remove (c); 55 break; 56 } 57 if (c.Read ()) { 58 string line = lineCorrecter (c.GetLine ()); 59 if (!c.IsAuthenticated ()) { 60 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) { 61 c.SetAuthenticated (); 62 c.WriteLine ("Logon successful."); 63 c.WriteLine (string.Empty); 64 c.WriteLine (string.Empty); 65 c.WriteLine (string.Empty); 66 LoginMessage (c); 67 } else { 68 c.WriteLine ("Password incorrect, please enter password:"); 69 } 70 } else { 71 if (line.ToLower ().Equals ("exit")) { 72 Log.Out ("Telnet connection closed by client: " + c.GetEndPoint ()); 73 c.Close (); 74 connections.Remove (c); 75 break; 76 } 77 Log.Out ("Telnet executed \"" + line + "\" from: " + c.GetEndPoint ()); 78 ConsoleOutputSeparator.QueueNetCommand (line, c); 79 } 80 } 81 } 51 } catch (SocketException) { 52 } catch (ThreadInterruptedException) { 82 53 } 83 Log.Out ("Exited thread_TelnetListenThread()");84 54 ThreadMaster.Remove (Thread.CurrentThread.Name); 85 55 } catch (Exception ex) { 86 Log.Out ("Error in TelnetListenThread: " + ex.Message); 87 Log.Out ("Stack Trace: " + ex.StackTrace); 56 Log.Out ("Error in TelnetListenThread: " + ex); 88 57 } 89 }90 91 private void LoginMessage (TelnetConnection c)92 {93 c.WriteLine ("*** Connected with 7DTD server.");94 c.WriteLine ("*** Dedicated server only build");95 c.WriteLine ("*** Allocs server fixes loaded");96 c.WriteLine (string.Empty);97 c.WriteLine ("Server IP: " +98 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any")99 );100 c.WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));101 c.WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));102 c.WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));103 c.WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));104 c.WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));105 c.WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));106 c.WriteLine (string.Empty);107 c.WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");108 c.WriteLine (string.Empty);109 }110 111 private string lineCorrecter (string line)112 {113 string res = "";114 for (int i = 0; i < line.Length; i++) {115 if (line [i] >= ' ' && line [i] != '\'') {116 res += line [i];117 }118 }119 return res.Trim ();120 58 } 121 59 … … 126 64 if (listener != null) { 127 65 listener.Stop (); 66 listener = null; 128 67 } 129 68 foreach (TelnetConnection c in connections) { … … 131 70 } 132 71 Thread.Sleep (100); 72 if (telnetThread != null) { 73 telnetThread.Interrupt (); 74 } 133 75 } catch (Exception e) { 134 76 Log.Out ("Error in Telnet.Disconnect: " + e); … … 139 81 { 140 82 try { 83 List<TelnetConnection> toRemove = new List<TelnetConnection> (); 141 84 foreach (TelnetConnection c in connections) { 142 if (c.IsClosed ()) { 143 c.Close (); 144 } 85 if (c.IsClosed ()) 86 toRemove.Add (c); 87 } 88 foreach (TelnetConnection c in toRemove) { 89 connections.Remove (c); 145 90 } 146 91 } catch (Exception e) { -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
r174 r182 1 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 2 4 using System.Net; 3 5 using System.Net.Sockets; 4 6 using System.Text; 7 using System.Threading; 5 8 6 9 namespace AllocsFixes.NetConnections.Servers.Telnet … … 8 11 public class TelnetConnection : IConnection 9 12 { 13 BlockingQueue<string> toClientQueue = new BlockingQueue<string> (); 14 private Thread receiveThread = null; 15 private Thread sendThread = null; 10 16 private bool authenticated = false; 11 17 private bool authEnabled; 12 18 private TcpClient client; 13 19 private NetworkStream stream; 14 private int lineBufferLength = 0;15 private byte[] lineBuffer = new byte[200];16 20 private EndPoint endpoint; 17 21 … … 22 26 this.stream = client.GetStream (); 23 27 this.endpoint = client.Client.RemoteEndPoint; 28 29 Log.Out ("Telnet connection from: " + endpoint); 30 31 receiveThread = ThreadMaster.Create ("TelnetClientReceive", new ThreadStart (ReceiveThread)); 32 receiveThread.Start (); 33 sendThread = ThreadMaster.Create ("TelnetClientSend", new ThreadStart (SendThread)); 34 sendThread.Start (); 35 36 if (authEnabled) { 37 WriteLine ("Please enter password:"); 38 } else { 39 LoginMessage (); 40 } 24 41 } 25 42 26 p ublic EndPoint GetEndPoint()43 private void LoginMessage () 27 44 { 28 return endpoint; 45 WriteLine ("*** Connected with 7DTD server."); 46 WriteLine ("*** Dedicated server only build"); 47 WriteLine ("*** Allocs server fixes loaded"); 48 WriteLine (string.Empty); 49 WriteLine ("Server IP: " + 50 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any") 51 ); 52 WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort)); 53 WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount)); 54 WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode)); 55 WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld)); 56 WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName)); 57 WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty)); 58 WriteLine (string.Empty); 59 WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session."); 60 WriteLine (string.Empty); 29 61 } 30 62 31 private void WriteByte (byte v)63 private void ReceiveThread () 32 64 { 33 if (!IsClosed () && stream.CanWrite) { 34 stream.WriteByte (v); 65 try { 66 StreamReader reader = new StreamReader (stream); 67 try { 68 while (!IsClosed()) { 69 string line = reader.ReadLine (); 70 if (line != null) { 71 line = line.Trim (); 72 73 if (!IsAuthenticated ()) { 74 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) { 75 authenticated = true; 76 WriteLine ("Logon successful."); 77 WriteLine (string.Empty); 78 WriteLine (string.Empty); 79 WriteLine (string.Empty); 80 LoginMessage (); 81 } else { 82 WriteLine ("Password incorrect, please enter password:"); 83 } 84 } else { 85 if (line.ToLower ().Equals ("exit")) { 86 Log.Out ("Telnet connection closed by client: " + endpoint); 87 Close (); 88 break; 89 } 90 Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint); 91 ConsoleOutputSeparator.QueueNetCommand (line, this); 92 } 93 } 94 } 95 } catch (IOException) { 96 } catch (ThreadInterruptedException) { 97 } 98 ThreadMaster.Remove (Thread.CurrentThread.Name); 99 } catch (Exception ex) { 100 Log.Out ("Error in TelnetClientReceive: " + ex); 35 101 } 102 103 } 104 105 private void SendThread () 106 { 107 try { 108 while (!IsClosed() && stream.CanWrite) { 109 try { 110 string line = toClientQueue.Dequeue (); 111 if (!IsClosed () && stream.CanWrite) { 112 byte[] utfData = Encoding.UTF8.GetBytes (line); 113 stream.Write (utfData, 0, utfData.Length); 114 stream.WriteByte (13); 115 stream.WriteByte (10); 116 } 117 } catch (IOException) { 118 } catch (ThreadInterruptedException) { 119 } catch (Exception e) { 120 Log.Out ("Error writing to client: " + e); 121 } 122 } 123 124 ThreadMaster.Remove (Thread.CurrentThread.Name); 125 } catch (Exception ex) { 126 Log.Out ("Error in TelnetClientSend: " + ex); 127 } 128 36 129 } 37 130 38 131 public void WriteLine (string s) 39 132 { 40 try { 41 if (!IsClosed () && stream.CanWrite) { 42 byte[] utfData = Encoding.UTF8.GetBytes (s); 43 stream.Write (utfData, 0, utfData.Length); 44 WriteByte (13); 45 WriteByte (10); 46 } 47 } catch (Exception e) { 48 Log.Out ("Error writing to client: " + e); 49 } 133 toClientQueue.Enqueue (s); 50 134 } 51 135 52 136 public void Close () 53 137 { 138 if (receiveThread != null) { 139 receiveThread.Interrupt (); 140 } 141 if (sendThread != null) { 142 sendThread.Interrupt (); 143 } 54 144 if (client != null) 55 145 client.Close (); … … 61 151 if (client != null && !client.Connected) { 62 152 Log.Out ("Telnet connection interrupted: " + endpoint); 153 Close (); 63 154 } 64 155 return (client == null) || (!client.Connected); … … 70 161 } 71 162 72 public void SetAuthenticated ()73 {74 authenticated = true;75 }76 77 public bool Read ()78 {79 while (!IsClosed() && stream.CanRead && stream.DataAvailable) {80 int b = stream.ReadByte ();81 if (b == '\r' || b == '\n') {82 if (lineBufferLength > 0) {83 return true;84 }85 } else if (b >= 0) {86 lineBuffer [lineBufferLength] = (byte)b;87 lineBufferLength++;88 }89 }90 return false;91 }92 93 public string GetLine ()94 {95 string res = Encoding.UTF8.GetString (lineBuffer, 0, lineBufferLength);96 lineBufferLength = 0;97 return res;98 }99 100 163 } 101 164 }
Note:
See TracChangeset
for help on using the changeset viewer.