Changeset 132 for binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
- Timestamp:
- Aug 26, 2014, 7:29:11 PM (10 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src/NetConnections
- Files:
-
- 3 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs
r130 r132 3 3 using System.Net.Sockets; 4 4 5 namespace AllocsFixes 5 namespace AllocsFixes.NetConnections.Servers.Telnet 6 6 { 7 public class AllocsTelnetConnection8 {9 private bool authenticated = false;10 private bool authEnabled;11 private TcpClient client;12 private NetworkStream stream;13 private string lineBuffer = string.Empty;14 private EndPoint endpoint;7 public class TelnetConnection : IConnection 8 { 9 private bool authenticated = false; 10 private bool authEnabled; 11 private TcpClient client; 12 private NetworkStream stream; 13 private string lineBuffer = string.Empty; 14 private EndPoint endpoint; 15 15 16 public AllocsTelnetConnection (TcpClient client, bool authEnabled)17 {18 this.authEnabled = authEnabled;19 this.client = client;20 this.stream = client.GetStream ();21 this.endpoint = client.Client.RemoteEndPoint;22 }16 public TelnetConnection (TcpClient client, bool authEnabled) 17 { 18 this.authEnabled = authEnabled; 19 this.client = client; 20 this.stream = client.GetStream (); 21 this.endpoint = client.Client.RemoteEndPoint; 22 } 23 23 24 public EndPoint GetEndPoint ()25 {26 return endpoint;27 }24 public EndPoint GetEndPoint () 25 { 26 return endpoint; 27 } 28 28 29 private void WriteByte (byte v) 30 { 31 if (!IsClosed () && stream.CanWrite) { 32 stream.WriteByte (v); 33 } 34 } 35 36 public void WriteLine (string s) 37 { 38 try { 29 private void WriteByte (byte v) 30 { 39 31 if (!IsClosed () && stream.CanWrite) { 40 for (int i = 0; i < s.Length; i++) { 41 WriteByte ((byte)s [i]); 42 } 43 WriteByte(13); 44 WriteByte (10); 45 } 46 } catch (Exception e) { 47 Log.Out("Error writing to client: " + e); 48 } 49 } 50 51 public void Close () 52 { 53 if (client != null) 54 client.Close (); 55 client = null; 56 } 57 58 public bool IsClosed () 59 { 60 if (client != null && !client.Connected) { 61 Log.Out ("Telnet connection interrupted: " + endpoint); 62 } 63 return (client == null) || (!client.Connected); 64 } 65 66 public bool IsAuthenticated () 67 { 68 return !authEnabled || authenticated; 69 } 70 71 public void SetAuthenticated () 72 { 73 authenticated = true; 74 } 75 76 public bool Read () 77 { 78 while (!IsClosed() && stream.CanRead && stream.DataAvailable) { 79 int b = stream.ReadByte (); 80 if (b == '\r' || b == '\n') { 81 if (lineBuffer.Length > 0) 82 return true; 83 } else { 84 lineBuffer += (char)b; 32 stream.WriteByte (v); 85 33 } 86 34 } 87 return false;88 }89 35 90 public string GetLine () 91 { 92 string res = lineBuffer; 93 lineBuffer = string.Empty; 94 return res; 36 public void WriteLine (string s) 37 { 38 try { 39 if (!IsClosed () && stream.CanWrite) { 40 for (int i = 0; i < s.Length; i++) { 41 WriteByte ((byte)s [i]); 42 } 43 WriteByte (13); 44 WriteByte (10); 45 } 46 } catch (Exception e) { 47 Log.Out ("Error writing to client: " + e); 48 } 49 } 50 51 public void Close () 52 { 53 if (client != null) 54 client.Close (); 55 client = null; 56 } 57 58 public bool IsClosed () 59 { 60 if (client != null && !client.Connected) { 61 Log.Out ("Telnet connection interrupted: " + endpoint); 62 } 63 return (client == null) || (!client.Connected); 64 } 65 66 public bool IsAuthenticated () 67 { 68 return !authEnabled || authenticated; 69 } 70 71 public void SetAuthenticated () 72 { 73 authenticated = true; 74 } 75 76 public bool Read () 77 { 78 while (!IsClosed() && stream.CanRead && stream.DataAvailable) { 79 int b = stream.ReadByte (); 80 if (b == '\r' || b == '\n') { 81 if (lineBuffer.Length > 0) 82 return true; 83 } else { 84 lineBuffer += (char)b; 85 } 86 } 87 return false; 88 } 89 90 public string GetLine () 91 { 92 string res = lineBuffer; 93 lineBuffer = string.Empty; 94 return res; 95 } 96 95 97 } 96 98 } 97 }
Note:
See TracChangeset
for help on using the changeset viewer.