Example #1
0
        // 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();
        }
Example #2
0
        public async Task <ZRunResult> RunAsync()
        {
            // send request to run the game
            var request  = ZRequestFactory.CreateRunGameRequest(_targetGame.RunnableName, _runArgs);
            var response = await ZRouter.GetResponseAsync(request);

            if (response.StatusCode != ZResponseStatusCode.Ok)
            {
                return(ZRunResult.Error);
            }

            // parse run results
            var runResult = _parser.Parse(response.ResponsePackets.Single());

            // ReSharper disable once InvertIf
            if (runResult == ZRunResult.Success)
            {
                // prepare instance state to track game state
                // begin track
                _pipe            = new _GamePipe(_logger, _pipeName);
                _pipe.PipeEvent += _PipeEventHandler;

                _processTracker.ProcessDetected += _ProcessTrackerOnProcessDetected;
                _processTracker.ProcessLost     += _ProcessTrackerOnProcessLost;

                // begin track process tracker
                _processTracker.StartTrack();
            }

            return(runResult);
        }
Example #3
0
 public async void Inject(ZGame game, IEnumerable <string> dllPaths)
 {
     foreach (var dllPath in dllPaths)
     {
         var request  = ZRequestFactory.CreateDllInjectRequest(game, dllPath);
         var response = await ZRouter.GetResponseAsync(request);
     }
 }
Example #4
0
        private async void _OnStreamRejectedCallback()
        {
            _isStreamRejected = true;

            // close stream
            var request = ZRequestFactory.CreateServerListCloseStreamRequest(_gameContext);
            await ZRouter.CloseStreamAsync(request);
        }
Example #5
0
        public void Connect()
        {
            // already connected ?
            if (IsConnected)
            {
                return;
            }

            ZRouter.Start();
        }
Example #6
0
        public void StartReceiving()
        {
            if (__disposed)
            {
                throw new InvalidOperationException("Object disposed.");
            }

            var openStreamRequest = ZRequestFactory.CreateServerListOpenStreamRequest(_gameContext);
            var response          = ZRouter.OpenStreamAsync(openStreamRequest, _packetsReceivedHandler, _OnStreamRejectedCallback).Result;
        }
Example #7
0
        private async void _OnPingTimerElapsedCallback(object sender, ElapsedEventArgs e)
        {
            // we are playing Ping-Pong with ZClient
            var pingRequest  = ZRequestFactory.CreatePingRequest();
            var pongResponse = await ZRouter.GetResponseAsync(pingRequest);

            // check and set current connection state
            if (pongResponse.StatusCode != ZResponseStatusCode.Ok)
            {
                // burning bridges, completely :)
                Disconnect(true);
            }
        }
Example #8
0
        public void Disconnect(bool raiseEvent = true)
        {
            // already disconnected ?
            if (!IsConnected)
            {
                return;
            }

            // prepare to disconnect
            _raiseOnConnectionChangedEvent = raiseEvent;

            // disconnect
            ZRouter.Stop();
        }
Example #9
0
        private bool?_internalConnectionState       = null; // where [null] - initial state, [true/false] - concrete state

        public ZConnection()
        {
            _userInfoParser = ZParsersFactory.CreateUserInfoParser();
            _logger         = ZLogger.Instance;
            _pingTimer      = new Timer(PING_INTERVAL)
            {
                Enabled = false, AutoReset = true
            };

            _pingTimer.Elapsed += _OnPingTimerElapsedCallback;

            // track client connection state
            ZRouter.Initialize();
            ZRouter.ConnectionChanged += _OnClientConnectionStateChangedCallback;
        }
Example #10
0
        public void Dispose()
        {
            if (__disposed)
            {
                return;
            }

            if (!_isStreamRejected)
            {
                var request  = ZRequestFactory.CreateServerListCloseStreamRequest(_gameContext);
                var response = ZRouter.CloseStreamAsync(request).Result;
            }

            _parser.Close();

            __disposed = true;
        }
        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);
        }
Example #12
0
        public async Task <ZStatsBase> GetStatsAsync(ZGame game)
        {
            ZStatsBase stats = null;

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

            if (response.StatusCode != ZResponseStatusCode.Ok)
            {
                _logger.Error($"Request fail {request}");
            }
            else
            {
                switch (game)
                {
                case ZGame.BF3: stats = _parser.ParseBF3Stats(response.ResponsePackets); break;

                case ZGame.BF4: stats = _parser.ParseBF4Stats(response.ResponsePackets); break;
                }
            }

            return(stats);
        }