private void OnBeginWrite(IAsyncResult result)
 {
     Server.RegisterThread();
     try
     {
         _Destination.EndWrite(result);
         _Source.BeginRead(_Buffer.Memory, 0, _Buffer.Memory.Length, OnBeginRead, null);
     }
     catch (Exception ex)
     {
         OnError(ex);
     }
 }
Example #2
0
        private void OnBytesRead(IAsyncResult result)
        {
            Server.RegisterThread();
            try
            {
                var readBytes     = _Source.EndRead(result);
                var currentBuffer = (Buffer)result.AsyncState;

                currentBuffer.BytesWritten = readBytes;

                var oldWriteBuffer = Interlocked.CompareExchange(ref _WriteBuffer, currentBuffer, null);

                if (oldWriteBuffer == null)
                {
                    //write buffer was empty, meaning we are no longer writing

                    if (readBytes > 0)
                    {
                        var buf = Interlocked.Exchange(ref _NextReadBuffer, null);

                        Interlocked.Exchange(ref _ReadBuffer, buf);

                        var readResult = _Source.BeginRead(buf.Memory, 0, buf.Memory.Length, OnBytesRead, buf);
                    }
                    else
                    {
                        _ReadBuffer = null;
                    }
                    //start write operation
                    _Destination.BeginWrite(currentBuffer.Memory, 0, currentBuffer.BytesWritten, OnBytesWritten, currentBuffer);
                }
                else
                {
                    _ReadBuffer = null;
                    Interlocked.Exchange(ref _NextWriteBuffer, currentBuffer);
                }
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
        }
Example #3
0
        private void OnBytesWritten(IAsyncResult result)
        {
            Server.RegisterThread();
            try
            {
                _Destination.EndWrite(result);
                var currentBuffer = (Buffer)result.AsyncState;

                UpdateBytes(currentBuffer.BytesWritten);

                var oldReadBuffer = Interlocked.CompareExchange(ref _ReadBuffer, currentBuffer, null);

                if (oldReadBuffer == null)
                {
                    var buf = Interlocked.Exchange(ref _NextWriteBuffer, null);

                    Interlocked.Exchange(ref _WriteBuffer, buf);

                    if (buf != null && buf.BytesWritten > 0)
                    {
                        _Destination.BeginWrite(buf.Memory, 0, buf.BytesWritten, OnBytesWritten, buf);
                    }
                    else
                    {
                        OnSuccess();
                        return;
                    }

                    //reading was paused
                    _Source.BeginRead(currentBuffer.Memory, 0, currentBuffer.Memory.Length, OnBytesRead, currentBuffer);
                }
                else
                {
                    _WriteBuffer = null;
                    Interlocked.Exchange(ref _NextReadBuffer, currentBuffer);
                }
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
        }
        private void OnBeginRead(IAsyncResult result)
        {
            Server.RegisterThread();
            try
            {
                var readBytes = _Source.EndRead(result);
                _Buffer.BytesWritten = readBytes;

                if (_Buffer.BytesWritten > 0)
                {
                    _Destination.BeginWrite(_Buffer.Memory, 0, _Buffer.BytesWritten, OnBeginWrite, null);
                }
                else
                {
                    OnSuccess();
                }
            }
            catch (Exception ex)
            {
                OnError(ex);
            }
        }