Esempio n. 1
0
        private void RecvThread()
        {
            try
            {
                if (ReceiveBytes(1)[0] != 4)
                {
                    Shutdown();
                    return;
                }
            }
            catch (Exception)
            {
                Shutdown();
                return;
            }

            if (ReceiveBytes(1)[0] != 1)
            {
                throw new Exception();
            }
            var p1   = ReceiveBytes(1)[0];
            var p2   = ReceiveBytes(1)[0];
            var port = BitConverter.ToUInt16(new[] { p2, p1 }, 0);
            var ip   = BitConverter.ToUInt32(ReceiveBytes(4), 0);

            while (ReceiveBytes(1)[0] != 0)
            {
            }

            var tcpClient = new TcpClient();

            try
            {
                tcpClient.Connect(new IPAddress(ip), port);
                _port = port;
            }
            catch (Exception)
            {
                _recvSocket.Send(new byte[] { 0, 91, 0, 0, 0, 0, 0, 0 });
                Shutdown();
                return;
            }

            try
            {
                _recvSocket.Send(new byte[] { 0, 90, 0, 0, 0, 0, 0, 0 });
                _sendSocket = tcpClient.Client;
                _sendThread = new Thread(SendThread);
                _sendThread.Start();

                while (true)
                {
                    var stream = new byte[15000];
                    var a      = _sendSocket.Receive(stream);
                    var buff   = new byte[a];
                    Buffer.BlockCopy(stream, 0, buff, 0, a);
                    _recvSocket.Send(buff, a, SocketFlags.None);
                    _logWriter.WritePacket(buff, PacketOrigin.ServerToClient, _port);

                    if (a == 0)
                    {
                        break;
                    }
                }

                _recvSocket.Shutdown(SocketShutdown.Receive);
                _sendSocket.Shutdown(SocketShutdown.Send);
                Shutdown(false);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return;
            }
        }