source: binary-improvements/7dtd-server-fixes/src/AllocsTelnetConnection.cs@ 83

Last change on this file since 83 was 75, checked in by alloc, 10 years ago

fixes

File size: 1.6 KB
Line 
1using System;
2using System.Net;
3using System.Net.Sockets;
4
5public class AllocsTelnetConnection
6{
7 private bool authenticated = false;
8 private bool authEnabled;
9 private TcpClient client;
10 private NetworkStream stream;
11 private string lineBuffer = string.Empty;
12 private EndPoint endpoint;
13
14 public AllocsTelnetConnection (TcpClient client, bool authEnabled)
15 {
16 this.authEnabled = authEnabled;
17 this.client = client;
18 this.stream = client.GetStream ();
19 this.endpoint = client.Client.RemoteEndPoint;
20 }
21
22 public EndPoint GetEndPoint ()
23 {
24 return endpoint;
25 }
26
27 private void WriteByte (byte v)
28 {
29 if (!IsClosed () && stream.CanWrite) {
30 stream.WriteByte (v);
31 }
32 }
33
34 public void WriteLine (string s)
35 {
36 if (!IsClosed () && stream.CanWrite) {
37 for (int i = 0; i < s.Length; i++) {
38 WriteByte ((byte)s [i]);
39 }
40 WriteByte (10);
41 }
42 }
43
44 public void Close ()
45 {
46 if (client != null)
47 client.Close ();
48 client = null;
49 }
50
51 public bool IsClosed ()
52 {
53 if (client != null && !client.Connected) {
54 Log.Out ("Telnet connection interrupted: " + endpoint);
55 }
56 return (client == null) || (!client.Connected);
57 }
58
59 public bool IsAuthenticated ()
60 {
61 return !authEnabled || authenticated;
62 }
63
64 public void SetAuthenticated ()
65 {
66 authenticated = true;
67 }
68
69 public bool Read ()
70 {
71 while (!IsClosed() && stream.CanRead && stream.DataAvailable) {
72 int b = stream.ReadByte ();
73 if (b == '\r' || b == '\n') {
74 if (lineBuffer.Length > 0)
75 return true;
76 } else {
77 lineBuffer += (char)b;
78 }
79 }
80 return false;
81 }
82
83 public string GetLine ()
84 {
85 string res = lineBuffer;
86 lineBuffer = string.Empty;
87 return res;
88 }
89}
Note: See TracBrowser for help on using the repository browser.