Exemple #1
0
        private void ProcessReceivedItem(GSObject response, GSConnection connection)
        {
            string theType = response.Type;

            if (theType.EndsWith("Response"))
            {
                if (theType.Equals(".AuthenticationResponse"))
                {
                    SetUserId(response.GetString("userId"));
                }

                ProcessReceivedResponse(response, connection);
            }
            else if (theType.EndsWith("Message"))
            {
                GSPlatform.ExecuteOnMainThread(() =>
                {
                    try
                    {
                        GSMessageHandler.HandleMessage(this, response);
                    }
                    catch (Exception e)
                    {
                        GSPlatform.DebugMsg(e.ToString());
                    }
                });
            }
        }
Exemple #2
0
        private void SetUserId(string userId)
        {
            // clear the pending durable requests for recent user.
            Log("New UserId init persistent queue " + userId);

            bool previous_durableQueuePaused = _durableQueuePaused;

            //Temporarily stop durable queue processing
            _durableQueuePaused = true;

            //Execute the callback
            GSPlatform.ExecuteOnMainThread(() => {
                GSPlatform.UserId = userId;

                InitialisePersistentQueue();

                //We want this to be callback to the user code to allow them to make decisions
                //about the queue before we start processing it, but after it's been initialised
                if (GameSparksAuthenticated != null)
                {
                    GameSparksAuthenticated(userId);
                }

                //Resume queue processing.
                _durableQueuePaused = previous_durableQueuePaused;
            });
        }
Exemple #3
0
        private void TrimOldConnections()
        {
            List <GSConnection> connectionsToCheck = null;

            lock (_connections) {
                connectionsToCheck = new List <GSConnection> (_connections);
            }

            foreach (GSConnection connection in connectionsToCheck)
            {
                if (connection.PendingRequestCount == 0 && connection._stopped)
                {
                    lock (_connections) {
                        _connections.Remove(connection);
                    }

                    connection.Close();

                    if (TraceMessages)
                    {
                        GSPlatform.DebugMsg("REMOVING CONNECTION");
                    }
                }
            }
        }
Exemple #4
0
 private void Log(string message)
 {
     if (TraceMessages)
     {
         GSPlatform.DebugMsg(message);
     }
 }
Exemple #5
0
        private void SendHandshake(GSObject response, GSConnection connection)
        {
            GSRequest handshakeRequest = new GSRequest(this, "AuthenticatedConnectRequest");

            if (OnGameSparksNonce != null)
            {
                handshakeRequest.AddString("hmac", OnGameSparksNonce(response.GetString("nonce")));
            }
            else
            {
                handshakeRequest.AddString("hmac", GSPlatform.MakeHmac(response.GetString("nonce"), GSPlatform.ApiSecret));
            }

            handshakeRequest.AddString("os", GSPlatform.DeviceOS);
            handshakeRequest.AddString("platform", GSPlatform.Platform);
            handshakeRequest.AddString("deviceId", GSPlatform.DeviceId);

            if (GSPlatform.AuthToken != null && !GSPlatform.AuthToken.Equals("0"))
            {
                handshakeRequest.AddString("authToken", GSPlatform.AuthToken);
            }

            if (_sessionId != null)
            {
                handshakeRequest.AddString("sessionId", _sessionId);
            }

            connection.SendImmediate(handshakeRequest);
        }
Exemple #6
0
        private void setAvailability(bool avail)
        {
            if (_ready != avail)
            {
                _ready = avail;

                if (GameSparksAvailable != null)
                {
                    GSPlatform.ExecuteOnMainThread(() =>
                    {
                        GameSparksAvailable(avail);
                    });
                }
            }
        }
Exemple #7
0
        /// <summary>
        /// Stops all connections, resets the authentication token and establishes a new connection to the service.
        /// </summary>
        public void Reset()
        {
            AddRequestedAction(() => {
                Stop(false);

                _sessionId = null;

                GSPlatform.ExecuteOnMainThread(() => {
                    GSPlatform.AuthToken = "0";
                    GSPlatform.UserId    = "";
                });

                ConnectIfRequired();
            });
        }
Exemple #8
0
        internal void WebSocketClient_OnError(String errorMessage, GSConnection connection)
        {
            if (errorMessage.Contains("INVALID LOCATION"))
            {
                Disconnect();
            }
            else if (errorMessage.Contains("UNKNOWN SERVICE"))
            {
                GSPlatform.DebugMsg("ERROR: UNKNOWN SERVICE");

                Disconnect();
            }

            _currentSocketUrl = BuildServiceUrl(GSPlatform);

            if (TraceMessages)
            {
                GSPlatform.DebugMsg("ERROR:" + errorMessage);
            }
        }
