//This can be called by numerous threads, make sure we wrap the code into an action
        public void OnReady(bool ready)
        {
            if (!this.Ready && ready)
            {
                SendData(OpCodes.PlayerReadyMessage, GameSparksRT.DeliveryIntent.RELIABLE, null, null);
                if (PeerId.HasValue && !ActivePeers.Contains(PeerId.Value))
                {
                    ActivePeers.Add(PeerId.Value);
                }
            }

            this.Ready = ready;

            if (!this.Ready)
            {
                ConnectState = GameSparksRT.ConnectState.Disconnected;
            }


            if (SessionListener != null)
            {
                SubmitAction(ActionCommand.pool.Pop().Configure(
                                 () => {
                    SessionListener.OnReady(ready);
                }));
            }
        }
Ejemplo n.º 2
0
        public async Task RunPeerUpdate()
        {
            lock (_isPeerUpdateInProgressLock)
            {
                if (_isPeerUpdateInProgress)
                {
                    return;
                }

                _isPeerUpdateInProgress = true;
            }

            var key = _perfService.StartPerfCalc();

            var availibleActiveCount = _configurationProvider.ActivePeersMaxCount - _activePeers.Count;

            if (availibleActiveCount <= 0)
            {
                return;
            }

            var candidates = _newPeers.OrderBy(x => x.Value.NodeStats.IsTrustedPeer)
                             .ThenByDescending(x => x.Value.NodeStats.CurrentNodeReputation)
                             .Take(availibleActiveCount).ToArray();

            var newActiveNodes = 0;

            for (var i = 0; i < candidates.Length; i++)
            {
                var candidate = candidates[i];

                _newPeers.TryRemove(candidate.Key, out _);
                if (!_activePeers.TryAdd(candidate.Key, candidate.Value))
                {
                    if (_logger.IsErrorEnabled)
                    {
                        _logger.Error($"Active peer was already added to collection: {candidate.Key.ToString(false)}");
                        continue;
                    }
                }

                var result = await InitializePeerConnection(candidate.Value);

                if (result)
                {
                    newActiveNodes++;
                }
            }

            if (_logger.IsInfoEnabled)
            {
                _logger.Info($"RunPeerUpdate | Tried: {candidates.Length}, Added {newActiveNodes} active peers, current new peers: {_newPeers.Count}, current active peers: {_activePeers.Count}");

                if (_logCounter % 5 == 0)
                {
                    //TODO Change to debug after testing
                    _logger.Info($"\n\nAll active peers: \n{string.Join('\n', ActivePeers.Select(x => $"{x.Node.ToString()} | P2PInitialized: {x.NodeStats.DidEventHappen(NodeStatsEvent.P2PInitialized)} | Eth62Initialized: {x.NodeStats.DidEventHappen(NodeStatsEvent.Eth62Initialized)} | ClientId: {x.NodeStats.NodeDetails.ClientId}"))} \n\n");
                }

                _logCounter++;
            }

            _perfService.EndPerfCalc(key, "RunPeerUpdate");
            _isPeerUpdateInProgress = false;
        }