Esempio n. 1
0
 public void should_throw_an_unabletocreatememoryexception_if_acquiring_memory_is_disabled_and_out_of_memory()
 {
     BufferManager manager = new BufferManager(1, 1000, 1, false);
     manager.CheckOut();
     //should be none left, boom
     Assert.Throws<UnableToCreateMemoryException>(() => manager.CheckOut());
 }
Esempio n. 2
0
        public void CheckOut_should_return_a_buffer_of_same_size_as_manager()
        {
            var buffer = _Manager.CheckOut();

            Assert.That(buffer, Is.Not.Null);
            Assert.That(buffer.Memory.Length, Is.EqualTo(_BufferSize));
        }
Esempio n. 3
0
        public void should_throw_an_unabletocreatememoryexception_if_acquiring_memory_is_disabled_and_out_of_memory()
        {
            BufferManager manager = new BufferManager(1, 1000, 1, false);

            manager.CheckOut();
            //should be none left, boom
            Assert.Throws <UnableToCreateMemoryException>(() => manager.CheckOut());
        }
Esempio n. 4
0
        public void should_throw_an_unabletocreatememoryexception_if_acquiring_memory_is_disabled_and_out_of_memory()
        {
            BufferManager       manager = new BufferManager(1, 1000, 1, false);
            ArraySegment <byte> buffer  = manager.CheckOut();

            //should be none left, boom
            manager.CheckOut();
        }
Esempio n. 5
0
        public void TestUpdatePacket()
        {
            var mgr = new BufferManager(2, 128);

            var segment1 = mgr.CheckOut();              // 1st buffer

            Assert.AreEqual(1, mgr.AvailableSegmentsCount);

            var segment2 = mgr.CheckOut();              // 1st buffer

            Assert.AreEqual(1, mgr.TotalBufferCount);
            Assert.AreEqual(0, mgr.AvailableSegmentsCount);
            //Assert.AreEqual(2, segment2.Buffer.UsedSegmentCount);

            var segment3 = mgr.CheckOut();             // 2nd buffer

            Assert.AreEqual(2, mgr.TotalBufferCount);
            Assert.AreEqual(1, mgr.AvailableSegmentsCount);
            //Assert.AreEqual(1, segment3.Buffer.UsedSegmentCount);

            segment3.DecrementUsage();
            Assert.AreEqual(2, mgr.AvailableSegmentsCount);
            //Assert.AreEqual(0, segment3.Buffer.UsedSegmentCount);

            //using (var outPacket = new UpdatePacket(segment2))
            using (var outPacket = new UpdatePacket(128))
            {
                Assert.AreEqual(outPacket.PacketId, (PacketId)RealmServerOpCode.SMSG_UPDATE_OBJECT);

                outPacket.Write(15);
                outPacket.WriteCString("abc");

                using (var inPacket = DisposableRealmPacketIn.CreateFromOutPacket(segment1, outPacket))
                {
                    Assert.AreEqual(0, inPacket.ReadInt32());                                           //update count

                    Assert.AreEqual(15, inPacket.ReadInt32());
                    Assert.AreEqual("abc", inPacket.ReadCString());
                }

                outPacket.Reset();
                Assert.AreEqual(outPacket.PacketId, (PacketId)RealmServerOpCode.SMSG_UPDATE_OBJECT);

                outPacket.WriteCString("def");
                outPacket.WriteUShortBE(300);


                using (var inPacket = DisposableRealmPacketIn.CreateFromOutPacket(segment1, outPacket))
                {
                    Assert.AreEqual(0, inPacket.ReadInt32());                                           //update count
                    Assert.AreEqual((byte)0, inPacket.ReadByte());                                      //block count

                    Assert.AreEqual("def", inPacket.ReadCString());
                    Assert.AreEqual((ushort)300, inPacket.ReadUInt16BE());
                }
            }
        }
Esempio n. 6
0
        public void should_increment_available_buffers()
        {
            BufferManager manager = new BufferManager(10, 1000, 0);

            manager.CheckIn(manager.CheckOut());
            Assert.AreEqual(10, manager.AvailableBuffers);
        }
