示例#1
0
 static void Setup(int port)
 {
     ServerThread = new Thread(() =>
     {
         Server = new TcpListener(IPAddress.Parse("127.0.0.1"), port);
         Server.Start();
         Console.WriteLine("  Server started.");
         Console.WriteLine("  Listening " + port.ToString());
         while (true)
         {
             var client             = Server.AcceptTcpClient();
             var tcpBridgeClient    = new TCPBridgeClient();
             tcpBridgeClient.Client = client;
             Clients.Add(tcpBridgeClient);
             tcpBridgeClient.HandleThread = new Thread(() =>
             {
                 HandleClient(tcpBridgeClient);
             });
             tcpBridgeClient.HandleThread.Start();
         }
     });
     ServerThread.Start();
 }
示例#2
0
        static void HandleClient(TCPBridgeClient client)
        {
            Console.WriteLine("  Client connected. ip=" + client.Client.Client.RemoteEndPoint.ToString());
            client.Bridge = new TcpClient();
            client.Bridge.Connect(ServerHost, ServerPort);
            var bridgeStream = client.Bridge.GetStream();

            bridgeStream.Write(Int32ToBytes(0), 0, 4);
            var buffer = new byte[4];

            ReadData(bridgeStream, buffer, 0, 4);
            var id = BytesToInt32(buffer);

            client.ID = id;
            Console.WriteLine("  Client ID=" + id.ToString());
            var clientStream = client.Client.GetStream();

            client.ReceiveBuffer = new byte[BufferSize];
            client.SendBuffer    = new byte[BufferSize];
            Action receive = () =>
            {
                try
                {
                    // resend the last package
                    if (client.ReceiveSize != 0)
                    {
                        //Send the size of the data
                        bridgeStream.Write(Int32ToBytes(client.ReceiveSize), 0, 4);
                        //send data to server
                        bridgeStream.Write(client.ReceiveBuffer, 0, client.ReceiveSize);
                    }
                    while (true)
                    {
                        if (client.Reset)
                        {
                            return;
                        }
                        //reset
                        client.ReceiveBuffer = new byte[BufferSize];
                        client.ReceiveSize   = 0;
                        //receive from client
                        client.ReceiveSize = clientStream.Read(client.ReceiveBuffer, 0, BufferSize);
                        //Send the size of the data
                        bridgeStream.Write(Int32ToBytes(client.ReceiveSize), 0, 4);
                        //send data to server
                        bridgeStream.Write(client.ReceiveBuffer, 0, client.ReceiveSize);

                        //reset
                        client.ReceiveBuffer = new byte[BufferSize];
                        client.ReceiveSize   = 0;
                    }
                }
                catch (Exception ex)
                {
                    client.Reset = true;
                }
            };
            Action send = () =>
            {
                try
                {
                    while (true)
                    {
                        if (client.Reset)
                        {
                            return;
                        }
                        var sizeBuffer = new byte[4];
                        //reset
                        client.SendBuffer = new byte[BufferSize];
                        client.SendSize   = 0;
                        //get the size of data
                        ReadData(bridgeStream, sizeBuffer, 0, 4);
                        client.SendSize = BytesToInt32(sizeBuffer);
                        //get data
                        ReadData(bridgeStream, client.SendBuffer, 0, client.SendSize);
                        //send the data to client
                        clientStream.Write(client.SendBuffer, 0, client.SendSize);

                        //reset
                        client.SendBuffer = new byte[BufferSize];
                        client.SendSize   = 0;
                    }
                }
                catch (Exception ex)
                {
                    client.Reset = true;
                }
            };

            while (true)
            {
                client.Reset         = false;
                client.ReceiveThread = new Thread(new ThreadStart(receive));
                client.SendThread    = new Thread(new ThreadStart(send));
                client.ReceiveThread.Start();
                client.SendThread.Start();
                while (!client.Reset)
                {
                    Thread.Sleep(500);
                }
TryAgain:
                client.Bridge.Close();
                client.Bridge = new TcpClient();
                client.Bridge.Connect(ServerHost, ServerPort);
                bridgeStream = client.Bridge.GetStream();
                bridgeStream.Write(Int32ToBytes(client.ID), 0, 4);
                buffer = new byte[4];
                ReadData(bridgeStream, buffer, 0, 4);
                if (BytesToInt32(buffer) != 0)
                {
                    goto TryAgain;
                }
            }
        }