| 1 |
figdor32 |
2 |
using System; |
| 2 |
|
|
using System.Collections.Generic; |
| 3 |
|
|
using System.Linq; |
| 4 |
|
|
using System.Net; |
| 5 |
|
|
using System.Net.Sockets; |
| 6 |
|
|
using System.Text; |
| 7 |
|
|
using System.Threading.Tasks; |
| 8 |
|
|
|
| 9 |
|
|
namespace TELTest { |
| 10 |
|
|
class Program { |
| 11 |
|
|
|
| 12 |
|
|
static void Main(string[] args) { |
| 13 |
|
|
Environment.CurrentDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); |
| 14 |
|
|
|
| 15 |
|
|
Task.Run(async () => { |
| 16 |
|
|
var ipEndPoint = new IPEndPoint(IPAddress.Any, 1553); |
| 17 |
|
|
|
| 18 |
|
|
TcpListener listener = new TcpListener(ipEndPoint); |
| 19 |
|
|
WriteWithTime("Listening on " + ipEndPoint); |
| 20 |
|
|
try { |
| 21 |
|
|
listener.Start(); |
| 22 |
|
|
while (true) { |
| 23 |
|
|
TcpClient tc = await listener.AcceptTcpClientAsync(); |
| 24 |
|
|
AcceptConnection(tc); |
| 25 |
|
|
} |
| 26 |
|
|
} finally { |
| 27 |
|
|
listener.Stop(); |
| 28 |
|
|
} |
| 29 |
|
|
}).Wait(); |
| 30 |
|
|
} |
| 31 |
|
|
static async void AcceptConnection(TcpClient handler) { |
| 32 |
|
|
var endpoint = handler.Client.RemoteEndPoint; |
| 33 |
|
|
handler.Client.SetSocketKeepAliveValues(2000, 1000); |
| 34 |
|
|
WriteWithTime("Incoming connection from " + endpoint); |
| 35 |
|
|
ConnectionHandler ch = new NormalConnectionHandler(); |
| 36 |
|
|
try { |
| 37 |
|
|
using (handler) { |
| 38 |
|
|
using (NetworkStream stream = handler.GetStream()) { |
| 39 |
|
|
|
| 40 |
|
|
WriteWithTime(ConnectionHandler.AllHandlers.Count + " concurrent connections right now."); |
| 41 |
|
|
if (Config.MaxConnections < ConnectionHandler.AllHandlers.Count) { |
| 42 |
|
|
await ch.WriteASCII(stream, "Sorry, the server is full (" + (ConnectionHandler.AllHandlers.Count - 1) + "/" + Config.MaxConnections + ")"); |
| 43 |
|
|
WriteWithTime(handler.Client.RemoteEndPoint + " exceeds server capacity, disconnecting."); |
| 44 |
|
|
} else { |
| 45 |
|
|
await ch.Handle(stream); |
| 46 |
|
|
} |
| 47 |
|
|
WriteWithTime(handler.Client.RemoteEndPoint + " disconnected."); |
| 48 |
|
|
} |
| 49 |
|
|
} |
| 50 |
|
|
} catch (System.IO.IOException x) { |
| 51 |
|
|
ConnectionHandler.AllHandlers.Remove(ch); |
| 52 |
|
|
WriteWithTime("Endpoint " + endpoint + " unexpectedly disconnected. ({0})", x.Message + ", " + x.InnerException); |
| 53 |
|
|
} catch (ArgumentOutOfRangeException x) { |
| 54 |
|
|
ConnectionHandler.AllHandlers.Remove(ch); |
| 55 |
|
|
WriteWithTime("Endpoint " + endpoint + " unexpectedly disconnected. ({0})", x.Message + ", " + x.InnerException); |
| 56 |
|
|
} catch (Exception x) { |
| 57 |
|
|
ConnectionHandler.AllHandlers.Remove(ch); |
| 58 |
|
|
var lc = Console.ForegroundColor; |
| 59 |
|
|
Console.ForegroundColor = ConsoleColor.Red; |
| 60 |
|
|
WriteWithTime("GENERAL EXCEPTION:\r\n{0}", x); |
| 61 |
|
|
Console.ForegroundColor = lc; |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
} |
| 65 |
|
|
public static void WriteWithTime(string format, params object[] arg) { |
| 66 |
|
|
Console.WriteLine("[" + DateTime.Now + "] " + format, arg); |
| 67 |
|
|
} |
| 68 |
|
|
} |
| 69 |
|
|
} |