Ejemplo n.º 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());
                    }
                });
            }
        }
Ejemplo n.º 2
0
        private void ProcessReceivedResponse(GSObject response, GSConnection connection)
        {
            String    requestId = response.GetString("requestId");
            GSRequest request   = connection.GetAndRemovePending(requestId);

            if (request == null)
            {
                return;
            }

            if (request.RequestExpiresAt > 0)
            {
                //It's durable request, if it's a ClientError do nothing as it will be retried
                if (response.ContainsKey("@class") && !response.GetString("@class").Equals("ClientError"))
                {
                    _durableQueueDirty = _persistantQueue.Remove(request);

                    request.Complete(this, response);
                }
            }
            else
            {
                request.Complete(this, response);
            }
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 public void GetFileBytesFromUrl(GSObject getUploadUrlResponse)
 {
     if (getUploadUrlResponse.ContainsKey("url"))
     {
         webRequest = (HttpWebRequest)HttpWebRequest.Create(getUploadUrlResponse.GetString("url"));
         webRequest.BeginGetResponse(new AsyncCallback(ResponseCallback), webRequest);
         if (autoResetEvent.WaitOne(120000))
         {
             getUploadUrlResponse.Add("bytes", responseBytes);
         }
     }
 }
Ejemplo n.º 5
0
        public void Upload(GSObject getUploadUrlResponse)
        {
            GameSparksFormUpload.FileParameter param = new GameSparksFormUpload.FileParameter(file);
            param.FileName = fileName;
            IDictionary <string, object> postParams = new Dictionary <string, object>();

            postParams.Add("file", param);
            if (getUploadUrlResponse.ContainsKey("url"))
            {
                String response = GameSparksFormUpload.MultipartFormDataPost(getUploadUrlResponse.GetString("url"), "GameSparksUploadAPI", postParams);
                getUploadUrlResponse.Add("uploadResponse", GSJson.From(response));
            }
        }
Ejemplo n.º 6
0
        private void CancelRequest(GSConnection connection, 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"));

            ProcessReceivedResponse(error, connection);
        }
Ejemplo n.º 7
0
 internal void Complete(GSInstance gsInstance, GSObject response)
 {
     _response = response;
     if (_completer != null)
     {
         _completer(response);
     }
     if (_errorCallback != null && response.ContainsKey("error"))
     {
         gsInstance.GSPlatform.ExecuteOnMainThread(() =>
         {
             _errorCallback(response);
         });
     }
     else if (_callback != null)
     {
         gsInstance.GSPlatform.ExecuteOnMainThread(() =>
         {
             _callback(response);
         });
     }
 }
Ejemplo n.º 8
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);
                }
            }
        }
Ejemplo n.º 9
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());
                }
            });
        }
Ejemplo n.º 10
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);
                }
            }
        }
Ejemplo n.º 11
0
 protected abstract GSTypedResponse BuildResponse(GSObject response);