Changeset 190 for binary-improvements/7dtd-server-fixes
- Timestamp:
- Sep 13, 2014, 12:55:47 PM (10 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/BlockingQueue.cs
r182 r190 7 7 public class BlockingQueue<T> 8 8 { 9 private bool closing = false; 9 10 private Queue<T> queue = new Queue<T> (); 10 11 … … 21 22 lock (queue) { 22 23 while (queue.Count == 0) { 24 if (closing) { 25 return default(T); 26 } 23 27 Monitor.Wait (queue); 24 28 } … … 27 31 } 28 32 33 public void Close () 34 { 35 lock (queue) { 36 closing = true; 37 Monitor.PulseAll (queue); 38 } 39 } 29 40 30 41 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/ConsoleOutputSeparator.cs
r189 r190 48 48 } else { 49 49 if (console.telnetServer != null && issuerOfCurrentCommand != null) 50 NetTelnetServer.WriteToClient_Single (_line, issuerOfCurrentCommand);50 issuerOfCurrentCommand.SendLine (_line); 51 51 else if (ControlPanel.IsStarted ()) 52 52 ControlPanel.AddTextToOutputBuffer (_line); -
binary-improvements/7dtd-server-fixes/src/NetConnections/IConnection.cs
r132 r190 1 1 using System; 2 using UnityEngine; 2 3 3 4 namespace AllocsFixes.NetConnections … … 5 6 public interface IConnection 6 7 { 8 void SendLine (string line); 9 void SendLog (string text, string trace, LogType type); 10 string GetDescription (); 7 11 } 8 12 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/IServer.cs
r132 r190 1 1 using System; 2 using UnityEngine; 2 3 3 4 namespace AllocsFixes.NetConnections … … 7 8 void Disconnect (); 8 9 9 void WriteToClient (string line); 10 11 void WriteToClient_Single (string line, IConnection client); 10 void SendLine (string line); 11 void SendLog (string text, string trace, LogType type); 12 12 } 13 13 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/NetTelnetServer.cs
r182 r190 32 32 public static void Disconnect () 33 33 { 34 Log.Out ("Disconnect console clients!"); 34 35 foreach (IServer s in servers) 35 36 s.Disconnect (); … … 42 43 } 43 44 foreach (IServer s in servers) 44 s. WriteToClient(line);45 s.SendLine (line); 45 46 } 46 47 47 public static void WriteToClient_Single (string line, IConnection client)48 {49 if (line == null) {50 return;51 }52 foreach (IServer s in servers)53 s.WriteToClient_Single (line, client);54 }55 48 } 56 49 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/Telnet.cs
r187 r190 63 63 } 64 64 65 public bool RegisterFailedLogin (int addressHash)66 {67 lock (loginAttemptsPerIP) {68 LoginAttempts la = loginAttemptsPerIP [addressHash];69 return la.LogAttempt ();70 }71 }72 73 65 private void AcceptClient (IAsyncResult asyncResult) 74 66 { … … 80 72 if (endpoint is IPEndPoint) { 81 73 addressHash = ((IPEndPoint)endpoint).Address.GetHashCode (); 82 //Log.Out ("Hash: " + endpointAddressHash);83 74 } else { 75 addressHash = endpoint.GetHashCode (); 84 76 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ()); 85 77 } … … 87 79 lock (loginAttemptsPerIP) { 88 80 LoginAttempts la = null; 89 if (loginAttemptsPerIP.ContainsKey (addressHash))81 if (loginAttemptsPerIP.ContainsKey (addressHash)) 90 82 la = loginAttemptsPerIP [addressHash]; 91 83 if (la == null) { … … 95 87 if (!la.IsBanned ()) { 96 88 TelnetConnection con = new TelnetConnection (this, client, authEnabled); 97 connections.Add (con); 89 lock (connections) { 90 connections.Add (con); 91 } 98 92 } else { 99 93 client.Close (); … … 102 96 } 103 97 listener.BeginAcceptTcpClient (new AsyncCallback (AcceptClient), null); 98 } 99 } 100 101 public bool RegisterFailedLogin (TelnetConnection con) 102 { 103 lock (loginAttemptsPerIP) { 104 LoginAttempts la = loginAttemptsPerIP [con.EndPointHash]; 105 return la.LogAttempt (); 106 } 107 } 108 109 public void ConnectionClosed (TelnetConnection con) 110 { 111 lock (connections) { 112 connections.Remove (con); 104 113 } 105 114 } … … 112 121 listener = null; 113 122 } 114 foreach (TelnetConnection c in connections) { 123 List<TelnetConnection> cur = new List<TelnetConnection> (connections); 124 foreach (TelnetConnection c in cur) { 115 125 c.Close (); 116 126 } … … 120 130 } 121 131 122 private void RemoveClosedConnections () 123 { 124 try { 125 List<TelnetConnection> toRemove = new List<TelnetConnection> (); 126 foreach (TelnetConnection c in connections) { 127 if (c.IsClosed ()) 128 toRemove.Add (c); 129 } 130 foreach (TelnetConnection c in toRemove) { 131 connections.Remove (c); 132 } 133 } catch (Exception e) { 134 Log.Out ("Error in Telnet.RemoveClosedConnections: " + e); 135 } 136 } 137 138 public void WriteToClient (string line) 132 public void SendLine (string line) 139 133 { 140 134 if (line == null) { 141 135 return; 142 136 } 143 RemoveClosedConnections ();144 foreach (TelnetConnection c in connections) {145 if (c.IsAuthenticated ())146 c.WriteLine (line);137 lock (connections) { 138 foreach (TelnetConnection c in connections) { 139 c.SendLine (line); 140 } 147 141 } 148 142 } 149 143 150 public void WriteToClient_Single (string line, IConnection client)144 public void SendLog (string text, string trace, UnityEngine.LogType type) 151 145 { 152 if (line == null) { 153 return; 154 } 155 RemoveClosedConnections (); 156 foreach (TelnetConnection con in connections) { 157 if (con == client) { 158 if (con.IsAuthenticated ()) 159 con.WriteLine (line); 160 } 161 } 146 throw new System.NotImplementedException (); 162 147 } 163 148 -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
r189 r190 12 12 { 13 13 private readonly BlockingQueue<string> toClientQueue = new BlockingQueue<string> (); 14 private readonly Telnet owner;14 private readonly Telnet telnet; 15 15 private readonly Thread receiveThread = null; 16 16 private readonly Thread sendThread = null; 17 17 private bool authenticated = false; 18 18 private readonly bool authEnabled; 19 private TcpClient client; 20 private readonly NetworkStream stream; 19 private readonly TcpClient client; 21 20 private readonly EndPoint endpoint; 22 21 private readonly int endpointAddressHash; 22 private bool closed = false; 23 24 public bool IsClosed { get { return closed; } } 25 26 public bool IsAuthenticated { get { return !authEnabled || authenticated; } } 27 28 public int EndPointHash { get { return endpointAddressHash; } } 23 29 24 30 public TelnetConnection (Telnet owner, TcpClient client, bool authEnabled) 25 31 { 26 this. owner= owner;32 this.telnet = owner; 27 33 this.authEnabled = authEnabled; 28 34 this.client = client; 29 this.stream = client.GetStream ();30 35 this.endpoint = client.Client.RemoteEndPoint; 36 37 if (endpoint is IPEndPoint) { 38 endpointAddressHash = ((IPEndPoint)endpoint).Address.GetHashCode (); 39 } else { 40 endpointAddressHash = endpoint.GetHashCode (); 41 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ()); 42 } 31 43 32 44 Log.Out ("Telnet connection from: " + endpoint); 33 45 34 receiveThread = ThreadMaster.Create ("TelnetClientReceive ", new ThreadStart (ReceiveThread));46 receiveThread = ThreadMaster.Create ("TelnetClientReceive_" + endpoint.ToString (), new ThreadStart (ReceiveThread)); 35 47 receiveThread.Start (); 36 sendThread = ThreadMaster.Create ("TelnetClientSend" , new ThreadStart (SendThread));48 sendThread = ThreadMaster.Create ("TelnetClientSend" + endpoint.ToString (), new ThreadStart (SendThread)); 37 49 sendThread.Start (); 38 50 39 if (endpoint is IPEndPoint) {40 endpointAddressHash = ((IPEndPoint)endpoint).Address.GetHashCode ();41 //Log.Out ("Hash: " + endpointAddressHash);42 } else {43 Log.Out ("EndPoint is not an IPEndPoint but: " + endpoint.GetType ().ToString ());44 }45 46 51 if (authEnabled) { 47 WriteLine ("Please enter password:");52 toClientQueue.Enqueue ("Please enter password:"); 48 53 } else { 49 54 LoginMessage (); … … 53 58 private void LoginMessage () 54 59 { 55 WriteLine ("*** Connected with 7DTD server.");56 WriteLine ("*** Dedicated server only build");57 WriteLine ("*** Allocs server fixes loaded");58 WriteLine (string.Empty);59 WriteLine ("Server IP: " +60 toClientQueue.Enqueue ("*** Connected with 7DTD server."); 61 toClientQueue.Enqueue ("*** Dedicated server only build"); 62 toClientQueue.Enqueue ("*** Allocs server fixes loaded"); 63 toClientQueue.Enqueue (string.Empty); 64 toClientQueue.Enqueue ("Server IP: " + 60 65 ((GamePrefs.GetString (EnumGamePrefs.ServerIP) != null && GamePrefs.GetString (EnumGamePrefs.ServerIP).Length != 0) ? GamePrefs.GetString (EnumGamePrefs.ServerIP) : "Any") 61 66 ); 62 WriteLine ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort));63 WriteLine ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount));64 WriteLine ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode));65 WriteLine ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld));66 WriteLine ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName));67 WriteLine ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty));68 WriteLine (string.Empty);69 WriteLine ("Press 'help' to get a list of all commands. Press 'exit' to end session.");70 WriteLine (string.Empty);67 toClientQueue.Enqueue ("Server port: " + GamePrefs.GetInt (EnumGamePrefs.ServerPort)); 68 toClientQueue.Enqueue ("Max players: " + GamePrefs.GetInt (EnumGamePrefs.ServerMaxPlayerCount)); 69 toClientQueue.Enqueue ("Game mode: " + GamePrefs.GetString (EnumGamePrefs.GameMode)); 70 toClientQueue.Enqueue ("World: " + GamePrefs.GetString (EnumGamePrefs.GameWorld)); 71 toClientQueue.Enqueue ("Game name: " + GamePrefs.GetString (EnumGamePrefs.GameName)); 72 toClientQueue.Enqueue ("Difficulty: " + GamePrefs.GetInt (EnumGamePrefs.GameDifficulty)); 73 toClientQueue.Enqueue (string.Empty); 74 toClientQueue.Enqueue ("Press 'help' to get a list of all commands. Press 'exit' to end session."); 75 toClientQueue.Enqueue (string.Empty); 71 76 } 72 77 … … 74 79 { 75 80 try { 76 StreamReader reader = new StreamReader (stream); 77 try { 78 while (!IsClosed()) { 79 string line = reader.ReadLine (); 80 if (line != null && line.Length > 0) { 81 line = line.Trim (); 82 if (line.Length > 0) { 83 if (!IsAuthenticated ()) { 84 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) { 85 authenticated = true; 86 WriteLine ("Logon successful."); 87 WriteLine (string.Empty); 88 WriteLine (string.Empty); 89 WriteLine (string.Empty); 90 LoginMessage (); 91 } else { 92 if (owner.RegisterFailedLogin (endpointAddressHash)) { 93 WriteLine ("Password incorrect, please enter password:"); 94 } else { 95 WriteLine ("Too many failed login attempts!"); 96 Thread.Sleep (100); 97 Close (); 98 Log.Out ("Telnet connection closed for too many login attempts: " + endpoint); 99 break; 100 } 101 } 81 StreamReader reader = new StreamReader (client.GetStream ()); 82 while (!closed) { 83 string line = reader.ReadLine (); 84 if (line != null && (line = line.Trim ()).Length > 0) { 85 if (!IsAuthenticated) { 86 if (line.Equals (GamePrefs.GetString (EnumGamePrefs.TelnetPassword))) { 87 authenticated = true; 88 toClientQueue.Enqueue ("Logon successful."); 89 toClientQueue.Enqueue (string.Empty); 90 toClientQueue.Enqueue (string.Empty); 91 toClientQueue.Enqueue (string.Empty); 92 LoginMessage (); 93 } else { 94 if (telnet.RegisterFailedLogin (this)) { 95 toClientQueue.Enqueue ("Password incorrect, please enter password:"); 102 96 } else { 103 if (line.ToLower ().Equals ("exit")) { 104 Log.Out ("Telnet connection closed by client: " + endpoint); 105 Close (); 106 break; 107 } 108 Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint); 109 ConsoleOutputSeparator.QueueNetCommand (line, this); 97 toClientQueue.Enqueue ("Too many failed login attempts!"); 98 Thread.Sleep (100); 99 Close (true); 100 break; 110 101 } 111 102 } 103 } else { 104 if (line.ToLower ().Equals ("exit")) { 105 break; 106 } 107 Log.Out ("Telnet executed \"" + line + "\" from: " + endpoint); 108 ConsoleOutputSeparator.QueueNetCommand (line, this); 112 109 } 113 110 } 114 } catch (ObjectDisposedException) {115 } catch (IOException) {116 } catch (ThreadInterruptedException) {117 111 } 118 ThreadMaster.Remove (Thread.CurrentThread.Name); 119 } catch (Exception ex) { 120 Log.Out ("Error in TelnetClientReceive: " + ex); 112 } catch (Exception) { 121 113 } 122 114 115 if (!closed) 116 Close (); 117 ThreadMaster.Remove (Thread.CurrentThread.Name); 123 118 } 124 119 … … 126 121 { 127 122 try { 128 while (!IsClosed() && stream.CanWrite) { 129 try { 130 string line = toClientQueue.Dequeue (); 131 if (!IsClosed () && stream.CanWrite) { 123 while (!closed && client.GetStream ().CanWrite) { 124 string line = toClientQueue.Dequeue (); 125 if (!closed && client.GetStream ().CanWrite) { 126 if (line == null) { 127 client.GetStream ().WriteByte (0); 128 } else { 132 129 byte[] utfData = Encoding.UTF8.GetBytes (line); 133 stream.Write (utfData, 0, utfData.Length);134 stream.WriteByte (13);135 stream.WriteByte (10);130 client.GetStream ().Write (utfData, 0, utfData.Length); 131 client.GetStream ().WriteByte (13); 132 client.GetStream ().WriteByte (10); 136 133 } 137 } catch (IOException) {138 } catch (ThreadInterruptedException) {139 } catch (Exception e) {140 Log.Out ("Error writing to client: " + e);141 134 } 142 135 } 143 144 ThreadMaster.Remove (Thread.CurrentThread.Name); 145 } catch (Exception ex) { 146 Log.Out ("Error in TelnetClientSend: " + ex); 136 } catch (Exception) { 147 137 } 148 138 139 if (!closed) 140 Close (); 141 ThreadMaster.Remove (Thread.CurrentThread.Name); 149 142 } 150 143 151 public void WriteLine (string s)144 public void Close (bool kickedForLogins = false) 152 145 { 153 toClientQueue.Enqueue (s); 146 if (!closed) { 147 closed = true; 148 toClientQueue.Close (); 149 client.GetStream ().Close (); 150 client.Close (); 151 telnet.ConnectionClosed (this); 152 153 if (kickedForLogins) 154 Log.Out ("Telnet connection closed for too many login attempts: " + endpoint); 155 else 156 Log.Out ("Telnet connection closed: " + endpoint); 157 } 154 158 } 155 159 156 public void Close ()160 public void SendLine (string line) 157 161 { 158 if (receiveThread != null) { 159 receiveThread.Interrupt (); 160 } 161 if (sendThread != null) { 162 sendThread.Interrupt (); 163 } 164 if (client != null) 165 client.Close (); 166 client = null; 162 if (!closed && IsAuthenticated) 163 toClientQueue.Enqueue (line); 164 else 165 toClientQueue.Enqueue (null); 167 166 } 168 167 169 public bool IsClosed ()168 public void SendLog (string text, string trace, UnityEngine.LogType type) 170 169 { 171 if (client != null && !client.Connected) { 172 Log.Out ("Telnet connection interrupted: " + endpoint); 173 Close (); 174 } 175 return (client == null) || (!client.Connected); 170 throw new System.NotImplementedException (); 176 171 } 177 172 178 public bool IsAuthenticated()173 public string GetDescription () 179 174 { 180 return !authEnabled || authenticated;175 return "Telnet from " + endpoint; 181 176 } 182 183 177 } 184 178 } -
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/Web.cs
r185 r190 128 128 } 129 129 130 public void WriteToClient(string line)130 public void SendLine (string line) 131 131 { 132 132 try { … … 137 137 } 138 138 139 public void WriteToClient_Single (string line, IConnection client)139 public void SendLog (string text, string trace, UnityEngine.LogType type) 140 140 { 141 try { 142 //Log.Out ("NOT IMPLEMENTED: Web.WriteToClient_Single"); 143 } catch (Exception e) { 144 Log.Out ("Error in Web.WriteToClient_Single: " + e); 145 } 141 throw new System.NotImplementedException (); 146 142 } 147 143
Note:
See TracChangeset
for help on using the changeset viewer.