public bool ExecuteCloudScript <TIn, TOut>(string functionName, TIn functionParameter, Dictionary <string, string> extraHeaders, out TOut result, out string errorReport)
        {
            // Perform the request
            var request = new ExecuteCloudScriptRequest
            {
                FunctionName            = functionName,
                FunctionParameter       = functionParameter,
                GeneratePlayStreamEvent = true
            };
            var task = clientApi.ExecuteCloudScriptAsync(request, null, extraHeaders);

            task.Wait();
            errorReport = PlayFabUtil.GetCloudScriptErrorReport(task.Result);

            if (task.Result.Error != null)
            {
                Console.WriteLine("Execute Cloudscript failure: " + functionName + ":\n" + json.SerializeObject(functionParameter));
                Console.WriteLine(errorReport);
                result = default(TOut);
                return(false);
            }

            // Re-serialize as the target type
            var resultJson = json.SerializeObject(task.Result.Result.FunctionResult);

            if (verbose)
            {
                Console.WriteLine("===== Verbose ExecuteCloudScript Json: =====");
                Console.WriteLine(resultJson);
                Console.WriteLine("===== End =====");
            }
            try
            {
                result = json.DeserializeObject <TOut>(resultJson);
            }
            catch (Exception)
            {
                Console.WriteLine("Could not serialize text: \"" + resultJson + "\" as " + typeof(TOut).Name);
                result = default(TOut);
                return(false);
            }
            return(task.Result.Error == null && task.Result.Result.Error == null && (result != null || resultJson == "null"));
        }
Exemple #2
0
        internal static void SendHeartBeatRequest()
        {
            var payload = _jsonWrapper.SerializeObject(CurrentState);

            if (string.IsNullOrEmpty(payload))
            {
                return;
            }
            var payloadBytes = Encoding.ASCII.GetBytes(payload);

            PlayFabHttp.SimplePostCall(baseURL, payloadBytes, (success) => {
                var json = System.Text.Encoding.UTF8.GetString(success);
                Debug.Log(json);
                if (string.IsNullOrEmpty(json))
                {
                    return;
                }
                var hb = _jsonWrapper.DeserializeObject <SessionHostHeartbeatInfo>(json);
                if (hb != null)
                {
                    ProcessAgentResponse(hb);
                }
                CurrentErrorState = ErrorStates.Ok;
                IsProcessing      = false;
            }, (error) => {
                var 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 (OnAgentError != null)
                {
                    OnAgentError.Invoke(error);
                }
                IsProcessing = false;
            });
        }
        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;
            });
        }
 public static string SerializeObject(object json)
 {
     return(_instance.SerializeObject(json));
 }