source: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Telnet/TelnetConnection.cs@ 139

Last change on this file since 139 was 132, checked in by alloc, 10 years ago

Fixes: Source cleanups

File size: 1.9 KB
RevLine 
[75]1using System;
2using System.Net;
3using System.Net.Sockets;
4
[132]5namespace AllocsFixes.NetConnections.Servers.Telnet
[130]6{
[132]7 public class TelnetConnection : IConnection
[75]8 {
[132]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;
[75]15
[132]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 }
[75]23
[132]24 public EndPoint GetEndPoint ()
25 {
26 return endpoint;
[75]27 }
28
[132]29 private void WriteByte (byte v)
30 {
[91]31 if (!IsClosed () && stream.CanWrite) {
[132]32 stream.WriteByte (v);
33 }
34 }
35
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);
[91]45 }
[132]46 } catch (Exception e) {
47 Log.Out ("Error writing to client: " + e);
[75]48 }
49 }
50
[132]51 public void Close ()
52 {
53 if (client != null)
54 client.Close ();
55 client = null;
56 }
[75]57
[132]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);
[75]64 }
65
[132]66 public bool IsAuthenticated ()
67 {
68 return !authEnabled || authenticated;
69 }
[75]70
[132]71 public void SetAuthenticated ()
72 {
73 authenticated = true;
74 }
[75]75
[132]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 }
[75]86 }
[132]87 return false;
[75]88 }
89
[132]90 public string GetLine ()
91 {
92 string res = lineBuffer;
93 lineBuffer = string.Empty;
94 return res;
95 }
96
[75]97 }
98}
Note: See TracBrowser for help on using the repository browser.