public void Release() { _listeningSocket.Shutdown(SocketShutdown.Both); _listeningSocket.Close(); var listeningThreads = _listeningThreads.ToList(); foreach (var lt in listeningThreads) { if (lt.IsAlive) { lt.Abort(); } } _listeningThreads.Clear(); var connections = _connectionsPool?.GetUsedObjects(); if (connections != null && connections.Any()) { foreach (var connection in connections) { connection.Dispose(); _connectionsPool.Release(connection); } } }
/// <summary> /// Receive message body /// </summary> /// <param name="ar"></param> private void ReceivePacketCallback(IAsyncResult ar) { var connection = (ClientConnection)ar.AsyncState; try { SocketError err; var readed = connection.Socket.EndReceive(ar, out err); if (err != SocketError.Success || readed <= 0) { Log.Debug("Client disconnected."); connection.Release(); connection.Socket.Disconnect(false); connection.Socket.Close(); _connectionsPool.Release(connection); return; } var header = connection.WaitPacketLen; var len = (BitConverter.ToUInt16(header, 0)) - connection.WaitPacketLen.Length; //packet body size without len if (len <= 0) //if message is empty, continue { connection.Socket.BeginReceive(connection.WaitPacketLen, 0, connection.WaitPacketLen.Length, SocketFlags.None, ReceivePacketCallback, connection); } else { var datas = new byte[len]; connection.Socket.AsyncReceiveFixed(datas, (state, buffer) => //receive fully datas by len { PacketHandler.Process(connection, datas); }, null); connection.Socket.BeginReceive(connection.WaitPacketLen, 0, connection.WaitPacketLen.Length, SocketFlags.None, ReceivePacketCallback, connection); //Make receive sequence } } catch (ObjectDisposedException) { } catch (SocketException) { } catch (Exception e) { Log.Info($"Exception on packet receive. {e}"); if (connection != null) { Disconnect(connection); } } }
public async Task WhenFirstObjectIsLockedAndSecondCallIsPerformedThenSecondObjectIsCreated() { var instanceNumber = 0; var testPool = new ObjectsPool <PooledObject>( new PooledItemManager <PooledObject>( () => Task.FromResult(new PooledObject(++instanceNumber)), item => Task.CompletedTask), _poolsCleaner, TimeSpan.MaxValue, _testDateTimeProvider, new LoggerStub(), 2); var testObject = await testPool.Acquire(); Assert.NotNull(testObject); Assert.Equal(1, testObject.InstanceNumber); var secondTestObject = await testPool.Acquire(); Assert.NotNull(secondTestObject); Assert.Equal(2, secondTestObject.InstanceNumber); testPool.Release(secondTestObject); secondTestObject = await testPool.Acquire(); Assert.NotNull(secondTestObject); Assert.Equal(2, secondTestObject.InstanceNumber); }
public async Task WhenPooledObjectBecomesStaleThenItIsRemoved() { var instanceNumber = 0; var disposedObjectsCount = 0; var testPool = new ObjectsPool <PooledObject>( new PooledItemManager <PooledObject>( () => Task.FromResult(new PooledObject(++instanceNumber)), item => { disposedObjectsCount++; return(Task.CompletedTask); }), _poolsCleaner, TimeSpan.FromSeconds(1), _testDateTimeProvider, new LoggerStub(), 2); var fixedTime = DateTime.Now; _testDateTimeProvider.FixDateTimeAt(fixedTime); var firstObject = await testPool.Acquire(); fixedTime += TimeSpan.FromMilliseconds(500); _testDateTimeProvider.FixDateTimeAt(fixedTime); var secondObject = await testPool.Acquire(); testPool.Release(firstObject); fixedTime += TimeSpan.FromMilliseconds(500); _testDateTimeProvider.FixDateTimeAt(fixedTime); testPool.Release(secondObject); fixedTime += TimeSpan.FromMilliseconds(500); _testDateTimeProvider.FixDateTimeAt(fixedTime); Thread.Sleep(500); Assert.Equal(1, disposedObjectsCount); var freeObject = await testPool.Acquire(); Assert.NotNull(freeObject); Assert.Equal(2, freeObject.InstanceNumber); }
public void SetCount(int count) { _shownImages?.ForEach(image => _itemsPool.Release(image)); var spritesToShow = SelectSprites(count); _shownImages = spritesToShow.Select(sprite => { var item = _itemsPool.Borrow(); item.sprite = sprite; item.transform.SetParent(_container); return(item); }).ToList(); }
protected virtual void Dispose(bool disposing) { if (Interlocked.CompareExchange(ref _disposed, 1, 0) != 0) { return; } if (!disposing) { return; } _channel.Dispose(); _redisPipelinePool.Release(_channel); }
public async Task WhenAllObjectsAreLockedAndMaxPoolCapacityIsReachedThenCallerThreadIsLocked() { var instanceNumber = 0; var testPool = new ObjectsPool <PooledObject>( new PooledItemManager <PooledObject>( () => Task.FromResult(new PooledObject(++instanceNumber)), item => Task.CompletedTask), _poolsCleaner, TimeSpan.MaxValue, _testDateTimeProvider, new LoggerStub(), 1); var testObject = await testPool.Acquire(); Assert.NotNull(testObject); Assert.Equal(1, testObject.InstanceNumber); Assert.False(Task.WaitAll(new Task[] { testPool.Acquire() }, 500)); testPool.Release(testObject); }
public async Task WhenFirstObjectIsCreatedAfterFirstCallWhenObjectIsReleasedThenSameObjectIsReturnedOnSecondCall() { var instanceNumber = 0; var testPool = new ObjectsPool <PooledObject>( new PooledItemManager <PooledObject>( () => Task.FromResult(new PooledObject(++instanceNumber)), item => Task.CompletedTask), _poolsCleaner, TimeSpan.MaxValue, _testDateTimeProvider, new LoggerStub(), 1); var testObject = await testPool.Acquire(); Assert.NotNull(testObject); Assert.Equal(1, testObject.InstanceNumber); testPool.Release(testObject); var secondTestObject = await testPool.Acquire(); Assert.Equal(1, testObject.InstanceNumber); Assert.Equal(testObject, secondTestObject); Assert.True(ReferenceEquals(testObject, secondTestObject)); }