public void a_write_will_automatically_grow_the_buffer_pool()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     int initialCapacity = pool.Capacity;
     pool[initialCapacity + 14] = 5;
     Assert.AreEqual(initialCapacity * 2, pool.Capacity);
 }
 public void length_is_updated_when_index_higher_than_count_set()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     Assert.AreEqual(0, pool.Length);
     pool[3] = 5;
     Assert.AreEqual(4, pool.Length);
 }
 static SocketServer()
 {
     var max = CurrencyStoreSection.Instance.Server.Buffer.MaxLength;
     var size = CurrencyStoreSection.Instance.Server.Buffer.Size;
     socketArgsPool = new SocketArgsPool(max);
     bufferManager = new BufferPool(max * size, size);
     allowAllowAnonymous = CurrencyStoreSection.Instance.Authorization.AllowAnonymous;
 }
 public void ReleaseBufferTest()
 {
     string strBufferName = string.Empty; // TODO: 初始化为适当的值
     long iInitialCapacity = 0; // TODO: 初始化为适当的值
     long iBufferSize = 0; // TODO: 初始化为适当的值
     BufferPool target = new BufferPool( strBufferName, iInitialCapacity, iBufferSize ); // TODO: 初始化为适当的值
     byte[] byteBuffer = null; // TODO: 初始化为适当的值
     target.ReleaseBuffer( byteBuffer );
     Assert.Inconclusive( "无法验证不返回值的方法。" );
 }
Exemple #5
0
		public void StartSound()
		{
			BufferSizeSamples = Sound.MillisecondsToSamples(Global.Config.SoundBufferSizeMs);
			MaxSamplesDeficit = BufferSizeSamples;

			_sourceID = AL.GenSource();

			_bufferPool = new BufferPool();
			_currentSamplesQueued = 0;
		}
Exemple #6
0
 public void BufferPool()
 {
     BufferPool bp = new BufferPool(10000);
     int ct = 1000;
     for (int i = 0; i < ct; i++) {
         byte[] b = bp.GetBuffer();
         Assert.AreEqual(10000, b.Length);
         bp.ReturnBuffer(b);
     }
     Console.WriteLine("{0} allocations for {1} uses of buffer pool.", bp.NumAllocations, ct);
 }
Exemple #7
0
		public void StopSound()
		{
			AL.SourceStop(_sourceID);

			AL.DeleteSource(_sourceID);

			_bufferPool.Dispose();
			_bufferPool = null;

			BufferSizeSamples = 0;
		}
Exemple #8
0
        internal NetSocket(INetAddress address, BufferPool bufferPool, SocketEventArgPool socketPool, Socket baseSocket)
        {
            this.BufferPool = bufferPool;
            this.SocketPool = socketPool;
            this.BaseSocket = baseSocket;
            this.BaseSocket.NoDelay = true;

            this.IsClient = true;
            this.OnAccepted = delegate { };
            this.OnReceived = delegate { };
            this.OnSent = delegate { };
            this.OnFault = delegate { };
        }
Exemple #9
0
        public NetSocket(INetAddress address, BufferPool bufferPool, SocketEventArgPool socketPool)
        {
            this.Address = address;
            this.BufferPool = bufferPool;
            this.SocketPool = socketPool;

            this.BaseSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.BaseSocket.LingerState = new LingerOption(true, 0);
            this.BaseSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);

            this.IsClient = false;
            this.OnAccepted = delegate { };
            this.OnReceived = delegate { };
            this.OnSent = delegate { };
            this.OnFault = delegate { };
        }
        public void ThreeThousandGetBuffers()
        {
            BufferPool pool = new BufferPool(10 * 1024 * 1024, 1, 1);

                //PLAN:
                //Just like HundredGetBuffers but with 3000 threads

                List<IBuffer> bufferList = new List<IBuffer>();

                int threadNumber = 3000;
                int sizeOdd = 524288;
                int sizeEven = 524288;

                AquireBuffersConcurrently(pool, bufferList, threadNumber, sizeOdd, sizeEven);
                AssertIsContiguous(bufferList);
                Assert.IsTrue(pool.SlabCount == 150 || pool.SlabCount == 151, "SlabCount is " + pool.SlabCount + ". Was expecting 150 or 151");
        }
        private static void AquireBuffersConcurrently(BufferPool pool, List<IBuffer> bufferList, int threadNumber, int sizeOdd, int sizeEven)
        {
            object bufferList_sync = new object();
            ManualResetEvent mre = new ManualResetEvent(false);

            for (int i = 0; i < threadNumber; i++)
            {
                Thread thread = new Thread(delegate(object number)
                {
                    var size = ((int)number % 2 == 1 ? sizeOdd : sizeEven);

                    //wait for signal
                    mre.WaitOne();

                    //Aquire buffer
                    IBuffer buff = pool.GetBuffer(size);

                    //Add to Queue
                    lock (bufferList_sync)
                    {
                        bufferList.Add(buff);
                    }
                });

                thread.IsBackground = true;
                thread.Start(i);
            }

            Thread.Sleep(500); //ensure all threads are waiting for signal

            mre.Set(); //signal event

            //Wait till all threads are done
            while (true)
            {
                lock (bufferList)
                {
                    if (bufferList.Count == threadNumber) break;
                    Thread.Sleep(500);
                }
            }
        }
