internal static void SendHeartBeatRequest()
        {
            string payload = _jsonWrapper.SerializeObject(CurrentState);

            if (string.IsNullOrEmpty(payload))
            {
                return;
            }

            byte[] payloadBytes = Encoding.ASCII.GetBytes(payload);

            if (IsDebugging)
            {
                Debug.Log(payload);
            }

            PlayFabHttp.SimplePostCall(_baseUrl, payloadBytes, success =>
            {
                string json = Encoding.UTF8.GetString(success);
                Debug.Log(json);
                if (string.IsNullOrEmpty(json))
                {
                    return;
                }

                HeartbeatResponse hb = _jsonWrapper.DeserializeObject <HeartbeatResponse>(json);
                if (hb != null)
                {
                    ProcessAgentResponse(hb);
                }

                CurrentErrorState = ErrorStates.Ok;
                IsProcessing      = false;
            }, error =>
            {
                Guid guid = Guid.NewGuid();
                Debug.LogFormat("CurrentError: {0} - {1}", error, guid.ToString());
                //Exponential backoff for 30 minutes for retries.
                switch (CurrentErrorState)
                {
                case ErrorStates.Ok:
                    CurrentErrorState = ErrorStates.Retry30S;
                    if (IsDebugging)
                    {
                        Debug.Log("Retrying heartbeat in 30s");
                    }

                    break;

                case ErrorStates.Retry30S:
                    CurrentErrorState = ErrorStates.Retry5M;
                    if (IsDebugging)
                    {
                        Debug.Log("Retrying heartbeat in 5m");
                    }

                    break;

                case ErrorStates.Retry5M:
                    CurrentErrorState = ErrorStates.Retry10M;
                    if (IsDebugging)
                    {
                        Debug.Log("Retrying heartbeat in 10m");
                    }

                    break;

                case ErrorStates.Retry10M:
                    CurrentErrorState = ErrorStates.Retry15M;
                    if (IsDebugging)
                    {
                        Debug.Log("Retrying heartbeat in 15m");
                    }

                    break;

                case ErrorStates.Retry15M:
                    CurrentErrorState = ErrorStates.Cancelled;
                    if (IsDebugging)
                    {
                        Debug.Log("Agent reconnection cannot be established - cancelling");
                    }

                    break;
                }

                if (OnAgentErrorCallback != null)
                {
                    OnAgentErrorCallback.Invoke(error);
                }

                IsProcessing = false;
            });
        }
Exemple #2
0
        public static IEnumerator SendHeartBeatRequest()
        {
            string payload = _jsonInstance.SerializeObject(CurrentState);

            if (string.IsNullOrEmpty(payload) || string.IsNullOrEmpty(_baseUrl))
            {
                yield break;
            }

            byte[] payloadBytes = Encoding.UTF8.GetBytes(payload);

            if (IsDebugging)
            {
                Debug.Log($"state: {CurrentState}, payload: {payload}");
            }

            using (UnityWebRequest req = new UnityWebRequest(_baseUrl, UnityWebRequest.kHttpVerbPOST))
            {
                req.SetRequestHeader("Accept", "application/json");
                req.SetRequestHeader("Content-Type", "application/json");
                req.downloadHandler = new DownloadHandlerBuffer();
                req.uploadHandler   = new UploadHandlerRaw(payloadBytes)
                {
                    contentType = "application/json"
                };
                yield return(req.SendWebRequest());

                if (req.result == UnityWebRequest.Result.ConnectionError || req.result == UnityWebRequest.Result.ProtocolError)
                {
                    Guid guid = Guid.NewGuid();
                    Debug.LogFormat("CurrentError: {0} - {1}", req.error, guid.ToString());
                    //Exponential backoff for 30 minutes for retries.
                    switch (CurrentErrorState)
                    {
                    case ErrorStates.Ok:
                        CurrentErrorState = ErrorStates.Retry30S;
                        if (IsDebugging)
                        {
                            Debug.Log("Retrying heartbeat in 30s");
                        }

                        break;

                    case ErrorStates.Retry30S:
                        CurrentErrorState = ErrorStates.Retry5M;
                        if (IsDebugging)
                        {
                            Debug.Log("Retrying heartbeat in 5m");
                        }

                        break;

                    case ErrorStates.Retry5M:
                        CurrentErrorState = ErrorStates.Retry10M;
                        if (IsDebugging)
                        {
                            Debug.Log("Retrying heartbeat in 10m");
                        }

                        break;

                    case ErrorStates.Retry10M:
                        CurrentErrorState = ErrorStates.Retry15M;
                        if (IsDebugging)
                        {
                            Debug.Log("Retrying heartbeat in 15m");
                        }

                        break;

                    case ErrorStates.Retry15M:
                        CurrentErrorState = ErrorStates.Cancelled;
                        if (IsDebugging)
                        {
                            Debug.Log("Agent reconnection cannot be established - cancelling");
                        }

                        break;
                    }

                    if (OnAgentErrorCallback != null)
                    {
                        OnAgentErrorCallback.Invoke(req.error);
                    }

                    IsProcessing = false;
                }
                else // success path
                {
                    string json = Encoding.UTF8.GetString(req.downloadHandler.data);
                    if (string.IsNullOrEmpty(json))
                    {
                        yield break;
                    }

                    HeartbeatResponse hb = _jsonInstance.DeserializeObject <HeartbeatResponse>(json);

                    if (hb != null)
                    {
                        ProcessAgentResponse(hb);
                    }

                    CurrentErrorState = ErrorStates.Ok;
                    IsProcessing      = false;
                }
            }
        }