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); }
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); } }
public GSRequest GetPending(String requestId) { GSRequest toReturn = null; _pendingRequests.TryGetValue(requestId, out toReturn); return(toReturn); }
/// <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; }
/// <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); } }
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); } } } }
public GSRequest GetAndRemovePending(String requestId) { GSRequest toReturn = null; _pendingRequests.TryGetValue(requestId, out toReturn); if (toReturn != null) { _pendingRequests.Remove(requestId); } return(toReturn); }
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); }
internal void Send(Action <GSObject> callback) { _callback = callback; GSRequest requestToSend = this.DeepCopy(); if (gsInstance != null) { gsInstance.Send(requestToSend); } else { GS.Instance.Send(requestToSend); } }
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); }
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); } }
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++; } } }
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; } } }
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()); } }); }
/// <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); } }); }
/// <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); }
protected GSTypedRequest(String type) { request = new GSRequest(type); }
internal GSTypedRequest(IDictionary <string, object> data) { request = new GSRequest(data); }
/// <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); }
public void RemovePending(GSRequest toRemove) { }
/// <summary> /// Send the given request. /// </summary> public static void Send(GSRequest request) { Instance.Send(request); }