Exemple #1
0
        public async Task StoreLocalClientAsync(LocalClient client)
        {
            await _asyncLockers.ClientsOfNodeOperationLocker.WaitAsync();

            try
            {
                if (_dic.ContainsKey(client.SessionId))
                {
                    _dic[client.SessionId] = client;
                }
                else
                {
                    _dic.Add(client.SessionId, client);
                }

                var clientInfo = _dic.Select(x => new ClientInfo
                {
                    HashCode  = x.Value.GetHashCode(),
                    ServerId  = _serverIdentifier.GetIdentifier(),
                    SessionId = x.Key
                });
                await _distributedCache.SetStringAsync(_pomeliumOptions.ClientsCachingPrefix + _serverIdentifier.GetIdentifier(), JsonConvert.SerializeObject(clientInfo));
            }
            finally
            {
                _asyncLockers.ClientsOfNodeOperationLocker.Release();
            }
        }
        protected virtual async void TimeIntervalCallBack(object state)
        {
            if (_lock)
            {
                return;
            }
            _lock        = true;
            _needCollect = false;
            try
            {
                var json = await _distributedCache.GetStringAsync(_pomeliumOptions.NodeCachingPrefix) ?? "[]";

                _nodeInfo = new HashSet <NodeInfo>(JsonConvert.DeserializeObject <IEnumerable <NodeInfo> >(json));
                if (!_nodeInfo.Any(x => x.ServerId == _serverIdentifier.GetIdentifier()))
                {
                    _nodeInfo.Add(new NodeInfo
                    {
                        AddressList = (await DnsResolutionAsync(_pomeliumOptions.Address)).Select(x => x.ToString()),
                        Port        = _pomeliumOptions.Port,
                        ServerId    = _serverIdentifier.GetIdentifier()
                    });
                    await _distributedCache.SetStringAsync(_pomeliumOptions.NodeCachingPrefix, JsonConvert.SerializeObject(_nodeInfo));
                }
                Parallel.ForEach(_nodes.Where(x => !_nodeInfo.Any(y => y.ServerId == x.Key)), x =>
                {
                    Node tmp;
                    _nodes.TryRemove(x.Key, out tmp);
                    _needCollect = true;
                });
                Parallel.ForEach(_nodeInfo.Where(x => !_nodes.Any(y => y.Key == x.ServerId)), x =>
                {
                    if (x.ServerId != _serverIdentifier.GetIdentifier())
                    {
                        try
                        {
                            _nodes.AddOrUpdate(x.ServerId, new Node(x, _serverIdentifier, _semaphoreProvider), (y, z) => z);
                        }
                        catch
                        {
                            _nodeInfo.Remove(x);
                            lock (this)
                            {
                                _distributedCache.SetStringAsync(_pomeliumOptions.NodeCachingPrefix, JsonConvert.SerializeObject(_nodeInfo));
                            }
                        }
                    }
                });
                if (_needCollect)
                {
                    GC.Collect();
                }
            }
            finally
            {
                _lock = false;
            }
        }
Exemple #3
0
        protected virtual async void CollectCallback(object state)
        {
            if (_collectLock)
            {
                return;
            }
            _collectLock = true;
            try
            {
                var garbages = (await GetGarbagesAsync()).Where(x => x.CollectServer == _serverIdentifier.GetIdentifier() && DateTime.Now >= x.ExpireTime);
                foreach (var x in garbages)
                {
                    try
                    {
                        // Clean group member
                        foreach (var g in await _clientCollection.GetJoinedGroupAsync(x.SessionId))
                        {
                            await _clientCollection.RemoveClientFromGroupAsync(x.SessionId, g);
                        }
                        await _distributedCache.RemoveAsync(_pomeliumOptions.ClientJoinedGroupsCachingPrefix + x.SessionId);

                        // Clean session
                        foreach (var s in await _session.GetKeysAsync(x.SessionId))
                        {
                            await _session.RemoveAsync(x.SessionId, s);
                        }
                        await _distributedCache.RemoveAsync(_pomeliumOptions.ClientOwnedSessionKeysCachingPrefix + x.SessionId);
                    }
                    finally
                    {
                    }
                }
            }
            finally
            {
                _collectLock = false;
            }
        }
Exemple #4
0
        public virtual async Task <object> InvokeAsync(Guid sessionId, string method, object[] args)
        {
            var requestId = _semaphoreProvider.Create();
            await _tcpClient.SendAsync(new Packet
            {
                SessionId = _serverIdentifier.GetIdentifier(),
                Type      = PacketType.Forward,
                RequestId = requestId,
                Arguments = new object[]
                {
                    new Packet
                    {
                        Type      = PacketType.Request,
                        SessionId = sessionId,
                        Method    = method,
                        Arguments = args,
                        RequestId = Guid.NewGuid()
                    }
                }
            });

            return(_semaphoreProvider.GetTaskById(requestId));
        }