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

Last change on this file since 182 was 182, checked in by alloc, 11 years ago

fixes

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Net;
4using System.Net.Sockets;
5using System.Threading;
6
7namespace AllocsFixes.NetConnections.Servers.Telnet
8{
9 public class Telnet : IServer
10 {
11 private Thread telnetThread = null;
12 private TcpListener listener = null;
13 private bool closed = false;
14 private bool authEnabled = false;
15 private List<TelnetConnection> connections = new List<TelnetConnection> ();
16
17 public Telnet ()
18 {
19 try {
20 if (!GamePrefs.GetBool (EnumGamePrefs.TelnetEnabled)) {
21 return;
22 }
23
24 int port = GamePrefs.GetInt (EnumGamePrefs.TelnetPort);
25
26 authEnabled = GamePrefs.GetString (EnumGamePrefs.TelnetPassword).Length != 0;
27 if (authEnabled)
28 listener = new TcpListener (IPAddress.Any, port);
29 else
30 listener = new TcpListener (IPAddress.Loopback, port);
31 telnetThread = ThreadMaster.Create ("thread TelnetListenThread", new ThreadStart (telnetListenThread));
32 telnetThread.Start ();
33
34 NetTelnetServer.RegisterServer (this);
35
36 Log.Out ("Started Telnet on " + port);
37 } catch (Exception e) {
38 Log.Out ("Error in Telnet.ctor: " + e);
39 }
40 }
41
42 private void telnetListenThread ()
43 {
44 try {
45 listener.Start ();
46 try {
47 while (!closed) {
48 TelnetConnection c = new TelnetConnection (listener.AcceptTcpClient (), authEnabled);
49 connections.Add (c);
50 }
51 } catch (SocketException) {
52 } catch (ThreadInterruptedException) {
53 }
54 ThreadMaster.Remove (Thread.CurrentThread.Name);
55 } catch (Exception ex) {
56 Log.Out ("Error in TelnetListenThread: " + ex);
57 }
58 }
59
60 public void Disconnect ()
61 {
62 try {
63 closed = true;
64 if (listener != null) {
65 listener.Stop ();
66 listener = null;
67 }
68 foreach (TelnetConnection c in connections) {
69 c.Close ();
70 }
71 Thread.Sleep (100);
72 if (telnetThread != null) {
73 telnetThread.Interrupt ();
74 }
75 } catch (Exception e) {
76 Log.Out ("Error in Telnet.Disconnect: " + e);
77 }
78 }
79
80 private void RemoveClosedConnections ()
81 {
82 try {
83 List<TelnetConnection> toRemove = new List<TelnetConnection> ();
84 foreach (TelnetConnection c in connections) {
85 if (c.IsClosed ())
86 toRemove.Add (c);
87 }
88 foreach (TelnetConnection c in toRemove) {
89 connections.Remove (c);
90 }
91 } catch (Exception e) {
92 Log.Out ("Error in Telnet.RemoveClosedConnections: " + e);
93 }
94 }
95
96 public void WriteToClient (string line)
97 {
98 if (line == null) {
99 return;
100 }
101 RemoveClosedConnections ();
102 foreach (TelnetConnection c in connections) {
103 if (c.IsAuthenticated ())
104 c.WriteLine (line);
105 }
106 }
107
108 public void WriteToClient_Single (string line, IConnection client)
109 {
110 if (line == null) {
111 return;
112 }
113 RemoveClosedConnections ();
114 foreach (TelnetConnection con in connections) {
115 if (con == client) {
116 if (con.IsAuthenticated ())
117 con.WriteLine (line);
118 }
119 }
120 }
121
122 }
123}
124
Note: See TracBrowser for help on using the repository browser.