Example #1
0
 public void Start()
 {
     running = true;
     task    = new StoppedTask();
     listener.Start();
     Task.Run(new Action(Background));
 }
Example #2
0
        private void Background()
        {
            var stopTask = task.Task;

            while (running)
            {
                var clientTask = listener.AcceptTcpClientAsync();
                if (Task.WaitAny(stopTask, clientTask) == 0)
                {
                    task = null;
                    return;
                }
                var client = clientTask.Result;
                Task.Run(() =>
                {
                    try
                    {
                        var stream = client.GetStream();
                        var sel    = new PacketSelector(stream);
                        sel.Catch();
                        if (sel.ID != 0)
                        {
                            return;
                        }
                        var endPoint  = (IPEndPoint)client.Client.RemoteEndPoint;
                        var handshake = new HandshakePacket(sel);
                        ServerShadow server;
                        if (servers.ContainsKey(handshake.Address))
                        {
                            server = servers[handshake.Address];
                        }
                        else
                        {
                            server = standard;
                        }
                        server.NewConnetcion(handshake, endPoint, stopTask);
                        stream.Close();
                    }
                    catch (Exception exception)
                    {
                        Log.WriteWarn(exception.Message);
                    }
                    finally
                    {
                        client.Close();
                    }
                });
            }
        }