Exemplo n.º 1
0
        public void Close()
        {
            if (!this.IsAlive)
            {
                return;
            }

            this.IsAlive = false;

            mSocket.Shutdown(SocketShutdown.Both);
            mSocket.Close();

            if (mSendCipher != null)
            {
                mSendCipher.Dispose();
            }

            if (mRecvCipher != null)
            {
                mRecvCipher.Dispose();
            }

            mBuffer     = null;
            mSendCipher = null;
            mRecvCipher = null;

            this.Terminate();
        }
Exemplo n.º 2
0
        public Session(Socket socket)
        {
            mSocket                   = socket;
            mSocket.NoDelay           = true;
            mSocket.SendBufferSize    = 0xFFFF;
            mSocket.ReceiveBufferSize = 0xFFFF;
            mNetworkStream            = new NetworkStream(mSocket);
            mLocker                   = new object();

            this.Host    = (mSocket.RemoteEndPoint as IPEndPoint).Address.ToString();
            this.IsAlive = true;

            mSendCipher = new MapleCryptograph(Constants.Version, Constants.SIV, TransformDirection.Encrypt);
            mRecvCipher = new MapleCryptograph(Constants.Version, Constants.RIV, TransformDirection.Decrypt);

            this.Receive();
        }
Exemplo n.º 3
0
        /// <summary>
        /// 持续接收数据
        /// </summary>
        public async void ReceiveData()
        {
            byte[] buffer;

            while (IsAlive)
            {
                if (!_networkStream.DataAvailable)
                {
                    continue;
                }

                try
                {
                    // 读取封包头
                    var length = _encryptor.IvLength;
                    buffer = new byte[length];
                    if (await _networkStream.ReadAsync(buffer, 0, length) == length)
                    {
                        length = MapleCryptograph.GetPacketLength(buffer);
                    }

                    // 通过收取到的封包头计算数据长度,如果比缓冲区还长则停止会话
                    // 如果检测出封包头不满足约束条件,则停止会话
                    if (length > _client.ReceiveBufferSize || !_decryptor.IsValidHeader(buffer))
                    {
                        Dispose();
                        return;
                    }

                    // 读取真正内容
                    buffer = new byte[length];
                    if (await _networkStream.ReadAsync(buffer, 0, length) == length)
                    {
                        _decryptor.Transform(buffer);
                        OnDataReceived(this, new DataReceivedEventArgs {
                            ReceivedData = buffer
                        });
                    }
                }
                catch (Exception e)
                {
                    Debug.WriteLine($"会话:读取数据出错,错误信息 {e.Message}");
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 会话构造方法
        /// </summary>
        /// <param name="client">网络接口</param>
        public Client(TcpClient client)
        {
            _client                   = client;
            _client.NoDelay           = true;
            _client.SendBufferSize    = 0xFFFF;
            _client.ReceiveBufferSize = 0xFFFF;
            _networkStream            = _client.GetStream();
            ClientIp                  = ((IPEndPoint)_client.Client.RemoteEndPoint).Address.ToString();
            IsAlive                   = true;

            // 生成用于发送和接收的初始化变量
            using (var rng = new RNGCryptoServiceProvider())
            {
                rng.GetNonZeroBytes(SendIv);
                rng.GetNonZeroBytes(ReceiveIv);
            }

            unchecked
            {
                _encryptor = new MapleCryptograph(SendIv, MapleCryptograph.TransformDirection.Encrypt);
                _decryptor = new MapleCryptograph(ReceiveIv, MapleCryptograph.TransformDirection.Decrypt);
            }
        }
Exemplo n.º 5
0
        private async void Receive()
        {
            if (!mNetworkStream.CanRead)
            {
                this.Close();
                return;
            }

            while (IsAlive)
            {
                if (!mNetworkStream.DataAvailable)
                {
                    continue;
                }

                var length = 4;
                mBuffer = new byte[length];
                if (await mNetworkStream.ReadAsync(mBuffer, 0, length) == length)
                {
                    length = MapleCryptograph.GetPacketLength(mBuffer);
                }

                if (length > mSocket.ReceiveBufferSize || !mRecvCipher.CheckServerPacket(mBuffer, 0))
                {
                    this.Close();
                    return;
                }

                mBuffer = new byte[length];
                if (await mNetworkStream.ReadAsync(mBuffer, 0, length) == length)
                {
                    mRecvCipher.Transform(mBuffer);
                    Dispatch(mBuffer);
                }
            }
        }