- Timestamp:
- Aug 26, 2014, 4:41:47 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/AllocsNetTelnetServer.cs
r115 r130 7 7 using System.Threading; 8 8 9 public class AllocsNetTelnetServer 9 namespace AllocsFixes 10 10 { 11 private static ConsoleSdtd console = null; 12 private static Thread telnetThread = null; 13 private static TcpListener listener = null; 14 private static bool closed = false; 15 private static bool authEnabled = false; 16 private static List<AllocsTelnetConnection> connections = new List<AllocsTelnetConnection> (); 11 public class AllocsNetTelnetServer 12 { 13 private static Thread telnetThread = null; 14 private static TcpListener listener = null; 15 private static bool closed = false; 16 private static bool authEnabled = false; 17 private static List<AllocsTelnetConnection> connections = new List<AllocsTelnetConnection> (); 17 18 18 public static void init (int port) 19 { 20 try { 21 Log.Out ("[7dtd-server-fixes by Alloc] Version: " + Assembly.GetExecutingAssembly ().GetName ().Version); 22 authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0; 23 if (authEnabled) 24 listener = new TcpListener (IPAddress.Any, port); 25 else 26 listener = new TcpListener (IPAddress.Loopback, port); 27 telnetThread = ThreadMaster.Create ("thread Allocs TelnetListenThread", new ThreadStart (telnetListenThread)); 28 telnetThread.Start (); 29 Log.Out ("Started Allocs NetTelnetServer thread on " + port); 30 } catch (Exception e) { 31 Log.Out ("Error in AllocsTelnetServer.init: " + e); 19 public static void init (int port) 20 { 21 try { 22 Log.Out ("[7dtd-server-fixes by Alloc] Version: " + Assembly.GetExecutingAssembly ().GetName ().Version); 23 authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0; 24 if (authEnabled) 25 listener = new TcpListener (IPAddress.Any, port); 26 else 27 listener = new TcpListener (IPAddress.Loopback, port); 28 telnetThread = ThreadMaster.Create ("thread Allocs TelnetListenThread", new ThreadStart (telnetListenThread)); 29 telnetThread.Start (); 30 Log.Out ("Started Allocs NetTelnetServer thread on " + port); 31 } catch (Exception e) { 32 Log.Out ("Error in AllocsTelnetServer.init: " + e); 33 } 32 34 } 33 }34 35 35 private static void telnetListenThread () 36 { 37 try { 38 Log.Out ("Started thread_Allocs_TelnetListenThread()"); 39 listener.Start (); 40 while (!closed) { 41 Thread.Sleep (10); 42 if (listener.Pending ()) { 43 AllocsTelnetConnection c = new AllocsTelnetConnection (listener.AcceptTcpClient (), authEnabled); 44 connections.Add (c); 45 Log.Out ("Telnet connection from: " + c.GetEndPoint ()); 46 if (authEnabled) { 47 c.WriteLine ("Please enter password:"); 48 } else { 49 LoginMessage (c); 36 private static void telnetListenThread () 37 { 38 try { 39 Log.Out ("Started thread_Allocs_TelnetListenThread()"); 40 listener.Start (); 41 while (!closed) { 42 Thread.Sleep (10); 43 if (listener.Pending ()) { 44 AllocsTelnetConnection c = new AllocsTelnetConnection (listener.AcceptTcpClient (), authEnabled); 45 connections.Add (c); 46 Log.Out ("Telnet connection from: " + c.GetEndPoint ()); 47 if (authEnabled) { 48 c.WriteLine ("Please enter password:"); 49 } else { 50 LoginMessage (c); 51 } 52 } 53 54 foreach (AllocsTelnetConnection c in connections) { 55 if (c.IsClosed ()) { 56 c.Close (); 57 connections.Remove (c); 58 break; 59 } 60 if (c.Read ()) { 61 string line = lineCorrecter (c.GetLine ()); 62 if (!c.IsAuthenticated ()) { 63 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) { 64 c.SetAuthenticated (); 65 c.WriteLine ("Logon successful."); 66 c.WriteLine (string.Empty); 67 c.WriteLine (string.Empty); 68 c.WriteLine (string.Empty); 69 LoginMessage (c); 70 } else { 71 c.WriteLine ("Password incorrect, please enter password:"); 72 } 73 } else { 74 if (line.ToLower ().Equals ("exit")) { 75 Log.Out ("Telnet connection closed by client: " + c.GetEndPoint ()); 76 c.Close (); 77 connections.Remove (c); 78 break; 79 } 80 Log.Out ("Telnet executed \"" + line + "\" from: " + c.GetEndPoint ()); 81 ConsoleOutputSeparator.QueueTelnetCommand (line, c); 82 } 83 } 50 84 } 51 85 } 86 Log.Out ("Exited thread_Allocs_TelnetListenThread()"); 87 ThreadMaster.Remove (Thread.CurrentThread.Name); 88 } catch (Exception ex) { 89 Log.Out ("Error in Allocs telnetListenThread: " + ex.Message); 90 Log.Out ("Stack Trace: " + ex.StackTrace); 91 } 92 } 52 93 94 private static void LoginMessage (AllocsTelnetConnection c) 95 { 96 c.WriteLine ("*** Connected with 7DTD server."); 97 c.WriteLine ("*** Dedicated server only build"); 98 c.WriteLine ("*** Allocs server fixes loaded"); 99 c.WriteLine (string.Empty); 100 c.WriteLine ("Server IP: " + 101 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any") 102 ); 103 c.WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort)); 104 c.WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount)); 105 c.WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode)); 106 c.WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld)); 107 c.WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName)); 108 c.WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty)); 109 c.WriteLine (string.Empty); 110 c.WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session."); 111 c.WriteLine (string.Empty); 112 } 113 114 private static string lineCorrecter (string line) 115 { 116 string res = ""; 117 for (int i = 0; i < line.Length; i++) { 118 if (line [i] >= ' ' && line [i] != '\'' && line [i] <= '~') { 119 res += line [i]; 120 } 121 } 122 return res.Trim (); 123 } 124 125 public static void Disconnect () 126 { 127 try { 128 closed = true; 129 if (listener != null) { 130 listener.Stop (); 131 } 132 foreach (AllocsTelnetConnection c in connections) { 133 c.Close (); 134 } 135 Thread.Sleep (100); 136 } catch (Exception e) { 137 Log.Out ("Error in AllocsTelnetServer.Disconnect: " + e); 138 } 139 } 140 141 public static void SetConsole (ConsoleSdtd console) 142 { 143 } 144 145 private static void RemoveClosedConnections () 146 { 147 try { 53 148 foreach (AllocsTelnetConnection c in connections) { 54 149 if (c.IsClosed ()) { 55 150 c.Close (); 56 connections.Remove (c);57 break;58 }59 if (c.Read ()) {60 string line = lineCorrecter (c.GetLine ());61 if (!c.IsAuthenticated ()) {62 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) {63 c.SetAuthenticated ();64 c.WriteLine ("Logon successful.");65 c.WriteLine (string.Empty);66 c.WriteLine (string.Empty);67 c.WriteLine (string.Empty);68 LoginMessage (c);69 } else {70 c.WriteLine ("Password incorrect, please enter password:");71 }72 } else {73 if (line.ToLower ().Equals ("exit")) {74 Log.Out ("Telnet connection closed by client: " + c.GetEndPoint ());75 c.Close ();76 connections.Remove (c);77 break;78 }79 Log.Out ("Telnet executed \"" + line + "\" from: " + c.GetEndPoint ());80 ConsoleOutputSeparator.QueueTelnetCommand (line, c);81 }82 151 } 83 152 } 84 } 85 Log.Out ("Exited thread_Allocs_TelnetListenThread()"); 86 ThreadMaster.Remove (Thread.CurrentThread.Name); 87 } catch (Exception ex) { 88 Log.Out ("Error in Allocs telnetListenThread: " + ex.Message); 89 Log.Out ("Stack Trace: " + ex.StackTrace); 90 } 91 } 92 93 private static void LoginMessage (AllocsTelnetConnection c) 94 { 95 c.WriteLine ("*** Connected with 7DTD server."); 96 c.WriteLine ("*** Dedicated server only build"); 97 c.WriteLine ("*** Allocs server fixes loaded"); 98 c.WriteLine (string.Empty); 99 c.WriteLine ("Server IP: " + 100 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any") 101 ); 102 c.WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort)); 103 c.WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount)); 104 c.WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode)); 105 c.WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld)); 106 c.WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName)); 107 c.WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty)); 108 c.WriteLine (string.Empty); 109 c.WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session."); 110 c.WriteLine (string.Empty); 111 } 112 113 private static string lineCorrecter (string line) 114 { 115 string res = ""; 116 for (int i = 0; i < line.Length; i++) { 117 if (line [i] >= ' ' && line [i] != '\'' && line [i] <= '~') { 118 res += line [i]; 153 } catch (Exception e) { 154 Log.Out ("Error in AllocsTelnetServer.RemoveClosedConnections: " + e); 119 155 } 120 156 } 121 return res.Trim ();122 }123 157 124 public static void Disconnect () 125 { 126 try { 127 closed = true; 128 if (listener != null) { 129 listener.Stop (); 158 public static void WriteToClient (string line) 159 { 160 if (line == null) { 161 return; 130 162 } 163 RemoveClosedConnections (); 131 164 foreach (AllocsTelnetConnection c in connections) { 132 c.Close (); 165 if (c.IsAuthenticated ()) 166 c.WriteLine (line); 133 167 } 134 Thread.Sleep (100);135 } catch (Exception e) {136 Log.Out ("Error in AllocsTelnetServer.Disconnect: " + e);137 168 } 138 }139 169 140 public static void SetConsole (ConsoleSdtd console) 141 { 142 Log.Out ("Telnet SetConsole called"); 143 AllocsNetTelnetServer.console = console; 144 } 145 146 private static void RemoveClosedConnections () 147 { 148 try { 170 public static void WriteToClient_Single (string line, AllocsTelnetConnection client) 171 { 172 if (line == null) { 173 return; 174 } 175 RemoveClosedConnections (); 149 176 foreach (AllocsTelnetConnection c in connections) { 150 if (c.IsClosed ()) { 151 c.Close (); 152 } 177 if (c.IsAuthenticated () && (c == client)) 178 c.WriteLine (line); 153 179 } 154 } catch (Exception e) {155 Log.Out ("Error in AllocsTelnetServer.RemoveClosedConnections: " + e);156 }157 }158 159 public static void WriteToClient (string line)160 {161 if (line == null) {162 return;163 }164 RemoveClosedConnections ();165 foreach (AllocsTelnetConnection c in connections) {166 if (c.IsAuthenticated ())167 c.WriteLine (line);168 }169 }170 171 public static void WriteToClient_Single (string line, AllocsTelnetConnection client)172 {173 if (line == null) {174 return;175 }176 RemoveClosedConnections ();177 foreach (AllocsTelnetConnection c in connections) {178 if (c.IsAuthenticated () && (c == client))179 c.WriteLine (line);180 180 } 181 181 }
Note:
See TracChangeset
for help on using the changeset viewer.