Пример #1
0
        public static void ToPipeStream(Stream source, int length, PipeStream dest)
        {
            int rlen = 0;

            while (true)
            {
                IBuffer buffer    = BufferPoolGroup.DefaultGroup.Next().Pop();
                int     offset    = 0;
                int     size      = buffer.Size;
                int     bufferlen = 0;
NEXT:
                if (length > size)
                {
                    rlen = source.Read(buffer.Data, offset, size);
                }
                else
                {
                    rlen = source.Read(buffer.Data, offset, length);
                }
                if (rlen == 0)
                {
                    if (bufferlen > 0)
                    {
                        buffer.SetLength(bufferlen);
                        dest.Import(buffer);
                    }
                    else
                    {
                        buffer.Free();
                    }
                    return;
                }
                bufferlen += rlen;
                offset    += rlen;
                size      -= rlen;
                length    -= rlen;
                if (length == 0)
                {
                    buffer.SetLength(bufferlen);
                    dest.Import(buffer);
                    return;
                }
                else
                {
                    if (size > 0)
                    {
                        goto NEXT;
                    }
                    else
                    {
                        buffer.SetLength(bufferlen);
                        dest.Import(buffer);
                    }
                }
            }
        }
Пример #2
0
        private System.IO.Stream GetReader()
        {
            bool isnew;

            Connect(out isnew);
            IBuffer buffer = this.BufferPool.Pop();

            try
            {
                ((Buffers.Buffer)buffer).From(Socket);
                if (buffer.Length == 0)
                {
                    throw new SocketException((int)SocketError.Shutdown);
                }
                buffer.Postion = 0;
                System.Threading.Interlocked.Add(ref mReceiveBytes, buffer.Length);
                System.Threading.Interlocked.Increment(ref mReceiveQuantity);
                mBaseNetworkStream.Import(buffer);
                return(Stream);
            }
            catch (SocketException e_)
            {
                buffer.Free();
                DisConnect();
                throw e_;
            }
        }
Пример #3
0
        private void FlushProxyStream(bool end)
        {
            IBuffer buffer = mProxyStream.GetWriteCacheBufers();

            if (buffer != null)
            {
                if (Request.Session.SSL)
                {
                    mProxyStream.Import(buffer);
                    var length = (int)mProxyStream.Length;
                    var data   = System.Buffers.ArrayPool <byte> .Shared.Rent(length);

                    mProxyStream.Read(data, 0, length);
                    ProxyDataBuffer proxyData = new ProxyDataBuffer(new ArraySegment <byte>(data, 0, length));
                    Request.Session.Send(proxyData);
                }
                else
                {
                    Request.Session.Send(buffer);
                }
            }
            if (end)
            {
                mProxyStream.Dispose();
            }
        }
Пример #4
0
 private void OnReceive(IBuffer buffer)
 {
     mBaseNetworkStream.Import(buffer);
     if (Packet != null)
     {
         try
         {
             Packet.Decode(this, this.Stream);
         }
         catch (Exception e_)
         {
             ProcessError(e_, "client packet decoding error!");
         }
     }
     else
     {
         try
         {
             mReceiveArgs.Stream = this.Stream;
             if (awaitPipeStream.Pending)
             {
                 awaitPipeStream.Success(this.Stream.ToPipeStream());
             }
             else
             {
                 DataReceive?.Invoke(this, mReceiveArgs);
             }
         }
         catch (Exception e_)
         {
             ProcessError(e_, "client  buffer process error!");
         }
     }
 }
Пример #5
0
 private void OnReceive(IBuffer buffer)
 {
     mBaseNetworkStream.Import(buffer);
     if (Packet != null)
     {
         try
         {
             Packet.Decode(this, this.Stream);
         }
         catch (Exception e_)
         {
             ProcessError(e_, "client  buffer decoding error!");
         }
     }
     else
     {
         try
         {
             mReceiveArgs.Stream = this.Stream;
             Receive?.Invoke(this, mReceiveArgs);
         }
         catch (Exception e_)
         {
             ProcessError(e_, "client  buffer process error!");
         }
     }
 }
Пример #6
0
 private void ImportReceive(IBuffer buffer)
 {
     mBaseNetworkStream.Import(buffer);
     if (!SSL)
     {
         OnReceive();
     }
 }
Пример #7
0
 private void OnReceive(IBuffer buffer)
 {
     mBaseNetworkStream.Import(buffer);
     if (SSL && mSslStream.SyncDataError != null)
     {
         ProcessError(mSslStream.SyncDataError, $"sync SslStream data error {mSslStream.SyncDataError.Message}");
         DisConnect();
         return;
     }
     if (Packet != null)
     {
         try
         {
             Packet.Decode(this, this.Stream);
         }
         catch (Exception e_)
         {
             ProcessError(e_, "client packet decoding error!");
         }
     }
     else
     {
         try
         {
             mReceiveArgs.Stream = this.Stream;
             if (awaitPipeStream.Pending)
             {
                 awaitPipeStream.Success(this.Stream.ToPipeStream());
             }
             else
             {
                 DataReceive?.Invoke(this, mReceiveArgs);
             }
         }
         catch (Exception e_)
         {
             ProcessError(e_, "client  buffer process error!");
         }
     }
 }