Example #1
0
        /// <inheritdoc/>
        protected override void BeginReceive()
        {
            _readBuffer.Clear();

            Boolean willRaiseEvent;

            try
            {
                willRaiseEvent = Socket.ReceiveAsync(_readBuffer.SocketAsyncEventArgs);
            }
            catch (ObjectDisposedException)
            {
                // do nothing
                return;
            }
            catch (Exception ex)
            {
                EndReceive(ex);
                return;
            }
            if (!willRaiseEvent)
            {
                ProcessReceive(_readBuffer.SocketAsyncEventArgs);
            }
        }
Example #2
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 #3
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);
                }
            }