Esempio n. 7
0
        public void should_return_a_valid_buffer_when_available()
        {
            BufferManager       manager = new BufferManager(1, 1000, 1);
            ArraySegment <byte> buffer  = manager.CheckOut();

            Assert.AreEqual(1000, buffer.Count);
        }
Esempio n. 8
0
        public void should_decrement_available_buffers()
        {
            BufferManager manager = new BufferManager(1, 1000, 1);

            manager.CheckOut();
            Assert.AreEqual(0, manager.AvailableBuffers);
        }
Esempio n. 9
0
        public void should_create_a_segment_if_none_are_availabke()
        {
            BufferManager manager = new BufferManager(10, 1000, 0);

            manager.CheckOut();
            Assert.AreEqual(9, manager.AvailableBuffers);
        }
Esempio n. 10
0
        public void should_release_acquired_buffers_if_size_requirement_cant_be_satisfied()
        {
            BufferManager manager = new BufferManager(1, 1000, 1, false);

            Assert.Throws(Is.InstanceOf(typeof(Exception)), () => manager.CheckOut(2));
            Assert.AreEqual(1, manager.AvailableBuffers);
        }
Esempio n. 11
0
        public void TestBufferManagerEtc()
        {
            var mgr = new BufferManager(2, 128);

            var segment1 = mgr.CheckOut();              // 1st buffer

            Assert.AreEqual(1, mgr.AvailableSegmentsCount);

            var segment2 = mgr.CheckOut();              // 1st buffer

            Assert.AreEqual(1, mgr.TotalBufferCount);
            Assert.AreEqual(0, mgr.AvailableSegmentsCount);
            //Assert.AreEqual(2, segment2.Buffer.UsedSegmentCount);

            var segment3 = mgr.CheckOut();             // 2nd buffer

            Assert.AreEqual(2, mgr.TotalBufferCount);
            Assert.AreEqual(1, mgr.AvailableSegmentsCount);
            //Assert.AreEqual(1, segment3.Buffer.UsedSegmentCount);

            segment3.DecrementUsage();
            Assert.AreEqual(2, mgr.AvailableSegmentsCount);
            //Assert.AreEqual(0, segment3.Buffer.UsedSegmentCount);

            using (var outPacket = new RealmPacketOut(RealmServerOpCode.CMSG_ACCEPT_TRADE))
            {
                outPacket.Position = outPacket.HeaderSize;
                outPacket.Write(1);
                outPacket.WriteCString("abc");

                var inPacket = DisposableRealmPacketIn.CreateFromOutPacket(segment1, outPacket);
                Assert.AreEqual(1, inPacket.ReadInt32());
                Assert.AreEqual("abc", inPacket.ReadCString());
            }

            using (var outPacket = new RealmPacketOut(RealmServerOpCode.CMSG_ACCEPT_TRADE))
            {
                outPacket.Position = outPacket.HeaderSize;
                outPacket.WriteCString("def");
                outPacket.Write(2);

                var inPacket = DisposableRealmPacketIn.CreateFromOutPacket(segment2, outPacket);
                Assert.AreEqual("def", inPacket.ReadCString());
                Assert.AreEqual(2, inPacket.ReadInt32());
            }
        }
