示例#1
0
 private void Connection(IAsyncResult ar)
 {
     try
     {
         TcpClient _client = this.listener.EndAcceptTcpClient(ar);
         this.listener.BeginAcceptTcpClient(Connection, this.listener);
         XyzClient client = new XyzClient(_client);
         this.on_Connect?.Invoke(client);
     }
     catch (Exception)
     {
     }
 }
        public void Listen()
        {
            server           = new XyzServer(listenPort);
            server.OnConnect = (XyzClient client) => {
                this.on_Connect?.Invoke(client, Source.Client);

                // backend
                XyzClient backend = new XyzClient();
                backend.OnConnect = () => {
                    this.on_Connect?.Invoke(backend, Source.Backend);
                };
                backend.OnDisconnect = () =>
                {
                    client.Disconnect();
                    this.on_Disconnect?.Invoke(backend, Source.Backend);
                };
                backend.OnMessage = (byte[] bytes, int type) =>
                {
                    client.Send(bytes, type);
                    this.on_Message?.Invoke(backend, bytes, type, Source.Backend);
                };
                backend.Connect(forwardHost, forwardPort);

                // frontend
                client.OnDisconnect = () =>
                {
                    backend.Disconnect();
                    this.on_Disconnect?.Invoke(client, Source.Client);
                };

                client.OnMessage = (byte[] bytes, int type) =>
                {
                    backend.Send(bytes, type);
                    this.on_Message?.Invoke(client, bytes, type, Source.Client);
                };

                client.Read();
            };
        }