public void EnqueueGetStatsRequest(PeerBase peer, GetLobbyStatsRequest statsRequest, SendParameters sendParameter)
 {
     this.fiber.Enqueue(() => this.ExecuteGetStatsRequest(peer, statsRequest, sendParameter));
 }
        private void ExecuteGetStatsRequest(PeerBase peer, GetLobbyStatsRequest statsRequest, SendParameters sendParameter)
        {
            try
            {
                OperationResponse errorresponse;
                if (this.ValidateRequest(statsRequest, out errorresponse) == false)
                {
                    peer.SendOperationResponse(errorresponse, sendParameter);
                    return;
                }

                var response = new GetLobbyStatsResponse();
                AppLobby[] lobbies;

                short returnCode = 0;
                string debugMessage = null;

                if (statsRequest.LobbyNames == null)
                {
                    lobbies = this.lobbyFactory.GetLobbies(this.maxLobbyStatsCount);
                    response = LobbyListToLobbyStatsData(lobbies);
                }
                else
                {
                    // Check if lobby stats limit is exceeded.
                    // If Limit is exceeded the complete list will be returned anyway but with the response result code set to -4.
                    // This behaviour might change in future versions.
                    if (this.maxLobbyStatsCount > 0 && statsRequest.LobbyNames.Length > this.maxLobbyStatsCount)
                    {
                        returnCode = (short)ErrorCode.ArgumentOutOfRange;
                        debugMessage = string.Format("LobbyStats limit of {0} exceeded", this.maxLobbyStatsCount);
                    }

                    lobbies = this.lobbyFactory.GetLobbies(statsRequest.LobbyNames, statsRequest.LobbyTypes);

                    var count = lobbies.Length;
                    response.PeerCount = new int[count];
                    response.GameCount = new int[count];

                    for (int i = 0; i < lobbies.Length; i++)
                    {
                        if (lobbies[i] != null)
                        {
                            response.PeerCount[i] = lobbies[i].PeerCount + lobbies[i].PlayerCount;
                            response.GameCount[i] = lobbies[i].GameCount;
                        }
                        else
                        {
                            response.PeerCount[i] = 0;
                            response.GameCount[i] = 0;
                        }
                    }
                }

                var operationResponse = new OperationResponse((byte)OperationCode.LobbyStats, response)
                    {
                        ReturnCode = returnCode,
                        DebugMessage = debugMessage
                    };

                peer.SendOperationResponse(operationResponse, sendParameter);
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
        private bool ValidateRequest(GetLobbyStatsRequest request, out OperationResponse errorResponse)
        {
            errorResponse = null;

            if (request.LobbyNames == null)
            {
                return true;
            }

            if (request.LobbyTypes == null)
            {
                errorResponse = new OperationResponse
                {
                    OperationCode = request.OperationRequest.OperationCode,
                    ReturnCode = (short)ErrorCode.OperationInvalid,
                    DebugMessage = LBErrorMessages.LobbyTypesNotSet,
                };

                return false;
            }

            //if (this.maxLobbyStatsCount > 0 && request.LobbyNames.Length > this.maxLobbyStatsCount)
            //{
            //    errorResponse = new OperationResponse
            //    {
            //        OperationCode = (byte)OperationCode.LobbyStats,
            //        ReturnCode = (short)ErrorCode.ArgumentOutOfRange,
            //        DebugMessage = string.Format("LobbyStats limit of {0}  exceeded", this.maxLobbyStatsCount)
            //    };

            //    return false;
            //}

            if (request.LobbyNames.Length != request.LobbyTypes.Length)
            {
                errorResponse = new OperationResponse
                {
                    OperationCode = request.OperationRequest.OperationCode,
                    ReturnCode = (short)ErrorCode.OperationInvalid,
                    DebugMessage = LBErrorMessages.LobbyTypesLenDoNotMatchLobbyNames,
                };

                return false;
            }

            return true;
        }
        public OperationResponse HandleLobbyStatsRequest(OperationRequest operationRequest, SendParameters sendParameters)
        {
            OperationResponse response;

            var getStatsRequest = new GetLobbyStatsRequest(this.Protocol, operationRequest);
            if (OperationHelper.ValidateOperation(getStatsRequest, log, out response) == false)
            {
                return response;
            }

            this.Application.LobbyStatsPublisher.EnqueueGetStatsRequest(this, getStatsRequest, sendParameters);
            return null;
        }