private void WriteQueueLoop()
        {
            while (!_writeQueue.IsAddingCompleted)
            {
                try
                {
                    if (_lastWritable == null)
                    {
                        _lastWritable = _writeQueue.Take();
                    }
                }
                catch (InvalidOperationException)
                {
                    //BlockingCollection is empty
                    if (_writeQueue.IsAddingCompleted)
                    {
                        return;
                    }
                }

                while (_sendBuffer.HasRemaining() && _lastWritable != null)
                {
                    var complete = _lastWritable.WriteTo(_sendBuffer);
                    if (complete)
                    {
                        //grap one from queue
                        ISocketWritable tmp;
                        _writeQueue.TryTake(out tmp);
                        _lastWritable = tmp;
                    }
                    else
                    {
                        break;
                    }
                }
                if (_sendBuffer.Position > 0)
                {
                    _sendBuffer.Flip();
                    try
                    {
                        _stream.Write(_sendBuffer.Array(), _sendBuffer.Position, _sendBuffer.Remaining());
                        _sendBuffer.Clear();
                    }
                    catch (Exception e)
                    {
                        _lastWritable = null;
                        HandleSocketException(e);
                    }
                }
            }
        }
Example #2
0
        private void WriteQueueLoop()
        {
            while (!_writeQueue.IsAddingCompleted)
            {
                try
                {
                    if (_lastWritable == null)
                    {
                        _lastWritable = _writeQueue.Take();
                    }
                }
                catch (InvalidOperationException)
                {
                    //BlockingCollection is empty
                    if (_writeQueue.IsAddingCompleted)
                    {
                        return;
                    }
                }

                while (_sendBuffer.HasRemaining() && _lastWritable != null)
                {
                    var complete = _lastWritable.WriteTo(_sendBuffer);
                    if (complete)
                    {
                        //grap one from queue
                        ISocketWritable tmp = null;
                        _writeQueue.TryTake(out tmp);
                        _lastWritable = tmp;
                    }
                    else
                    {
                        break;
                    }
                }
                if (_sendBuffer.Position > 0)
                {
                    _sendBuffer.Flip();
                    try
                    {
                        SocketError socketError;
                        var         sendByteSize = _clientSocket.Send(
                            _sendBuffer.Array(),
                            _sendBuffer.Position,
                            _sendBuffer.Remaining(),
                            SocketFlags.None, out socketError);

                        if (sendByteSize <= 0)
                        {
                            Close();
                            return;
                        }

                        if (socketError != SocketError.Success)
                        {
                            HandleSocketException(new SocketException((int)socketError));
                            return;
                        }

                        _sendBuffer.Position += sendByteSize;
                        //logger.Info("SEND BUFFER CALLBACK: pos:" + sendBuffer.Position + " remaining:" + sendBuffer.Remaining());

                        //if success case
                        if (_sendBuffer.HasRemaining())
                        {
                            _sendBuffer.Compact();
                        }
                        else
                        {
                            _sendBuffer.Clear();
                        }
                    }
                    catch (Exception e)
                    {
                        _lastWritable = null;
                        HandleSocketException(e);
                    }
                }
            }
        }
Example #3
0
        private void WriteQueueLoop()
        {
            try
            {
                while (!_writeQueue.IsAddingCompleted)
                {
                    try
                    {
                        if (_lastWritable == null)
                        {
                            _lastWritable = _writeQueue.Take();
                        }
                    }
                    catch (Exception)
                    {
                        //BlockingCollection is empty
                        if (_writeQueue.IsAddingCompleted)
                        {
                            return;
                        }
                    }

                    while (_sendBuffer.HasRemaining() && _lastWritable != null)
                    {
                        var complete = _lastWritable.WriteTo(_sendBuffer);
                        if (complete)
                        {
                            //grap one from queue
                            ISocketWritable tmp;
                            _writeQueue.TryTake(out tmp);
                            _lastWritable = tmp;
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (_sendBuffer.Position > 0)
                    {
                        _sendBuffer.Flip();
                        try
                        {
                            _stream.Write(_sendBuffer.Array(), _sendBuffer.Position, _sendBuffer.Remaining());
                            LastWrite = DateTime.Now;
                            _sendBuffer.Clear();
                        }
                        catch (Exception e)
                        {
                            _lastWritable = null;
                            HandleSocketException(e);
                        }
                    }
                }
            }
            catch (ThreadInterruptedException)
            {
                if (Logger.IsFinestEnabled())
                {
                    Logger.Finest("Writer thread interreptud, stopping...");
                }
            }
        }