예제 #1
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;
            });
        }
예제 #2
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());
                    }
                });
            }
        }
예제 #3
0
        private void setAvailability(bool avail)
        {
            if (_ready != avail)
            {
                _ready = avail;

                if (GameSparksAvailable != null)
                {
                    GSPlatform.ExecuteOnMainThread(() =>
                    {
                        GameSparksAvailable(avail);
                    });
                }
            }
        }
예제 #4
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();
            });
        }
예제 #5
0
 private void ProcessQueues()
 {
     try
     {
         ExecuteRequestedActions();
         ConnectIfRequired();
         TrimOldConnections();
         ProcessPersistantQueue();
         ProcessSendQueue();
         ProcessPendingQueue();
     }
     catch (Exception e)
     {
         GSPlatform.ExecuteOnMainThread(() =>
         {
             if (TraceMessages)
             {
                 GSPlatform.DebugMsg(e.ToString());
             }
         });
     }
 }
예제 #6
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);
                }
            }
        }
예제 #7
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());
                }
            });
        }
예제 #8
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);
                }
            }
        }