internal NetworkBufferStorage(int perBufferSize, int buffersToAlloc)
        {
            //Debug.Assert(perBufferSize > 0);
            //Debug.Assert(buffersToAlloc > 0);

            if (perBufferSize * buffersToAlloc >= 1048576)
                throw new ArgumentException("Cannot allocate a 1mb or larger buffer storage.");

            _size = perBufferSize;
            _count = buffersToAlloc;

            _rootBuffer = new byte[perBufferSize * buffersToAlloc];
            _buffers = new Stack<NetworkBuffer>(buffersToAlloc);

            // allocate all the network buffers
            for (int i = 0; i < buffersToAlloc; i++)
            {
                NetworkBuffer buffer = new NetworkBuffer(_rootBuffer, i * _size, _size, this);
                _buffers.Push(buffer);
            }

#if DEBUG
            ClearOnRelease = true;
#endif
        }
        internal NetworkBufferStorage(int perBufferSize, int buffersToAlloc)
        {
            //Debug.Assert(perBufferSize > 0);
            //Debug.Assert(buffersToAlloc > 0);

            if (perBufferSize * buffersToAlloc >= 1048576)
            {
                throw new ArgumentException("Cannot allocate a 1mb or larger buffer storage.");
            }

            _size  = perBufferSize;
            _count = buffersToAlloc;

            _rootBuffer = new byte[perBufferSize * buffersToAlloc];
            _buffers    = new Stack <NetworkBuffer>(buffersToAlloc);

            // allocate all the network buffers
            for (int i = 0; i < buffersToAlloc; i++)
            {
                NetworkBuffer buffer = new NetworkBuffer(_rootBuffer, i * _size, _size, this);
                _buffers.Push(buffer);
            }

#if DEBUG
            ClearOnRelease = true;
#endif
        }
Exemple #3
0
 /// <summary>
 /// Creates a new BNCS packet with the specified packet ID.
 /// </summary>
 /// <param name="id">The BNCS packet ID.</param>
 public BncsPacket(BncsPacketId id, NetworkBuffer backingStore)
     : base(backingStore)
 {
     _id = (byte)id;
     InsertByte(0xff);
     InsertByte((byte)id);
     InsertInt16(0);
 }
Exemple #4
0
        /// <summary>
        /// Creates a new <b>DataBuffer</b> backed by a NetworkBuffer.
        /// </summary>
        public DataBuffer(NetworkBuffer targetBuffer)
        {
            if (targetBuffer == null)
                throw new ArgumentNullException("targetBuffer");

            _target = targetBuffer;
            _buffer = targetBuffer.UnderlyingBuffer;
            _ms = new MemoryStream(_buffer, targetBuffer.StartingPosition, targetBuffer.Length, true);
        }
Exemple #5
0
        /// <summary>
        /// Creates a new data reader with the specified network buffer.
        /// </summary>
        /// <param name="buffer">The source of the buffer to read.</param>
        public DataReader(NetworkBuffer buffer)
        {
            if (buffer == null)
                throw new ArgumentNullException("buffer");

            _buffer = buffer;
            _data = buffer.UnderlyingBuffer;
            _baseIndex = buffer.StartingPosition;
            _baseSize = buffer.Length;
        }
Exemple #6
0
        /// <summary>
        /// Creates a new <b>DataBuffer</b> backed by a NetworkBuffer.
        /// </summary>
        public DataBuffer(NetworkBuffer targetBuffer)
        {
            if (targetBuffer == null)
            {
                throw new ArgumentNullException("targetBuffer");
            }

            _target = targetBuffer;
            _buffer = targetBuffer.UnderlyingBuffer;
            _ms     = new MemoryStream(_buffer, targetBuffer.StartingPosition, targetBuffer.Length, true);
        }
Exemple #7
0
        /// <summary>
        /// Creates a new data reader with the specified network buffer.
        /// </summary>
        /// <param name="buffer">The source of the buffer to read.</param>
        public DataReader(NetworkBuffer buffer)
        {
            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            _buffer    = buffer;
            _data      = buffer.UnderlyingBuffer;
            _baseIndex = buffer.StartingPosition;
            _baseSize  = buffer.Length;
        }
