예제 #1
0
        private bool SubscriptionHandlerV2(SocketSubscription subscription, JToken data)
        {
            var v2Sub = (string)data["op"] == "sub";

            if (!v2Sub)
            {
                return(false);
            }

            var subResponse = Deserialize <HuobiSocketAuthResponse>(data, false);

            if (!subResponse.Success)
            {
                log.Write(LogVerbosity.Warning, "Subscription failed: " + subResponse.Error);
                subscription.SetEventByName(SubscriptionEvent, false, subResponse.Error);
                return(true);
            }

            if (!subResponse.Data.IsSuccessful)
            {
                log.Write(LogVerbosity.Warning, "Subscription failed: " + subResponse.Data.ErrorMessage);
                subscription.SetEventByName(SubscriptionEvent, false, new ServerError(subResponse.Data.ErrorCode, subResponse.Data.ErrorMessage));
                return(true);
            }

            log.Write(LogVerbosity.Debug, "Subscription completed");
            subscription.SetEventByName(SubscriptionEvent, true, null);
            return(true);
        }
예제 #2
0
        private bool SubscriptionHandlerV1(SocketSubscription subscription, JToken data)
        {
            var v1Sub   = data["subbed"] != null;
            var v1Error = data["status"] != null && (string)data["status"] == "error";

            if (v1Sub || v1Error)
            {
                var subResponse = Deserialize <HuobiSubscribeResponse>(data, false);
                if (!subResponse.Success)
                {
                    log.Write(LogVerbosity.Warning, "Subscription failed: " + subResponse.Error);
                    subscription.SetEventByName(SubscriptionEvent, false, subResponse.Error);
                    return(true);
                }

                if (!subResponse.Data.IsSuccessful)
                {
                    log.Write(LogVerbosity.Warning, "Subscription failed: " + subResponse.Data.ErrorMessage);
                    subscription.SetEventByName(SubscriptionEvent, false, new ServerError($"{subResponse.Data.ErrorCode}, {subResponse.Data.ErrorMessage}"));
                    return(true);
                }

                log.Write(LogVerbosity.Debug, "Subscription completed");
                subscription.SetEventByName(SubscriptionEvent, true, null);
                return(true);
            }

            return(false);
        }
예제 #3
0
        private bool AuthenticationHandler(SocketSubscription subscription, JToken data)
        {
            if ((string)data["op"] != "auth")
            {
                return(false);
            }

            var authResponse = Deserialize <HuobiSocketAuthDataResponse <object> >(data, false);

            if (!authResponse.Success)
            {
                log.Write(LogVerbosity.Warning, "Authorization failed: " + authResponse.Error);
                subscription.SetEventByName(AuthenticationEvent, false, authResponse.Error);
                return(true);
            }
            if (!authResponse.Data.IsSuccessful)
            {
                log.Write(LogVerbosity.Warning, "Authorization failed: " + authResponse.Data.ErrorMessage);
                subscription.SetEventByName(AuthenticationEvent, false, new ServerError(authResponse.Data.ErrorCode, authResponse.Data.ErrorMessage));
                return(true);
            }

            log.Write(LogVerbosity.Debug, "Authorization completed");
            subscription.SetEventByName(AuthenticationEvent, true, null);
            return(true);
        }
예제 #4
0
        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);
        }
예제 #5
0
        private bool DataHandlerV2 <T>(SocketSubscription subscription, JToken data, Action <T> handler) where T : class
        {
            var v2Data = (string)data["op"] == "notify" || (string)data["op"] == "req";

            if (!v2Data)
            {
                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);
        }