Ejemplo n.º 1
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.º 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
        public GSRequest GetPending(String requestId)
        {
            GSRequest toReturn = null;

            _pendingRequests.TryGetValue(requestId, out toReturn);

            return(toReturn);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Send the given request durable.
        /// Durable requests are persisted automatically.
        /// If it cannot be send right now the sdk will try to send it later.
        /// </summary>
        private void SendDurable(GSRequest request)
        {
            request.AddString("requestId", "d_" + DateTime.Now.Ticks + "_" + (_requestCounter++));

            _persistantQueue.AddLast(request);

            _durableQueueDirty = true;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Removes a request from the durable queue. This will prevent the SDK from further trying to send this request.
        /// </summary>
        /// <param name="requestToRemove"></param>
        /// <returns></returns>
        public bool RemoveDurableQueueEntry(GSRequest requestToRemove)
        {
            lock (_persistantQueue)
            {
                _durableQueueDirty = _persistantQueue.Remove(requestToRemove);

                return(_durableQueueDirty);
            }
        }
Ejemplo n.º 6
0
        internal void SendImmediate(GSRequest request)
        {
            if (_WebSocketClient != null)
            {
                lock (_WebSocketClient)
                {
                    if (_WebSocketClient != null)
                    {
                        if (!request.Type.Equals(".AuthenticatedConnectRequest"))
                        {
                            if (request.GetString("requestId") == null)
                            {
                                request.AddString("requestId", DateTime.Now.Ticks + "_" + (_gs._requestCounter++));
                            }

                            //if (request.MaxResponseTimeInMillis != _gs.RequestTimeout) {
                            //	request.AddNumber ("timeout", request.MaxResponseTimeInMillis);
                            //}

                            lock (_pendingRequests) {
                                _pendingRequests.Add(request.GetString("requestId"), request);
                            }
                        }

                        String requestJson = request.JSON;

                        _gSPlatform.DebugMsg("SEND:" + requestJson);

                        //Wrap it in a secure request
                        if (_gs.GSPlatform.ApiSecret.Contains(":") && SessionId != null)
                        {
                            GSRequestData secureRequest = new GSRequestData();

                            secureRequest.AddString("json", requestJson);
                            secureRequest.AddString("hmac", _gs.GSPlatform.MakeHmac(requestJson, _gs.GSPlatform.ApiSecret + "-" + SessionId));

                            requestJson = secureRequest.JSON;
                        }

                        if (_gs.GSPlatform.ApiSecret.Contains(":"))
                        {
                            requestJson = Encrypt(requestJson);
                        }

                        if (_gs.TraceMessages)
                        {
                            _gSPlatform.DebugMsg("SOCKET-SEND:" + requestJson);
                        }

                        _WebSocketClient.Send(requestJson);
                    }
                }
            }
        }
Ejemplo n.º 7
0
        public GSRequest GetAndRemovePending(String requestId)
        {
            GSRequest toReturn = null;

            _pendingRequests.TryGetValue(requestId, out toReturn);

            if (toReturn != null)
            {
                _pendingRequests.Remove(requestId);
            }

            return(toReturn);
        }
Ejemplo n.º 8
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.º 9
0
        internal void Send(Action <GSObject> callback)
        {
            _callback = callback;
            GSRequest requestToSend = this.DeepCopy();

            if (gsInstance != null)
            {
                gsInstance.Send(requestToSend);
            }
            else
            {
                GS.Instance.Send(requestToSend);
            }
        }
Ejemplo n.º 10
0
        private GSRequest DeepCopy()
        {
            var request = new GSRequest(this._data);

            request.Durable                 = this.Durable;
            request._callback               = _callback;
            request._completer              = _completer;
            request._errorCallback          = _errorCallback;
            request._response               = _response;
            request.MaxResponseTimeInMillis = this.MaxResponseTimeInMillis;
            request.WaitForResponseTicks    = this.WaitForResponseTicks;
            request.RequestExpiresAt        = this.RequestExpiresAt;
            request.DurableAttempts         = this.DurableAttempts;

            return(request);
        }
Ejemplo n.º 11
0
        internal void Send(Action <GSObject> successCallback, Action <GSObject> errorCallback)
        {
            _callback      = successCallback;
            _errorCallback = errorCallback;

            GSRequest requestToSend = this.DeepCopy();

            if (gsInstance != null)
            {
                gsInstance.Send(requestToSend);
            }
            else
            {
                GS.Instance.Send(requestToSend);
            }
        }
Ejemplo n.º 12
0
        private void ProcessPersistantQueue()
        {
            int numDurableRequestsProcessed = 0;

            if (!DurableQueueRunning || _durableQueuePaused || _persistantQueue.Count == 0 || _connections.Count == 0 ||
                !Available || _durableDrainTimer >= DateTime.Now.Ticks)
            {
                return;
            }

            _durableDrainTimer = DateTime.Now.AddMilliseconds(DurableDrainInterval).Ticks;

            for (LinkedListNode <GSRequest> node = _persistantQueue.First; node != _persistantQueue.Last.Next; node = node.Next)
            {
                GSRequest request = node.Value;

                if (request.RequestExpiresAt >= DateTime.Now.Ticks)
                {
                    numDurableRequestsProcessed++;
                }
            }

            for (LinkedListNode <GSRequest> node = _persistantQueue.First; node != _persistantQueue.Last.Next; node = node.Next)
            {
                GSRequest request = node.Value;

                if (numDurableRequestsProcessed >= DurableConcurrentRequests || _durableDrainTimer < DateTime.Now.Ticks)
                {
                    return;
                }

                if (request.RequestExpiresAt == 0 || request.RequestExpiresAt < DateTime.Now.Ticks)
                {
                    request.WaitForResponseTicks = DateTime.Now.AddMilliseconds(request.GetResponseTimeout()).Ticks;

                    request.DurableAttempts++;

                    request.RequestExpiresAt = DateTime.Now.AddMilliseconds(request.GetResponseTimeout() + ComputeSleepPeriod(request.DurableAttempts)).Ticks;

                    _connections [0].SendImmediate(request);

                    numDurableRequestsProcessed++;
                }
            }
        }
Ejemplo n.º 13
0
        private void ProcessSendQueue()
        {
            while (_sendQueue.Count > 0)
            {
                GSRequest request = _sendQueue.First.Value;

                if (request.WaitForResponseTicks < DateTime.Now.Ticks)
                {
                    _sendQueue.Remove(request);

                    CancelRequest(request);

                    continue;
                }

                if (_connections.Count > 0 && _connections[0].Ready && Available)
                {
                    try
                    {
                        _connections[0].SendImmediate(request);

                        if (_sendQueue.Contains(request))
                        {
                            _sendQueue.Remove(request);
                        }
                    }
                    catch {
                        return;
                    }
                }
                else
                {
                    return;
                }
            }
        }
Ejemplo n.º 14
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.º 15
0
        /// <summary>
        /// Send the given request.
        /// </summary>
        public void Send(GSRequest request)
        {
            AddRequestedAction(() => {
                if (request.Durable)
                {
                    SendDurable(request);

                    return;
                }

                lock (_connections) {
                    if (_connections.Count == 0)
                    {
                        NewConnection();
                    }
                }

                request.WaitForResponseTicks = DateTime.Now.AddMilliseconds(request.GetResponseTimeout()).Ticks;

                lock (_sendQueue) {
                    _sendQueue.AddLast(request);
                }
            });
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Send the given request durable.
        /// Durable requests are persisted automatically.
        /// If it cannot be send right now the sdk will try to send it later.
        /// </summary>
        public static void SendDurable(GSRequest request)
        {
            request.Durable = true;

            Instance.Send(request);
        }
Ejemplo n.º 17
0
 protected GSTypedRequest(String type)
 {
     request = new GSRequest(type);
 }
Ejemplo n.º 18
0
 internal GSTypedRequest(IDictionary <string, object> data)
 {
     request = new GSRequest(data);
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Create a new Request of the given type.
 /// </summary>
 /// <param name="instance">The GSInstance this request will be send with</param>
 /// <param name="type">Request type name</param>
 public GSTypedRequest(GSInstance instance, string type)
 {
     request = new GSRequest(instance, type);
 }
Ejemplo n.º 20
0
 public void RemovePending(GSRequest toRemove)
 {
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Send the given request.
 /// </summary>
 public static void Send(GSRequest request)
 {
     Instance.Send(request);
 }