private bool SubscriptionHandler(SocketSubscription subscription, JToken data) { var evnt = subscription.GetWaitingEvent(SubscriptionEvent); if (evnt == null) { return(false); } if (((int?)data["id"])?.ToString() != evnt.WaitingId) { return(false); } var authResponse = Deserialize <CoinExSocketRequestResponse <CoinExSocketRequestResponseMessage> >(data, false); if (!authResponse.Success) { log.Write(LogVerbosity.Warning, "Subscription failed: " + authResponse.Error); subscription.SetEventById(evnt.WaitingId, false, authResponse.Error); return(true); } if (authResponse.Data.Error != null) { log.Write(LogVerbosity.Debug, $"Failed to subscribe: {authResponse.Data.Error.Code} {authResponse.Data.Error.Message}"); subscription.SetEventById(evnt.WaitingId, false, new ServerError(authResponse.Data.Error.Code, authResponse.Data.Error.Message)); return(true); } log.Write(LogVerbosity.Debug, "Subscription completed"); subscription.SetEventById(evnt.WaitingId, true, null); return(true); }
private bool DataHandlerV1 <T>(SocketSubscription subscription, JToken data, Action <T> handler) where T : class { var v1Data = (data["data"] != null || data["tick"] != null) && (data["rep"] != null || data["ch"] != null); var v1Error = data["status"] != null && (string)data["status"] == "error"; if (!v1Data && !v1Error) { return(false); } if (!v1Data && subscription.GetWaitingEvent(DataEvent) == null) { return(false); } var desResult = Deserialize <T>(data, false); if (!desResult.Success) { log.Write(LogVerbosity.Warning, $"Failed to deserialize data: {desResult.Error}. Data: {data}"); return(false); } handler(desResult.Data); subscription.SetEventByName(DataEvent, true, null); return(true); }
private bool DataHandlerQuery(SocketSubscription subscription, JToken data, Action <JToken[]> handler) { var evnt = subscription.GetWaitingEvent(DataEvent); if (evnt == null) { return(false); } if (((int?)data["id"])?.ToString() != evnt.WaitingId) { return(false); } if (data["result"].Type == JTokenType.Null) { subscription.SetEventById(evnt.WaitingId, false, new ServerError((int)data["error"]["code"], (string)data["error"]["message"])); } else { handler(new[] { data["result"] }); subscription.SetEventById(evnt.WaitingId, true, null); } return(true); }