Example #1
0
        /// <summary>
        /// Processes receive events.
        /// </summary>
        /// <param name="e"></param>
        public void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                if (e.BytesTransferred > 0)
                {
                    _readBuffer.Position = e.BytesTransferred;
                    _readBuffer.Flip();

                    if (ReuseBuffer)
                    {
                        EndReceive(_readBuffer);
                    }
                    else
                    {
                        IoBuffer buf = IoBuffer.Allocate(_readBuffer.Remaining);
                        buf.Put(_readBuffer);
                        buf.Flip();
                        EndReceive(buf);
                    }

                    return;
                }
                else
                {
                    // closed
                    //Processor.Remove(this);
                    this.FilterChain.FireInputClosed();
                }
            }
            else
            {
                EndReceive(new SocketException((Int32)e.SocketError));
            }
        }
Example #2
0
        /// <summary>
        /// Processes receive events.
        /// </summary>
        /// <param name="e"></param>
        public void ProcessReceive(SocketAsyncEventArgs e)
        {
            if (e.SocketError == SocketError.Success)
            {
                if (e.BytesTransferred > 0)
                {
                    _readBuffer.Position = e.BytesTransferred;
                    _readBuffer.Flip();

                    if (ReuseBuffer)
                    {
                        EndReceive(_readBuffer);
                    }
                    else
                    {
                        IoBuffer buf = IoBuffer.Allocate(_readBuffer.Remaining);
                        buf.Put(_readBuffer);
                        buf.Flip();
                        EndReceive(buf);
                    }

                    return;
                }
            }
            else if (e.SocketError != SocketError.OperationAborted &&
                     e.SocketError != SocketError.Interrupted)
            {
                EndReceive(new SocketException((Int32)e.SocketError));
            }
        }
Example #3
0
        /// <inheritdoc/>
        protected override void BeginSend(IWriteRequest request, IoBuffer buf)
        {
            SocketAsyncEventArgs       saea;
            SocketAsyncEventArgsBuffer saeaBuf = buf as SocketAsyncEventArgsBuffer;

            if (saeaBuf == null)
            {
                _writeBuffer.Clear();
                if (_writeBuffer.Remaining < buf.Remaining)
                {
                    Int32 oldLimit = buf.Limit;
                    buf.Limit = buf.Position + _writeBuffer.Remaining;
                    _writeBuffer.Put(buf);
                    buf.Limit = oldLimit;
                }
                else
                {
                    _writeBuffer.Put(buf);
                }
                _writeBuffer.Flip();
                _writeBuffer.SetBuffer();
                saea = _writeBuffer.SocketAsyncEventArgs;
            }
            else
            {
                saea            = saeaBuf.SocketAsyncEventArgs;
                saea.Completed += _completeHandler;
            }

            Boolean willRaiseEvent;

            try
            {
                willRaiseEvent = Socket.SendAsync(saea);
            }
            catch (ObjectDisposedException)
            {
                // do nothing
                return;
            }
            catch (SocketException ex)
            {
                EndSend(ex);
                return;
            }
            catch (Exception ex)
            {
                EndSend(ex);
                return;
            }
            if (!willRaiseEvent)
            {
                ProcessSend(saea);
            }
        }
Example #4
0
            private void BeginSend(AsyncDatagramSession session, IoBuffer buf, EndPoint remoteEP)
            {
                _writeBuffer.Clear();

                SocketAsyncEventArgs       saea;
                SocketAsyncEventArgsBuffer saeaBuf = buf as SocketAsyncEventArgsBuffer;

                if (saeaBuf == null)
                {
                    if (_writeBuffer.Remaining < buf.Remaining)
                    {
                        // TODO allocate a temp buffer
                    }
                    else
                    {
                        _writeBuffer.Put(buf);
                    }
                    _writeBuffer.Flip();
                    saea = _writeBuffer.SocketAsyncEventArgs;
                    saea.SetBuffer(saea.Offset + _writeBuffer.Position, _writeBuffer.Limit);
                }
                else
                {
                    saea            = saeaBuf.SocketAsyncEventArgs;
                    saea.Completed += _completeHandler;
                }

                saea.UserToken      = session;
                saea.RemoteEndPoint = remoteEP;

                Boolean willRaiseEvent;

                try
                {
                    willRaiseEvent = Socket.SendToAsync(saea);
                }
                catch (ObjectDisposedException)
                {
                    // do nothing
                    return;
                }
                catch (Exception ex)
                {
                    EndSend(session, ex);
                    return;
                }
                if (!willRaiseEvent)
                {
                    ProcessSend(saea);
                }
            }