Exemple #8
0
        /// <summary>
        /// Creates a new data reader with the specified byte data.
        /// </summary>
        /// <param name="data">The data to read.</param>
        /// <exception cref="ArgumentNullException">Thrown if <b>data</b> is 
        /// <b>null</b> (<b>Nothing</b> in Visual Basic).</exception>
        public BncsReader(NetworkBuffer buffer)
            : base(buffer)
        {
            if (buffer == null)
                throw new ArgumentNullException("buffer");

            if (this.ReadByte() != 0xff)
                throw new InvalidDataException("The buffer was invalid.");
            PacketID = (BncsPacketId)this.ReadByte();
            _len = this.ReadUInt16();
            if (_len > 8192)
                throw new InvalidDataException("The buffer was invalid.");
        }
        public async void Release(NetworkBuffer buffer)
        {
            DebugVerifyBufferIsNotAlreadyInList(buffer);

            if (ClearOnRelease)
                await buffer.Clear();

            Debug.Assert(buffer.Parent == this);

            lock (_syncObject)
            {
                _buffers.Push(buffer);
            }
        }
        /// <summary>
        /// Asynchronously receives an amount of data into a preexisting network buffer.
        /// </summary>
        /// <param name="partialDestination"></param>
        /// <param name="startIndex"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public async Task <NetworkBuffer> ReceiveAsync(NetworkBuffer partialDestination, int startIndex, int length, bool assertHeader = false)
        {
            if (partialDestination == null)
            {
                throw new ArgumentNullException("partialDestination");
            }

            if (length < 1)
            {
                throw new ArgumentOutOfRangeException("length", length, "Length must be a positive integer.");
            }

            if (startIndex >= partialDestination.UnderlyingBuffer.Length)
            {
                throw new ArgumentOutOfRangeException("startIndex", startIndex, "Start index is outside of the range of the destination buffer.");
            }
            if (startIndex + length >= partialDestination.UnderlyingBuffer.Length)
            {
                throw new ArgumentException("Length given start index end outside of the bounds of the destination buffer.");
            }

            int totRecv = 0;
            var stream  = _client.GetStream();

            while (_open && _client.Connected && totRecv < length)
            {
                try
                {
                    totRecv += await stream.ReadAsync(partialDestination.UnderlyingBuffer, partialDestination.StartingPosition + totRecv + startIndex, length - totRecv);
                }
                catch (IOException se)
                {
                    if (IsConnected)
                    {
                        Close();
                        OnConnectionErrorOccurred("A read error occurred on the connection.", se);
                    }

                    return(null);
                }
            }

            if (assertHeader)
            {
                Debug.Assert(partialDestination.UnderlyingBuffer[startIndex + partialDestination.StartingPosition] == 0xff);
            }

            return(partialDestination);
        }
        public async void Release(NetworkBuffer buffer)
        {
            DebugVerifyBufferIsNotAlreadyInList(buffer);

            if (ClearOnRelease)
            {
                await buffer.Clear();
            }

            Debug.Assert(buffer.Parent == this);

            lock (_syncObject)
            {
                _buffers.Push(buffer);
            }
        }
        /// <summary>
        /// Asynchronously receives an amount of data.
        /// </summary>
        /// <param name="length">The amount of data to receive.</param>
        /// <returns>A NetworkBuffer containing the data.</returns>
        public async Task <NetworkBuffer> ReceiveAsync(int length)
        {
            NetworkBuffer buffer = _storage.Acquire();

            if (buffer == null)
            {
                throw new InvalidOperationException("No network storage could be retrieved.");
            }

            if (length > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("length", length, "The underlying storage for network data was not large enough for the requested receive operation.");
            }

            return(await ReceiveAsync(buffer, 0, length));
        }
        /// <summary>
        /// Sends part of the specified binary data to the server.  This method automatically releases the network buffer upon completion.
        /// </summary>
        /// <param name="data">The data to send.</param>
        /// <param name="index">The start index of the data.</param>
        /// <param name="length">The amount of data to send.</param>
        public virtual async Task SendAsync(NetworkBuffer data, int index, int length)
        {
            await SendAsync(data.UnderlyingBuffer, data.StartingPosition + index, length);

            _storage.Release(data);
        }
 /// <summary>
 /// Sends the specified binary data to the server.  This method automatically releases the network buffer upon completion.
 /// </summary>
 /// <param name="data">The data to send.</param>
 public virtual async Task SendAsync(NetworkBuffer data, int length)
 {
     await SendAsync(data, 0, length);
 }
Exemple #15
0
 /// <summary>
 /// Sends part of the specified binary data to the server.  This method automatically releases the network buffer upon completion.
 /// </summary>
 /// <param name="data">The data to send.</param>
 /// <param name="index">The start index of the data.</param>
 /// <param name="length">The amount of data to send.</param>
 public virtual async Task SendAsync(NetworkBuffer data, int index, int length)
 {
     await SendAsync(data.UnderlyingBuffer, data.StartingPosition + index, length);
     _storage.Release(data);
 }
Exemple #16
0
 /// <summary>
 /// Sends the specified binary data to the server.  This method automatically releases the network buffer upon completion.
 /// </summary>
 /// <param name="data">The data to send.</param>
 public virtual async Task SendAsync(NetworkBuffer data, int length)
 {
     await SendAsync(data, 0, length);
 }
Exemple #17
0
        /// <summary>
        /// Asynchronously receives an amount of data into a preexisting network buffer.
        /// </summary>
        /// <param name="partialDestination"></param>
        /// <param name="startIndex"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public async Task<NetworkBuffer> ReceiveAsync(NetworkBuffer partialDestination, int startIndex, int length, bool assertHeader = false)
        {
            if (partialDestination == null)
                throw new ArgumentNullException("partialDestination");

            if (length < 1)
                throw new ArgumentOutOfRangeException("length", length, "Length must be a positive integer.");

            if (startIndex >= partialDestination.UnderlyingBuffer.Length)
                throw new ArgumentOutOfRangeException("startIndex", startIndex, "Start index is outside of the range of the destination buffer.");
            if (startIndex + length >= partialDestination.UnderlyingBuffer.Length)
                throw new ArgumentException("Length given start index end outside of the bounds of the destination buffer.");

            int totRecv = 0;
            var stream = _client.GetStream();
            while (_open && _client.Connected && totRecv < length)
            {
                try
                {
                    totRecv += await stream.ReadAsync(partialDestination.UnderlyingBuffer, partialDestination.StartingPosition + totRecv + startIndex, length - totRecv);
                }
                catch (IOException se)
                {
                    if (IsConnected)
                    {
                        Close();
                        OnConnectionErrorOccurred("A read error occurred on the connection.", se);
                    }

                    return null; 
                }
            }

            if (assertHeader)
                Debug.Assert(partialDestination.UnderlyingBuffer[startIndex + partialDestination.StartingPosition] == 0xff);

            return partialDestination;
        }
Exemple #18
0
 private void DebugVerifyBufferIsNotAlreadyInList(NetworkBuffer buffer)
 {
     Debug.Assert(!_buffers.Contains(buffer));
 }
 private void DebugVerifyBufferIsNotAlreadyInList(NetworkBuffer buffer)
 {
     Debug.Assert(!_buffers.Contains(buffer));
 }