Exemple #12
0
		public void StartSound()
		{
			BufferSizeSamples = Sound.MillisecondsToSamples(Global.Config.SoundBufferSizeMs);
			MaxSamplesDeficit = BufferSizeSamples;

			var format = new WaveFormat
				{
					SamplesPerSecond = Sound.SampleRate,
					BitsPerSample = Sound.BytesPerSample * 8,
					Channels = Sound.ChannelCount,
					FormatTag = WaveFormatTag.Pcm,
					BlockAlignment = Sound.BlockAlign,
					AverageBytesPerSecond = Sound.SampleRate * Sound.BlockAlign
				};

			_sourceVoice = new SourceVoice(_device, format);

			_bufferPool = new BufferPool();
			_runningSamplesQueued = 0;

			_sourceVoice.Start();
		}
        public void HundredGetBuffers()
        {
            BufferPool pool = new BufferPool(10 * 1024 * 1024, 1, 1);

                //PLAN:
                //Create 100 threads. Odd numbered threads will request buffer sizes of 314567 bytes
                //whereas even numbered threads will request buffer sizes of 314574 bytes
                //for a total allocation of 31457100 bytes.
                //If everything goes well there will be no overlap in allocated buffers
                //and there'll be no free space greater than 314574 (the lower number) on total slabs - 1
                //and slabs should be 4

                List<IBuffer> bufferList = new List<IBuffer>();

                int threadNumber = 100;
                int sizeOdd = 314567;
                int sizeEven = 314574;

                AquireBuffersConcurrently(pool, bufferList, threadNumber, sizeOdd, sizeEven);
                AssertIsContiguous(bufferList);
                Assert.IsTrue(pool.SlabCount == 3 || pool.SlabCount == 4, "SlabCount is " + pool.SlabCount + ". Was expecting 3 or 4");
        }
 public void length_is_set_when_setting_length()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     pool.SetLength(5000, false);
     Assert.AreEqual(5000, pool.Length);
 }
 public void the_byte_array_should_be_the_same_length_as_the_pool_with_data()
 {
     BufferPool pool = new BufferPool(5, BufferManager);
     for (int i = 0; i < 500; i++)
     {
         pool[i] = 12;
     }
     Assert.AreEqual(500, pool.ToByteArray().Length);
 }
 public void data_is_written_to_the_internal_buffer()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     byte[] data = { 1, 2, 3, 4, 5 };
     pool.Append(data);
     for (byte i = 0; i < 5; i++)
     {
         Assert.AreEqual(i + 1, pool[i]);
     }
 }
 public void a_write_past_end_will_check_out_a_buffer_from_the_buffer_pool()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     int initial = BufferManager.AvailableBuffers;
     pool[pool.Capacity + 14] = 5;
     Assert.AreEqual(initial - 1, BufferManager.AvailableBuffers);
 }
 public void data_that_has_been_set_can_read()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     pool[3] = 5;
     Assert.AreEqual(5, pool[3]);
 }
 public void pool_can_expand_capacity()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     int initialCapacity = pool.Capacity;
     byte[] data = new byte[initialCapacity + 25];
     pool.Append(data);
     Assert.AreEqual(initialCapacity * 2, pool.Capacity);
 }
 public override void Setup()
 {
     base.Setup();
     m_DisposedPool = new BufferPool(10, BufferManager);
     m_DisposedPool.Dispose();
 }
 public void an_index_under_zero_throws_an_argument_exception()
 {
     BufferPool pool = new BufferPool(12, BufferManager);
     Assert.Throws<ArgumentException>(() => pool[-1] = 4);
 }
 public void can_write_given_a_self_offset()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     byte[] data = { 1, 2, 3, 4, 5 };
     pool.Write(4, data, 0, 5); //start at position 4
     for (byte i = 4; i < 9; i++)
     {
         Assert.AreEqual(i - 3, pool[i]);
     }
 }
 void DecreaseQuota(ref BufferPool bufferPool)
 {
     ChangeQuota(ref bufferPool, -1);
 }
 public void can_write_given_a_source_offset()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     byte[] data = { 1, 2, 3, 4, 5 };
     pool.Write(0, data, 3, 2);
     Assert.AreEqual(pool[0], 4);
     Assert.AreEqual(pool[1], 5);
 }
 public void a_larger_length_makes_capacity_larger()
 {
     BufferManager manager = new BufferManager(10, 1000, 1);
     BufferPool pool = new BufferPool(1, manager);
     pool.SetLength(5000);
     Assert.AreNotEqual(5000, pool.Capacity);
 }
 public void a_negative_length_throws_an_argumentexception()
 {
     BufferPool pool = new BufferPool(1, BufferManager);
     pool.SetLength(-1, false);
 }
 void ChangeQuota(ref BufferPool bufferPool, int delta)
 {
     BufferPool oldBufferPool = bufferPool;
     int newLimit = oldBufferPool.Limit + delta;
     BufferPool newBufferPool = new BufferPool(oldBufferPool.BufferSize, newLimit);
     for (int i = 0; i < newLimit; i++)
     {
         byte[] buffer = oldBufferPool.Take();
         if (buffer == null)
         {
             break;
         }
         newBufferPool.Return(buffer);
         newBufferPool.IncrementCount();
     }
     this.remainingMemory -= oldBufferPool.BufferSize * delta;
     bufferPool = newBufferPool;
 }
 public void a_smaller_length_lowers_capacity()
 {
     BufferManager manager = new BufferManager(10, 1000, 1);
     BufferPool pool = new BufferPool(5, manager);
     pool.SetLength(1);
     Assert.AreEqual(9, manager.AvailableBuffers);
 }
 void IncreaseQuota(ref BufferPool bufferPool)
 {
     ChangeQuota(ref bufferPool, 1);
 }
 public void a_smaller_length_checks_buffers_back_in_when_not_allowed()
 {
     BufferManager manager = new BufferManager(10, 1000, 1);
     BufferPool pool = new BufferPool(5, manager);
     pool.SetLength(1, false);
     Assert.AreEqual(5, manager.AvailableBuffers);
 }