public async Task Run(Settings settings) { Settings = settings; var local = settings.Local; var listener = new TcpListener(local.Listen, local.Port); listener.Start(); var pairIdSeed = 0; while (true) { var client = await listener.AcceptSocketAsync(); var pairId = Interlocked.Increment(ref pairIdSeed); _ = Task.Run(async() => { var command = Negotiate(client); if (command != null) { var cmd = command.Value; await SendMessageAsync($"+E {pairId} {cmd.act} {cmd.addr}"); EndPointStation.AddEndPoint(pairId, client); } }); } }
protected override async Task HandleReceivedMessageAsync(string message) { if (message.StartsWith("+E ")) { var parts = message.Split(' '); var pairId = Int32.Parse(parts[1]); var act = Byte.Parse(parts[2]); var addr = parts[3]; var sep = addr.LastIndexOf(':'); var host = addr.Substring(0, sep); var port = Int32.Parse(addr.Substring(sep + 1)); var socket = new Socket(SocketType.Stream, ProtocolType.Tcp); switch (act) { case 1: // Connect await socket.ConnectAsync(host, port); break; case 2: // Bind case 3: // Udp Associate throw new NotImplementedException(); default: break; } EndPointStation.AddEndPoint(pairId, socket); } }