Exemplo n.º 1
0
        void ExtractCurrentXiaomiPackageInfo(RequestQueueItem request)
        {
            ListRequest currentPackageListRequest = (ListRequest)request.Request;

            if (currentPackageListRequest.Status == StatusCode.Success)
            {
                System.Console.WriteLine("Current xiaomi package version is " +
                                         (string.IsNullOrEmpty(currentXiaomiPackageVersion)
                        ? "empty"
                        : currentXiaomiPackageVersion));

                if (currentPackageListRequest.IsCompleted && string.IsNullOrEmpty(currentXiaomiPackageVersion))
                {
                    PackageManager.PackageInfo info = currentPackageListRequest.Result.FirstOrDefault(p => p.name == xiaomiPackageName);
                    if (info != null)
                    {
                        currentXiaomiPackageVersion = info.version;
                    }
                    if (!string.IsNullOrEmpty(currentXiaomiPackageVersion))
                    {
                        xiaomiPackageInstalled = true;
                    }
                }
                currentVersionInitialized = true;
            }
            else
            {
                System.Console.WriteLine(currentPackageListRequest + " failed with error: " +
                                         currentPackageListRequest.Error);
            }
        }
Exemplo n.º 2
0
        bool CheckPackmanOperation()
        {
            if (requestList.Count == 0)
            {
                return(true);
            }

            RequestQueueItem requestItem = requestList[0];
            StatusCode       statusCode  = requestItem.Request.Status;

            if (statusCode == StatusCode.Failure)
            {
                Error error = requestItem.Request.Error;
                Debug.LogError("Operation " + requestItem.Request + " failed with Error: " + error);
                return(true);
            }
            else if (statusCode == StatusCode.InProgress)
            {
                return(false);
            }
            else if (statusCode == StatusCode.Success)
            {
                System.Console.WriteLine("Operation " + requestItem.Request + " Done");
                switch (requestItem.OperationType)
                {
                case PackmanOperationType.List:
                    ExtractCurrentXiaomiPackageInfo(requestItem);
                    break;

                case PackmanOperationType.Add:
                    PerformAdd(requestItem);
                    break;

                case PackmanOperationType.Remove:
                    PerformRemove(requestItem.Request.Status);
                    break;

                case PackmanOperationType.Search:
                    ExtractLatestXiaomiPackageInfo(requestItem);
                    break;

                default:
                    System.Console.WriteLine("Type " + requestItem.OperationType + " Not Supported");
                    break;
                }
                requestList.RemoveAt(0);
                if (requestList.Count > 0)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        private void RequestEnqueue(RequestQueueItem item)
        {
            requestQueue.Enqueue(item);

            if (false == isRequestQueueCheck)
            {
                isRequestQueueCheck = true;
                GamebaseUnitySDKSettings.Instance.StartCoroutine(UpdateQueue());
            }
        }
Exemplo n.º 4
0
 void PerformAdd(RequestQueueItem request)
 {
     currentXiaomiPackageVersion = latestXiaomiPackageVersion = ((AddRequest)request.Request).Result.version;
     if (request.Request.Status == StatusCode.Failure)
     {
         Debug.LogError("Adding/Updating to " + latestXiaomiPackageVersion + " resulted in error, please add it again.");
         return;
     }
     xiaomiPackageInstalled = true;
 }
        private void RequestEnqueue(RequestQueueItem item)
        {
            requestQueue.Enqueue(item);

            if (false == isRequestQueueCheck)
            {
                isRequestQueueCheck = true;
                GamebaseCoroutineManager.StartCoroutine(GamebaseGameObjectManager.GameObjectType.WEBSOCKET_TYPE, UpdateQueue());
            }
        }
Exemplo n.º 6
0
        public EnqueueResponse Enqueue(RequestQueueItem queueItem)
        {
            var response = CanEnqueue(queueItem);

            if (response == EnqueueResponse.Ok)
            {
                _apiQueue.Enqueue(queueItem);
                if (queueItem.Endpoint == RequestQueueItem.ApiEndpoint.SearchJobRequest)
                {
                    _searchJobCounter.Increase(1);
                }
            }

            return(response);
        }
Exemplo n.º 7
0
        private void RetryItem()
        {
            RequestQueueItem[] itemList = null;
            if (0 < requestQueue.Count)
            {
                itemList = requestQueue.ToArray();
            }

            requestQueueItem.retryCount += 1;
            RequestEnqueue(requestQueueItem);
            requestQueueItem = null;

            if (null != itemList)
            {
                foreach (var item in itemList)
                {
                    requestQueue.Enqueue(item);
                }
            }
        }
Exemplo n.º 8
0
        private async Task <IActionResult> ProcessRequest(RequestQueueItem.ApiEndpoint endpoint, IActionResult result)
        {
            var queueItem      = new RequestQueueItem(Request, endpoint);
            var enqueuResponse = _requestQueue.Enqueue(queueItem);

            if (enqueuResponse != EnqueueResponse.Ok)
            {
                var response =
                    StatusCode(TooManyRequestHttpCode, $"Too many requests. {enqueuResponse} occured.");
                LogMessage(LogLevel.Error, response);
                return(response);
            }

            await Task.Delay(_random.Next(500, 2000));

            LogMessage(LogLevel.Information, result);
            _requestQueue.Dequeu();

            return(result);
        }
Exemplo n.º 9
0
        private IEnumerator UpdateQueue()
        {
            while (true == isRequestQueueCheck)
            {
                if (0 == requestQueue.Count)
                {
                    isRequestQueueCheck = false;
                    yield return(null);
                }
                else
                {
                    if (null == requestQueueItem)
                    {
                        requestQueueItem = RequestDequeue();
                        yield return(GamebaseUnitySDKSettings.Instance.StartCoroutine(Send()));
                    }

                    yield return(null);
                }
            }
        }
        private IEnumerator UpdateQueue()
        {
            while (true == isRequestQueueCheck)
            {
                if (0 == requestQueue.Count)
                {
                    isRequestQueueCheck = false;
                    yield return(null);
                }
                else
                {
                    if (null == requestQueueItem)
                    {
                        requestQueueItem = RequestDequeue();
                        yield return(GamebaseCoroutineManager.StartCoroutine(GamebaseGameObjectManager.GameObjectType.WEBSOCKET_TYPE, Send()));
                    }

                    yield return(null);
                }
            }
        }
Exemplo n.º 11
0
        private EnqueueResponse CanEnqueue(RequestQueueItem queueItem)
        {
            if (_apiQueue.IsEmpty && _searchJobCounter.Count < MaxSearchJobRequest)
            {
                return(EnqueueResponse.Ok);
            }

            _apiQueue.TryPeek(out var firstItem);
            if (firstItem == null)
            {
                return(EnqueueResponse.Ok);
            }

            var requestedAtUnixTimeSeconds = queueItem.RequestedAt.ToUnixTimeSeconds();
            var firstItemUnixTimeSeconds   = firstItem.RequestedAt.ToUnixTimeSeconds();
            var requestedAtUnixTimeMinutes = Math.Floor(requestedAtUnixTimeSeconds / 60D);
            var firstItemUnixTimeMinutes   = Math.Floor(firstItemUnixTimeSeconds / 60D);

            var itemCount = _apiQueue.Count;

            if (itemCount >= MaxRequestsPerSecond &&
                requestedAtUnixTimeSeconds == firstItemUnixTimeSeconds)
            {
                return(EnqueueResponse.MaxRequestsPerSecondError);
            }

            if (itemCount >= MaxRequestsPerMinute &&
                requestedAtUnixTimeMinutes == firstItemUnixTimeMinutes)
            {
                return(EnqueueResponse.MaxRequestsPerMinuteError);
            }

            if (queueItem.Endpoint == RequestQueueItem.ApiEndpoint.SearchJobRequest &&
                _searchJobCounter.Count >= MaxSearchJobRequest)
            {
                return(EnqueueResponse.MaxSearchJobRequestError);
            }

            return(EnqueueResponse.Ok);
        }
Exemplo n.º 12
0
        private IEnumerator RetryConnect()
        {
            if (requestQueueItem.retryCount < requestQueueItem.retryLimits)
            {
                yield return(new WaitForSecondsRealtime(RETRY_CONNECT_DELAY));

                yield return(GamebaseUnitySDKSettings.Instance.StartCoroutine(InternetReachability((reachable) =>
                {
                    if (true == reachable)
                    {
                        GamebaseUnitySDKSettings.Instance.StartCoroutine(socket.Reconnect((error) =>
                        {
                            if (true == Gamebase.IsSuccess(error))
                            {
                                RequestEnqueue(requestQueueItem);
                                requestQueueItem = null;
                                return;
                            }

                            RetryItem();
                        }));
                    }
                    else
                    {
                        RetryItem();
                    }

                    GamebaseLog.Debug(string.Format("index:{0}, apiId:{1}, retryCount:{2}, internetReachability:{3}", requestQueueItem.index, requestQueueItem.requestVO.apiId, requestQueueItem.retryCount, reachable), this, "RetryConnect");
                })));
            }
            else
            {
                GamebaseLog.Debug(string.Format("Reconnect failed. Index of queue item:{0}", requestQueueItem.index), this, "RetryConnect");
                requestQueueItem.callback(string.Empty, new GamebaseError(GamebaseErrorCode.SOCKET_ERROR));
                requestQueueItem = null;
            }
        }
Exemplo n.º 13
0
        private void RecvEvent(string response)
        {
            GamebaseLog.Debug(response, this, "RecvEvent");

            ProtocolResponse protocol = JsonMapper.ToObject <ProtocolResponse>(response);

            if (null != protocol.header.serverPush)
            {
                ServerPush.Instance.OnServerPush(response);
                return;
            }

            if (protocol.header.transactionId == requestQueueItem.requestVO.transactionId)
            {
                requestQueueItem.callback(response, null);
                isTimeOutCheck   = false;
                requestQueueItem = null;
                socket.SetPollingInterval(PollingIntervalType.LONG_INTERVAL);
            }
            else
            {
                GamebaseLog.Debug(string.Format("Missing response TransactionId : {0}", protocol.header.transactionId.ToString()), this, "RecvEvent");
            }
        }
Exemplo n.º 14
0
        void ExtractLatestXiaomiPackageInfo(RequestQueueItem request)
        {
            SearchRequest latestPackageSearchRequest = (SearchRequest)request.Request;

            if (latestPackageSearchRequest.Status == StatusCode.Success)
            {
                System.Console.WriteLine("Latest xiaomi package version is " +
                                         (string.IsNullOrEmpty(latestXiaomiPackageVersion)
                        ? "empty"
                        : latestXiaomiPackageVersion));

                if (latestPackageSearchRequest.IsCompleted && latestPackageSearchRequest.Result.Length > 0 &&
                    string.IsNullOrEmpty(latestXiaomiPackageVersion))
                {
                    latestXiaomiPackageVersion = latestPackageSearchRequest.Result[0].version;
                }
                latestVersionInitialized = true;
            }
            else
            {
                System.Console.WriteLine(latestPackageSearchRequest + " failed with error: " +
                                         latestPackageSearchRequest.Error);
            }
        }
Exemplo n.º 15
0
        private RequestQueueItem RequestDequeue()
        {
            RequestQueueItem item = requestQueue.Dequeue();

            return(item);
        }