Esempio n. 12
0
        public static byte[] CompressResponse(byte[] response, string compressionAlgorithm)
        {
            if (string.IsNullOrEmpty(compressionAlgorithm) ||
                !SupportedCompressionAlgorithms.Contains(compressionAlgorithm))
            {
                return(response);
            }

            MemoryStream outputStream;
            var          useBufferManager =
                10L * response.Length <=
                9L * _compressionBufferManager
                .ChunkSize;                         //in some rare cases, compression can result in larger outputs. Added a 10% overhead just to be sure.
            var bufferManagerArraySegment = new ArraySegment <byte>();

            if (useBufferManager)
            {
                //use buffer manager to handle responses less than ~50kb long to prevent excessive memory allocations
                bufferManagerArraySegment = _compressionBufferManager.CheckOut();
                outputStream = new MemoryStream(bufferManagerArraySegment.Array, bufferManagerArraySegment.Offset,
                                                bufferManagerArraySegment.Count);
                outputStream.SetLength(0);
            }
            else
            {
                //since Gzip/Deflate compression ratio doesn't go below 20% in most cases, we can initialize the array to a quarter of the original response length
                //this also limits memory stream growth operations to at most 2.
                outputStream = new MemoryStream((response.Length + 4 - 1) / 4);
            }

            using (outputStream) {
                Stream compressedStream = null;
                if (compressionAlgorithm.Equals(CompressionAlgorithms.Gzip))
                {
                    compressedStream = new GZipStream(outputStream, CompressionLevel.Fastest);
                }
                else if (compressionAlgorithm.Equals(CompressionAlgorithms.Deflate))
                {
                    compressedStream = new DeflateStream(outputStream, CompressionLevel.Fastest);
                }

                using (compressedStream)
                    using (var dataStream = new MemoryStream(response)) {
                        dataStream.CopyTo(compressedStream);
                    }

                var result = outputStream.ToArray();
                if (useBufferManager)
                {
                    _compressionBufferManager.CheckIn(bufferManagerArraySegment);
                }
                return(result);
            }
        }
Esempio n. 13
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BufferPool"/> class.
 /// </summary>
 /// <param name="initialBufferCount">The number of initial buffers.</param>
 /// <param name="bufferManager">The buffer manager.</param>
 public BufferPool(int initialBufferCount, BufferManager bufferManager)
 {
     if (initialBufferCount <= 0)
         throw new ArgumentException("initialBufferCount");
     if (bufferManager == null)
         throw new ArgumentNullException("bufferManager");
     _length = 0;
     _buffers = new List<ArraySegment<byte>>(bufferManager.CheckOut(initialBufferCount));
     // must have 1 buffer
     _chunkSize = _buffers[0].Count;
     _bufferManager = bufferManager;
     _disposed = false;
 }
Esempio n. 14
0
        private void StartReceive()
        {
            var buffer = BufferManager.CheckOut();

            if (buffer.Array == null || buffer.Count == 0 || buffer.Array.Length < buffer.Offset + buffer.Count)
            {
                throw new Exception("Invalid buffer allocated");
            }

            lock (_receiveSocketArgs)
            {
                _receiveSocketArgs.SetBuffer(buffer.Array, buffer.Offset, buffer.Count);
                if (_receiveSocketArgs.Buffer == null)
                {
                    throw new Exception("Buffer was not set");
                }
            }
            try
            {
                Interlocked.Increment(ref _recvAsyncs);
                NotifyReceiveStarting();
                bool firedAsync;
                lock (_receiveSocketArgs)
                {
                    if (_receiveSocketArgs.Buffer == null)
                    {
                        throw new Exception("Buffer was lost");
                    }
                    firedAsync = _receiveSocketArgs.AcceptSocket.ReceiveAsync(_receiveSocketArgs);
                }
                if (!firedAsync)
                {
                    Log.Info("SYNC receive");
                    ProcessReceive(_receiveSocketArgs);
                }
            }
            catch (ObjectDisposedException)
            {
                ReturnReceivingSocketArgs();
            }
        }
Esempio n. 15
0
        private void StartReceive()
        {
            var buffer = BufferManager.CheckOut();

            if (buffer.Array == null || buffer.Count == 0 || buffer.Array.Length < buffer.Offset + buffer.Count)
            {
                throw new Exception("Invalid buffer allocated");
            }
            // TODO AN: do we need to lock on _receiveSocketArgs?..
            lock (_receiveSocketArgs)
            {
                _receiveSocketArgs.SetBuffer(buffer.Array, buffer.Offset, buffer.Count);
                if (_receiveSocketArgs.Buffer == null)
                {
                    throw new Exception("Buffer was not set");
                }
            }
            try
            {
                NotifyReceiveStarting();
                bool firedAsync;
                lock (_receiveSocketArgs)
                {
                    if (_receiveSocketArgs.Buffer == null)
                    {
                        throw new Exception("Buffer was lost");
                    }
                    firedAsync = _receiveSocketArgs.AcceptSocket.ReceiveAsync(_receiveSocketArgs);
                }
                if (!firedAsync)
                {
                    ProcessReceive(_receiveSocketArgs);
                }
            }
            catch (ObjectDisposedException)
            {
                ReturnReceivingSocketArgs();
            }
        }
