コード例 #1
0
ファイル: ZConnection.cs プロジェクト: GVNCoder/Zlo4NET
        // https://stackoverflow.com/questions/19415646/should-i-avoid-async-void-event-handlers
        private async void _OnClientConnectionStateChangedCallback(bool clientConnectionState)
        {
            // lock current execution context
            await _semaphore.WaitAsync();

            // due to the peculiarities of the work of the old and new client
            // where the old one, in case of an unauthorized state, generates two events of changing the state of the connection at once
            // the first is positive, and the second, negative
            // while a negative one is generated after receiving the first request (which is a request to obtain data about an authorized user)
            // the new client, in an unauthorized state, generates only one event, negative

            // so the code is structured to be compatible with these two situations

            // to solve this problem, the concept of an internal state was introduced (there are three of them at once)
            // no decisions about the state of the connection will be made until the incoming state is different from the previous one
            if (_internalConnectionState == null || _internalConnectionState.Value != clientConnectionState)
            {
                var isAuthorized = false;

                // so, if we have connected to the host, this does not mean that the connection is active (for example, the user has not yet logged into the ZClient)
                // therefore, before we say in the affirmative that the connection has been established, we will try to get an authorized user
                if (clientConnectionState)
                {
                    var userRequest = ZRequestFactory.CreateUserInfoRequest();
                    var response    = await ZRouter.GetResponseAsync(userRequest);

                    if (response.StatusCode == ZResponseStatusCode.Ok)
                    {
                        _currentUserInfo = _ParseUserInfo(response.ResponsePackets);
                        _pingTimer.Start();

                        isAuthorized = true;
                    }
                    else
                    {
                        _logger.Warning($"Request failed {userRequest}");
                    }

                    // set current connection state
                    _internalConnectionState = isAuthorized;
                }
                else
                {
                    // reset internal connection state
                    _internalConnectionState = null;
                    _currentUserInfo         = null;

                    _pingTimer.Stop();
                }

                // ReSharper disable once InvertIf
                if (_raiseOnConnectionChangedEvent)
                {
                    _RaiseOnConnectionChangedEvent(IsConnected, _currentUserInfo);
                    _raiseOnConnectionChangedEvent = true;
                }
            }

            _semaphore.Release();
        }
コード例 #2
0
        private void _PlayerListActionHandler(ZServerBase model)
        {
            var target = _collectionWrapper.Collection.FirstOrDefault(s => s.Id == model.Id);

            if (target != null)
            {
                ZSynchronizationWrapper.Send <object>(o => _changesMapper.MapCollection(model.Players, target.Players));

                target.CurrentPlayersNumber = model.CurrentPlayersNumber;
                target.UpdateByName("CurrentPlayersNumber");
            }
            else
            {
                _logger.Warning($"Parsed players for server id: {model.Id} not found this server.", true);
            }
        }
コード例 #3
0
ファイル: _GameStateParser.cs プロジェクト: GVNCoder/Zlo4NET
        public static _GameState ParseStates(string rawEvent, string rawState)
        {
            // get pipe game event enum value
            _events.TryGetValue(rawEvent, out var pipeEvent);

            var resultState = new _GameState
            {
                RawEvent = rawEvent,
                RawState = rawState,
                Event    = pipeEvent,
                States   = CollectionHelper.GetEmptyEnumerable <ZGameState>().ToArray()
            };

            // select handle paths
            switch (pipeEvent)
            {
            case ZGameEvent.StateChanged:

                var endOfStatesIndex = rawState.IndexOfAny("0123456789".ToCharArray());

                // Some states may not have numbers, so we need to handle this situation
                endOfStatesIndex = endOfStatesIndex != -1 ? endOfStatesIndex : rawState.Length;

                var stateString = rawState.Substring(0, endOfStatesIndex);
                var splitStates = stateString.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                var states      = splitStates.Select(state =>
                {
                    // get game state enum value
                    _states.TryGetValue(state, out var stateEnum);

                    return(stateEnum);
                }).ToArray();

                resultState.States = states;

                break;

            case ZGameEvent.Alert:
                break;

            case ZGameEvent.GameWaiting:
                break;

            case ZGameEvent.Unknown:
            default:

                _log.Warning($"{nameof(_GameStateParser)} event doesn't match ({rawEvent} {rawState})");

                break;
            }

            return(resultState);
        }
コード例 #4
0
        public async Task <ZInstalledGames> GetInstalledGamesAsync()
        {
            ZInstalledGames installedGames = null;

            var request  = ZRequestFactory.CreateInstalledGamesRequest();
            var response = await ZRouter.GetResponseAsync(request);

            if (response.StatusCode != ZResponseStatusCode.Ok)
            {
                _logger.Warning($"Request fail {request}");
            }
            else
            {
                var responsePacket = response.ResponsePackets.Single();
                installedGames = _installedGamesParser.Parse(responsePacket);
            }

            return(installedGames);
        }