Exemple #9
0
        private void InitAllTimers()
        {
            if (_mainLoopTimer != null)
            {
                _mainLoopTimer.Stop();
                _mainLoopTimer = null;
            }

            _mainLoopTimer = GSPlatform.GetTimer();
            _mainLoopTimer.Initialize(1000 / 60, ProcessQueues);

            if (_durableWriteTimer != null)
            {
                _durableWriteTimer.Stop();
                _durableWriteTimer = null;
            }

            _durableWriteTimer = GSPlatform.GetTimer();
            _durableWriteTimer.Initialize(1000 / 60, WriteDurableQueueIfDirty);
        }
Exemple #10
0
 private void ProcessQueues()
 {
     try
     {
         ExecuteRequestedActions();
         ConnectIfRequired();
         TrimOldConnections();
         ProcessPersistantQueue();
         ProcessSendQueue();
         ProcessPendingQueue();
     }
     catch (Exception e)
     {
         GSPlatform.ExecuteOnMainThread(() =>
         {
             if (TraceMessages)
             {
                 GSPlatform.DebugMsg(e.ToString());
             }
         });
     }
 }
Exemple #11
0
        internal void OnMessageReceived(String message, GSConnection connection)
        {
            GSPlatform.DebugMsg("RECV:" + message);

            GSObject response = GSObject.FromJson(message);

            if (response.ContainsKey("connectUrl"))
            {
                _currentSocketUrl = response.GetString("connectUrl");

                connection.Stop();

                CalcNewReconnectionTimeout(0);

                NewConnection();
            }

            if (response.ContainsKey("authToken"))
            {
                GSPlatform.ExecuteOnMainThread(() =>
                {
                    GSPlatform.AuthToken = response.GetString("authToken");
                });
            }

            if (response.Type != null)
            {
                if (".AuthenticatedConnectResponse".Equals(response.Type))
                {
                    Handshake(response, connection);
                }
                else
                {
                    ProcessReceivedItem(response, connection);
                }
            }
        }
Exemple #12
0
        private void CancelRequest(GSRequest request)
        {
            if (request.Durable)
            {
                return;
            }

            GSObject error = new GSObject("ClientError");

            error.AddObject("error", new GSRequestData().AddString("error", "timeout"));
            error.AddString("requestId", request.GetString("requestId"));

            GSPlatform.ExecuteOnMainThread(() =>
            {
                try
                {
                    request.Complete(this, error);
                }
                catch (Exception e)
                {
                    GSPlatform.DebugMsg(e.ToString());
                }
            });
        }
Exemple #13
0
        private void Handshake(GSObject response, GSConnection connection)
        {
            if (response.ContainsKey("error"))
            {
                GSPlatform.DebugMsg(response.GetString("error"));

                ShutDown(null);
            }
            else if (response.ContainsKey("nonce"))
            {
                SendHandshake(response, connection);
            }
            else
            {
                if (response.ContainsKey("sessionId"))
                {
                    _sessionId = response.GetString("sessionId");

                    connection.SessionId = _sessionId;

                    if (response.ContainsKey("authToken"))
                    {
                        GSPlatform.ExecuteOnMainThread(() =>
                        {
                            GSPlatform.AuthToken = response.GetString("authToken");
                        });
                    }
                    else
                    {
                        GSPlatform.ExecuteOnMainThread(() =>
                        {
                            GSPlatform.AuthToken = "0";
                            GSPlatform.UserId    = "";
                        });
                    }

                    if (response.ContainsKey("clientConfig"))
                    {
                        GSData clientConfig = response.GetGSData("clientConfig");

                        RetryBase                 = clientConfig.GetInt("retryBase").GetValueOrDefault(GS.RetryBase);
                        RetryMax                  = clientConfig.GetInt("retryMax").GetValueOrDefault(GS.RetryMax);
                        RequestTimeout            = clientConfig.GetInt("requestTimeout").GetValueOrDefault(GS.RequestTimeout);
                        DurableConcurrentRequests = clientConfig.GetInt("durableConcurrentRequests").GetValueOrDefault(GS.DurableConcurrentRequests);
                        DurableDrainInterval      = clientConfig.GetInt("durableDrainInterval").GetValueOrDefault(GS.DurableDrainInterval);
                        HandshakeOffset           = clientConfig.GetInt("handshakeOffset").GetValueOrDefault(GS.HandshakeOffset);
                    }
                    else
                    {
                        RetryBase                 = GS.RetryBase;
                        RetryMax                  = GS.RetryMax;
                        RequestTimeout            = GS.RequestTimeout;
                        DurableConcurrentRequests = GS.DurableConcurrentRequests;
                        DurableDrainInterval      = GS.DurableDrainInterval;
                        HandshakeOffset           = GS.HandshakeOffset;
                    }

                    //We want availability to be triggered before authenticated
                    GSPlatform.DebugMsg("Available");

                    connection.Ready = true;

                    setAvailability(true);

                    if (response.ContainsKey("userId"))
                    {
                        SetUserId(response.GetString("userId"));
                    }

                    CalcNewReconnectionTimeout(0);
                }
            }
        }