Exemplo n.º 1
0
        internal static byte[] ReadBytesFromSocketBuffer(int count, SocketBuffer socketBuffer)
        {
            if (count > socketBuffer.UnreadBytes)
            {
                throw new Exception("Did not find enough data to read.");
            }
            var retBytes = new byte[count];

            Buffer.BlockCopy(socketBuffer.Buffer, (int)socketBuffer.ReadIndex, retBytes, 0, count);
            socketBuffer.BytesRead(count);
            return(retBytes);
        }
Exemplo n.º 2
0
 internal bool GetBusyBufferAt(int index, out SocketBuffer buffer)
 {
     buffer = null;
     lock (_mutex)
     {
         if (index >= _busyBuffers.Count)
         {
             return(false);
         }
         buffer = _busyBuffers[index];
         return(true);
     }
 }
Exemplo n.º 3
0
 public void OnBufferFree(SocketBuffer buffer)
 {
     lock (_mutex)
     {
         if (_busyBuffers.Count > 0)
         {
             if (_busyBuffers.Remove(buffer))
             {
                 _freeBuffers.Enqueue(buffer);
                 Monitor.Pulse(_mutex);
             }
         }
     }
 }
Exemplo n.º 4
0
        internal bool FreeTopBusyBuffer()
        {
            lock (_mutex)
            {
                if (_busyBuffers.Count > 0)
                {
                    SocketBuffer buffer = _busyBuffers[0];
                    buffer.Clear();
                    _busyBuffers.RemoveAt(0);
                    _freeBuffers.Enqueue(buffer);

                    Monitor.Pulse(_mutex);
                    return(true);
                }
                return(false);
            }
        }
Exemplo n.º 5
0
        internal bool GetFreeOrNewBuffer(out SocketBuffer socketBuffer)
        {
            socketBuffer = null;

            lock (_mutex)
            {
                while (_busyBuffers.Count == _maxBuffers && !_isDisposed)
                {
                    Monitor.Wait(_mutex);
                }

                if (_freeBuffers.Count == 0)
                {
                    _freeBuffers.Enqueue(_bufferFactory.CreateSocketBuffer(this));
                }

                socketBuffer = _freeBuffers.Dequeue();
                _busyBuffers.Add(socketBuffer);
                return(true);
            }
        }
Exemplo n.º 6
0
        // TODO: Introduce the .Net 4.7.x's Span class here.
        public override int Read(byte[] buffer, int offset, int count)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }
            if (offset < 0 /*|| _commandLength - _position < offset*/)
            {
                throw new ArgumentOutOfRangeException("offset");
            }
            if (count < 0 /*|| _commandLength - _position < count*/)
            {
                throw new ArgumentOutOfRangeException("count");
            }
            if (buffer.Length - offset < count)
            {
                throw new ArgumentException("Argument_InvalidOffLen");
            }
            if (_buffers.Count == 0)
            {
                throw new Exception("No Buffer");
            }

            int bytesRead = 0;

            while (bytesRead < count && _currBufferIndex < _buffers.Count)
            {
                SocketBuffer currentBuffer = _buffers[_currBufferIndex];


                bytesRead += SocketBufferUtil.ReadBytesFromSocketBuffer(buffer, offset + bytesRead, count - bytesRead, currentBuffer);
                _position += bytesRead;
                if (currentBuffer.UnreadBytes == 0)
                {
                    _currBufferIndex++;
                }
                // }
            }
            return(bytesRead);
        }
Exemplo n.º 7
0
        internal static int ReadBytesFromSocketBuffer(byte[] buffer, int offset, int count, SocketBuffer socketBuffer)
        {
            int byteToRead = (int)socketBuffer.UnreadBytes > count ? count : (int)socketBuffer.UnreadBytes;

            Buffer.BlockCopy(socketBuffer.Buffer, (int)socketBuffer.ReadIndex, buffer, offset, byteToRead);
            socketBuffer.BytesRead(byteToRead);
            return(byteToRead);
        }
Exemplo n.º 8
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            switch (origin)
            {
            case SeekOrigin.Begin:
            {
                if (offset < 0 || _commandLength < offset)
                {
                    throw new ArgumentOutOfRangeException("offset");
                }

                _position        = offset;
                _currBufferIndex = 0;

                for (int i = 0; i < _buffers.Count; i++)
                {
                    if (i == 0)
                    {
                        _buffers[i].BytesRead(_commandStart - (int)_buffers[i].ReadIndex);
                    }
                    else
                    {
                        _buffers[i].BytesRead(0 - _buffers[i].ReadIndex);
                    }
                }

                while (offset > 0)
                {
                    SocketBuffer currentBuffer = _buffers[_currBufferIndex];

                    if (currentBuffer.UnreadBytes < offset)
                    {
                        offset -= currentBuffer.UnreadBytes;
                        currentBuffer.BytesRead(currentBuffer.UnreadBytes);
                        _currBufferIndex++;
                    }
                    else
                    {
                        currentBuffer.BytesRead(offset);
                    }
                }
                break;
            }

            case SeekOrigin.Current:
            {
                long tempPosition = unchecked (_position + offset);
                if (tempPosition < 0 || tempPosition > _commandLength)
                {
                    throw new IOException("IO.IO_SeekBeforeBegin");
                }

                _position = tempPosition;

                if (offset < 0)
                {
                    while (offset < 0)
                    {
                        SocketBuffer currentBuffer = _buffers[_currBufferIndex];
                        currentBuffer.BytesRead(0 - currentBuffer.ReadIndex);

                        if (currentBuffer.ReadIndex + offset < 0)
                        {
                            offset += currentBuffer.ReadIndex;
                            currentBuffer.BytesRead(0 - currentBuffer.ReadIndex);
                            _currBufferIndex--;
                        }
                        else
                        {
                            currentBuffer.BytesRead(offset);
                        }
                    }
                }
                else
                {
                    while (offset > 0)
                    {
                        SocketBuffer currentBuffer = _buffers[_currBufferIndex];

                        if (currentBuffer.UnreadBytes < offset)
                        {
                            offset -= currentBuffer.UnreadBytes;
                            currentBuffer.BytesRead(currentBuffer.UnreadBytes);
                            _currBufferIndex++;
                        }
                        else
                        {
                            currentBuffer.BytesRead(offset);
                        }
                    }
                }

                break;
            }

            case SeekOrigin.End:
            {
                if (offset > 0 || _commandLength < -1 * offset)
                {
                    throw new ArgumentOutOfRangeException("offset");
                }

                _currBufferIndex = _buffers.Count - 1;
                _position        = _commandLength + offset;

                for (int i = _buffers.Count - 1; i >= 0; i--)
                {
                    if (i == _buffers.Count - 1)
                    {
                        _buffers[i].BytesRead(_commandEnd - _buffers[i].ReadIndex);
                    }
                    else
                    {
                        _buffers[i].BytesRead(_buffers[i].UnreadBytes);
                    }
                }

                while (offset < 0)
                {
                    SocketBuffer currentBuffer = _buffers[_currBufferIndex];

                    if (currentBuffer.ReadIndex + offset < 0)
                    {
                        offset += currentBuffer.ReadIndex;
                        currentBuffer.BytesRead(0 - currentBuffer.ReadIndex);
                        _currBufferIndex--;
                    }
                    else
                    {
                        currentBuffer.BytesRead(offset);
                    }
                }

                break;
            }

            default: throw new ArgumentException("Argument_InvalidSeekOrigin");
            }

            return(_position);
        }