Esempio n. 16
0
            private void StartReceive()
            {
                try
                {
                    Interlocked.Increment(ref _recvAsyncs);

                    _currentBuffer = BufferManager.CheckOut();
                    _socket.BeginReceive(_currentBuffer.Array,
                                         _currentBuffer.Offset,
                                         _currentBuffer.Count,
                                         SocketFlags.None,
                                         OnReceiveAsyncCompleted,
                                         null);
                }
                catch (SocketException exc)
                {
                    CloseInternal(exc.SocketErrorCode);
                }
                catch (Exception)
                {
                    CloseInternal(SocketError.Shutdown);
                }
            }
Esempio n. 17
0
        private void StartReceive()
        {
            var buffer = BufferManager.CheckOut();

            if (buffer.Array == null || buffer.Count == 0 || buffer.Array.Length < buffer.Offset + buffer.Count)
            {
                throw new Exception("Invalid buffer allocated");
            }

            try
            {
                NotifyReceiveStarting();
                _receiveSocketArgs.SetBuffer(buffer.Array, buffer.Offset, buffer.Count);
                var firedAsync = _receiveSocketArgs.AcceptSocket.ReceiveAsync(_receiveSocketArgs);
                if (!firedAsync)
                {
                    ProcessReceive(_receiveSocketArgs);
                }
            }
            catch (ObjectDisposedException)
            {
                ReturnReceivingSocketArgs();
            }
        }
Esempio n. 18
0
 public void should_accept_a_checked_out_buffer()
 {
     BufferManager manager = new BufferManager(10, 1000, 0);
     manager.CheckIn(manager.CheckOut());
 }
Esempio n. 19
0
        public void should_accept_a_checked_out_buffer()
        {
            BufferManager manager = new BufferManager(10, 1000, 0);

            manager.CheckIn(manager.CheckOut());
        }
Esempio n. 20
0
 public void should_throw_an_unabletocreatememoryexception_if_acquiring_memory_is_disabled_and_out_of_memory()
 {
     BufferManager manager = new BufferManager(1, 1000, 1, false);
     ArraySegment<byte> buffer = manager.CheckOut();
     //should be none left, boom
     manager.CheckOut();
 }
Esempio n. 21
0
 public void should_increment_available_buffers()
 {
     BufferManager manager = new BufferManager(10, 1000, 0);
     manager.CheckIn(manager.CheckOut());
     Assert.AreEqual(10, manager.AvailableBuffers);
 }
Esempio n. 22
0
 public void should_return_a_valid_buffer_when_available()
 {
     BufferManager manager = new BufferManager(1, 1000, 1);
     ArraySegment<byte> buffer = manager.CheckOut();
     Assert.AreEqual(1000, buffer.Count);
 }
Esempio n. 23
0
 public void should_decrement_available_buffers()
 {
     BufferManager manager = new BufferManager(1, 1000, 1);
     manager.CheckOut();
     Assert.AreEqual(0, manager.AvailableBuffers);
 }
Esempio n. 24
0
 public void should_create_a_segment_if_none_are_availabke()
 {
     BufferManager manager = new BufferManager(10, 1000, 0);
     manager.CheckOut();
     Assert.AreEqual(9, manager.AvailableBuffers);
 }
Esempio n. 25
0
 /// <summary>
 /// </summary>
 public ISComV2ClientBase()
 {
     this._bufferSegment = Buffers.CheckOut();
 }
Esempio n. 26
0
 public void should_release_acquired_buffers_if_size_requirement_cant_be_satisfied()
 {
     BufferManager manager = new BufferManager(1, 1000, 1, false);
     Assert.Throws(Is.InstanceOf(typeof(Exception)), () => manager.CheckOut(2));
     Assert.AreEqual(1, manager.AvailableBuffers);
 }