Пример #1
0
        public override int Read(byte[] buffer, int offset, int count)
        {
            int readlen = count > (int)(_bytestream.Length - _position) ? (int)(_bytestream.Length - _position) : count;

            if (readlen > 0)
            {
                ArraySegment <byte> buff = _bytestream.GetIOBuffer();
                System.Buffer.BlockCopy(buff.Array
                                        , (int)_position + buff.Offset
                                        , buffer, offset
                                        , readlen);
                _position += readlen;
                return(readlen);
            }
            else
            {
                return(0);
            }
        }
Пример #2
0
        public override void ChannelRead(IContext context, object msg)
        {
            IByteStream input = msg as IByteStream;

            if (input != null)
            {
                int endOffset = input.WriterIndex;

                // 使用该信息计算当前SSL记录的长度。
                if (this._packetLength > 0)
                {
                    if (endOffset < this._packetLength)
                    {
                        //数据未完整
                        context.Channel.MergeRead();
                        return;
                    }
                    else
                    {
                        this._packetLength = 0;
                    }
                }

                //判断是否为SSL加密包
                if (endOffset < SSLUtils.SSL_RECORD_HEADER_LENGTH)
                {
                    return;
                }

                int encryptedPacketLength = SSLUtils.GetEncryptedPacketLength(input, 0);
                if (encryptedPacketLength == -1)
                {
                    return;
                }

                if (encryptedPacketLength > endOffset)
                {
                    // 数据未完整
                    this._packetLength = encryptedPacketLength;
                    context.Channel.MergeRead();
                    return;
                }

                ArraySegment <byte> inputIoBuffer = input.GetIOBuffer();
                _agent.SetSource(inputIoBuffer.Array, inputIoBuffer.Offset, input.Length);
                if ((_agent.State & SSLHandlerState.Authenticating) != 0)
                {
                    _agent.TriggerHandshakeRead();
                }
                else
                {
                    if (EnsureAuthenticated())
                    {
                        IByteStream output = this.Unwrap(context, input);
                        context.FireNextRead(new SSLUnwrapStream(output));
                    }
                }


                return;
            }

            context.FireNextRead(msg);
        }
Пример #3
0
 public ArraySegment <byte> GetIOBuffer()
 {
     return(_stream.GetIOBuffer());
 }