コード例 #1
0
        public ClientWebSocketManager Get()
        {
            //增加池大小
            Task.Factory.StartNew(() => {
                if (_currentPoolSize < MaxPoolSize && _items.Count == _currentPoolSize && _items.Values.Where(i => !i.IsDisposing).All(i => i.SendingCount > 0))
                {
                    //increase pool
                    _currentPoolSize = Math.Min(_currentPoolSize + _poolSizeIncreaseStep, MaxPoolSize);
                }
            });

            (int ID, int SendingCount)randomItem = (_rnd.Next() % _currentPoolSize, 0);

            ClientWebSocketManager client = null;

            for (var x = _items.Count; x >= 0; x--)
            {
                var key = _items.Values.Select(i => (i.ID, i.SendingCount)).Concat(new[] { randomItem }).OrderBy(v => v.SendingCount).ThenBy(v => _rnd.Next()).First().ID;//.Where(v => !v.IsDisposing)//client.SendingCount最小

                _logger.LogDebug($"get or add socket client:{key}");

                client = _items.GetOrAdd(key, k => {
                    var item = new ClientWebSocketManager(_loggerFactory, _targetHost)
                    {
                        ID = k
                    };
                    item.OnDispose += Item_OnDispose;
                    item.Start();

                    return(item);
                });

                if (!client.IsDisposing)
                {
                    break;
                }

                client = null;//状态不可用
            }

            return(client);
        }
コード例 #2
0
 public void Dispose(ClientWebSocketManager item)
 {
     _items.TryRemove(item.ID, out _);
     item.OnDispose -= Item_OnDispose;
     item.Dispose();
 }