Пример #1
0
        /// <summary>
        /// Internally handled event for when a connection or reconnection is made
        /// re-subscribes to topics on retry success
        /// </summary>
        private void OnInternalConnected()
        {
            _currentState = ConnectionStates.Connected;

            //If this connection was caused by a reconnect, resubscribe to all subscriptions silently
            if (_CurrentErrorState != ErrorStates.Ok)
            {
                Subscribe(_currentlySubscribed, (topics) =>
                {
                    _currentlySubscribed.ForEach((topic) =>
                    {
                        var currT = topics.Find(t => t.GetHashCode() == topic.GetHashCode());
                        if (currT != null)
                        {
                            return;
                        }
                        //Topic couldn't resubscribe
                        OnTopicResubscriptionFailed?.Invoke(topic);
                        _currentlySubscribed.Remove(topic);
                    });
                }, null);
            }

            _CurrentErrorState = ErrorStates.Ok;

            OnConnected?.Invoke();
        }
Пример #2
0
        /// <summary>
        /// Internal Handler for connection failures
        /// Initiates retry & back-off states
        /// Note: OnConnectionError only fires if we cannot retry after 15 minutes.
        /// </summary>
        /// <param name="error"></param>
        private void OnInternalConnectionError(PlayFabError error)
        {
            _currentState = ConnectionStates.ConnectionFailed;
            switch (_CurrentErrorState)
            {
            case ErrorStates.Ok:
                _CurrentErrorState = ErrorStates.Retry30S;
                OnRetryConnection?.Invoke((float)_CurrentErrorState, "Trying to Reconnect in 30s");
                if (Debugging)
                {
                    Debug.Log("Trying to Reconnect in 30s");
                }
                break;

            case ErrorStates.Retry30S:
                _CurrentErrorState = ErrorStates.Retry5M;
                OnRetryConnection?.Invoke((float)_CurrentErrorState, "Trying to Reconnect in 5m");
                if (Debugging)
                {
                    Debug.Log("Trying to Reconnect in 5m");
                }
                break;

            case ErrorStates.Retry5M:
                _CurrentErrorState = ErrorStates.Retry10M;
                OnRetryConnection?.Invoke((float)_CurrentErrorState, "Trying to Reconnect in 10m");
                if (Debugging)
                {
                    Debug.Log("Trying to Reconnect in 10m");
                }
                break;

            case ErrorStates.Retry10M:
                _CurrentErrorState = ErrorStates.Retry15M;
                OnRetryConnection?.Invoke((float)_CurrentErrorState, "Trying to Reconnect in 15m");
                if (Debugging)
                {
                    Debug.Log("Trying to Reconnect in 15m");
                }
                break;

            case ErrorStates.Retry15M:
                _CurrentErrorState = ErrorStates.Cancelled;
                OnRetryConnection?.Invoke((float)_CurrentErrorState, "Cannot reconnect to server, no more retries");
                OnConnectionError?.Invoke(error);
                if (Debugging)
                {
                    Debug.Log("Cannot reconnect to server, no more retries");
                }
                break;
            }
        }
Пример #3
0
        public ErrorStates CheckConfig()
        {
            ErrorStates state = ErrorStates.None;

            if (TraceMoeToken.Trim().Equals(String.Empty) && BotOwner.Trim().Equals(String.Empty))
            {
                state = ErrorStates.Warning;
            }
            if (DiscordToken.Trim().Equals(String.Empty))
            {
                state = ErrorStates.Error;
            }
            return(state);
        }
Пример #4
0
        public static void Start()
        {
            string fileName = Environment.GetEnvironmentVariable(GsdkConfigFileEnvVarKey);

            if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName))
            {
                _gsdkconfig = _jsonInstance.DeserializeObject <GSDKConfiguration>(File.ReadAllText(fileName));
            }
            else
            {
                Debug.LogError(string.Format("Environment variable {0} not defined", GsdkConfigFileEnvVarKey));
                Application.Quit();
            }

            _baseUrl = string.Format("http://{0}/v1/sessionHosts/{1}/heartbeats", _gsdkconfig.HeartbeatEndpoint, _gsdkconfig.SessionHostId);
            CurrentState.CurrentGameState  = GameState.Initializing;
            CurrentErrorState              = ErrorStates.Ok;
            CurrentState.CurrentPlayers    = new List <ConnectedPlayer>();
            CurrentState.CurrentGameHealth = "Healthy"; // initial state is healthy
            if (_configMap == null)
            {
                _configMap = CreateConfigMap(_gsdkconfig);
            }
            if (IsDebugging)
            {
                Debug.Log(_baseUrl);
                Debug.Log(_gsdkconfig.SessionHostId);
                Debug.Log(_gsdkconfig.LogFolder);
            }

            //Create an agent that can talk on the main-thread and pull on an interval.
            //This is a unity thing, need an object in the scene.

            if (_agentView == null)
            {
                _agentView = new GameObject("PlayFabAgentView");
                _agentView.AddComponent <PlayFabMultiplayerAgentView>();
                UnityEngine.Object.DontDestroyOnLoad(_agentView);
            }
        }
Пример #5
0
        public static void Init()
        {
            _jsonWrapper = PlayFab.PluginManager.GetPlugin <ISerializerPlugin>(PluginContract.PlayFab_Serializer);
            _endpoint    = Environment.GetEnvironmentVariable(HEARTBEAT_ENDPOINT_VARIABLE_NAME);
            sessionId    = Environment.GetEnvironmentVariable(SERVER_ID_VARIABLE_NAME);
            logFolder    = Environment.GetEnvironmentVariable(LOG_FOLDER_VARIABLE_NAME);
            baseURL      = string.Format("http://{0}/v1/sessionHosts/{1}/heartbeats", _endpoint, sessionId);
            CurrentState.currentGameState = SessionHostStatus.Initializing;
            CurrentErrorState             = ErrorStates.Ok;

            if (IsDebugging)
            {
                Debug.Log(baseURL);
                Debug.Log(sessionId);
                Debug.Log(logFolder);
            }

            //Create an agent that can talk on the main-tread and pull on an interval.
            //This is a unity thing, need an object in the scene.
            GameObject agentView = new GameObject("PlayFabAgentView");

            agentView.AddComponent <PlayFabAgentView>();
        }
Пример #6
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;
            });
        }
Пример #7
0
 public void ReadFromStream(MStreamReader sr)
 {
     ErrorState = (ErrorStates)sr.ReadInt32();
 }
Пример #8
0
        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;
            });
        }
Пример #9
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;
                }
            }
        }