private IEnumerator Coro_Send()
        {
            while (requestQueue.Count > 0)
            {
                var reqData = requestQueue.Dequeue();

                request = CreateReq(reqData);

                // Send request to server.
                yield return(request.Send());

                // TODO: This handles to execute agent priority requests based on verified status code
                // Example: when request token is expired, so there must be a priority request to refresh token to be executed first
                // and then continue the other requests again that has issue with expired token
                if (priorityRequestForStatusCode != null)
                {
                    var pReq = priorityRequestForStatusCode(request.ResponseCode);

                    if (pReq != null && pReq.reqData != null)
                    {
                        if (request != null)
                        {
                            request.Destroy();
                            request = null;
                        }

                        var leftReqData = requestQueue.ToArray();
                        requestQueue.Clear();

                        // Add the request that has problem based on checked status code above
                        requestQueue.Enqueue(reqData);

                        for (int i = 0; i < leftReqData.Length; i++)
                        {
                            requestQueue.Enqueue(leftReqData[i]);
                        }

                        yield return(Coro_ExecutePriorityRequest(pReq));

                        continue;
                    }
                }

                if (request.IsError)
                {
                    if (onError != null)
                    {
                        onError(request);
                    }
                }
                else
                {
                    if (request.IsSuccess)
                    {
                        if (onSuccess != null)
                        {
                            onSuccess(request);
                        }
                    }
                    else
                    {
                        if (onFailure != null)
                        {
                            onFailure(request);
                        }
                    }
                }

                if (onFinishEachRequest != null)
                {
                    onFinishEachRequest(request);
                }

                if (request != null)
                {
                    request.Destroy();
                    request = null;
                }
            }

            if (onFinish != null)
            {
                onFinish();
            }

            // Tell that the instance of this class is being destroyed.
            isDestroying = true;

            yield return(0);

            // Destroy in 0.01 sec
            Destroy(gameObject, 0.01f);
        }
        private IEnumerator Coro_Send()
        {
            HttpRequest httpReq  = null;
            HttpRequest pHttpReq = null;

            while (requestQueue.Count > 0)
            {
                httpReq     = requestQueue.Dequeue();
                requestNode = CreateReq(httpReq.requestData);

                // Send request to server.
                yield return(requestNode.Send());

                // TODO: This handles to execute agent priority requests based on verified status code
                // Example: when request token is expired, so there must be a priority request to refresh token to be executed first
                // and then continue the other requests again that has issue with expired token
                if (priorityRequestForStatusCode != null)
                {
                    pHttpReq = priorityRequestForStatusCode(requestNode);

                    if (pHttpReq != null && pHttpReq.requestData != null)
                    {
                        if (requestNode != null)
                        {
                            requestNode.Destroy();
                            requestNode = null;
                        }

                        HttpRequest[] leftHttpReqs = requestQueue.ToArray();
                        requestQueue.Clear();

                        // Add the request that has problem based on checked status code above
                        requestQueue.Enqueue(httpReq);

                        for (int i = 0; i < leftHttpReqs.Length; i++)
                        {
                            requestQueue.Enqueue(leftHttpReqs[i]);
                        }

                        yield return(Coro_ExecutePriorityRequest(pHttpReq));

                        leftHttpReqs = null;
                        httpReq      = null;
                        pHttpReq     = null;

                        continue;
                    }

                    pHttpReq = null;
                }

                if (requestNode.IsNetworkError)
                {
                    if (onError != null)
                    {
                        onError(requestNode);
                    }

                    if (httpReq.onError != null)
                    {
                        httpReq.onError(requestNode);
                    }
                }
                else
                {
                    if (requestNode.IsSuccess)
                    {
                        if (onSuccess != null)
                        {
                            onSuccess(requestNode);
                        }

                        if (httpReq.onSuccess != null)
                        {
                            httpReq.onSuccess(requestNode);
                        }
                    }
                    else
                    {
                        if (onFailure != null)
                        {
                            onFailure(requestNode);
                        }

                        if (httpReq.onFailure != null)
                        {
                            httpReq.onFailure(requestNode);
                        }
                    }
                }

                if (onFinishEachRequest != null)
                {
                    onFinishEachRequest(requestNode);
                }

                if (httpReq.onFinish != null)
                {
                    httpReq.onFinish(requestNode);
                }

                if (requestNode != null)
                {
                    requestNode.Destroy();
                    requestNode = null;
                }

                httpReq  = null;
                pHttpReq = null;
            }

            if (onFinish != null)
            {
                onFinish();
            }

            if (destroyWhenDone)
            {
                // Tell that the instance of this class is being destroyed.
                isDestroying = true;
                isPending    = false;

                yield return(new WaitForEndOfFrame());

                // Destroy in 0.01 sec
                Destroy(gameObject, 0.01f);
            }
            else
            {
                isPending = false;
            }

            pendingCoroutine = null;
        }
        private IEnumerator Coro_ExecutePriorityRequest(PriorityRequest newReq)
        {
            priorityRequests.Push(newReq);

            while (priorityRequests.Count > 0)
            {
                var req = priorityRequests.Pop();

                request = CreateReq(req.reqData);

                yield return(request.Send());

                if (priorityRequestForStatusCode != null)
                {
                    var pReq = priorityRequestForStatusCode(request.ResponseCode);

                    if (pReq != null && pReq.reqData != null)
                    {
                        if (request != null)
                        {
                            request.Destroy();
                            request = null;
                        }

                        priorityRequests.Push(pReq);
                        priorityRequests.Push(req);

                        continue;
                    }
                }

                if (request.IsError)
                {
                    if (req.onError != null)
                    {
                        req.onError(request);
                    }
                }
                else
                {
                    if (request.IsSuccess)
                    {
                        if (req.onSuccess != null)
                        {
                            req.onSuccess(request);
                        }
                    }
                    else
                    {
                        if (req.onFailure != null)
                        {
                            req.onFailure(request);
                        }
                    }
                }

                if (request != null)
                {
                    request.Destroy();
                    request = null;
                }
            }
        }
        private IEnumerator Coro_ExecutePriorityRequest(HttpRequest newReq)
        {
            HttpRequest httpReq  = null;
            HttpRequest pHttpReq = null;

            priorityRequests.Push(newReq);

            while (priorityRequests.Count > 0)
            {
                httpReq     = priorityRequests.Pop();
                requestNode = CreateReq(httpReq.requestData);

                yield return(requestNode.Send());

                if (priorityRequestForStatusCode != null)
                {
                    pHttpReq = priorityRequestForStatusCode(requestNode);

                    if (pHttpReq != null && pHttpReq.requestData != null)
                    {
                        if (requestNode != null)
                        {
                            requestNode.Destroy();
                            requestNode = null;
                        }

                        priorityRequests.Push(pHttpReq);
                        priorityRequests.Push(httpReq);

                        httpReq  = null;
                        pHttpReq = null;
                        continue;
                    }

                    pHttpReq = null;
                }

                if (requestNode.IsNetworkError)
                {
                    if (httpReq.onError != null)
                    {
                        httpReq.onError(requestNode);
                    }
                }
                else
                {
                    if (requestNode.IsSuccess)
                    {
                        if (httpReq.onSuccess != null)
                        {
                            httpReq.onSuccess(requestNode);
                        }
                    }
                    else
                    {
                        if (httpReq.onFailure != null)
                        {
                            httpReq.onFailure(requestNode);
                        }
                    }
                }

                if (httpReq.onFinish != null)
                {
                    httpReq.onFinish(requestNode);
                }

                if (requestNode != null)
                {
                    requestNode.Destroy();
                    requestNode = null;
                }

                httpReq  = null;
                pHttpReq = null;
            }
        }