示例#1
0
        /// <summary>
        /// Add a handler
        /// </summary>
        /// <param name="request">The request object</param>
        /// <param name="userSubscription">If it is a user subscription or a generic handler</param>
        /// <param name="dataHandler">The data handler</param>
        /// <returns></returns>
        public SocketSubscription AddHandler(object request, bool userSubscription, Action <SocketConnection, JToken> dataHandler)
        {
            var handler = new SocketSubscription(null, request, userSubscription, dataHandler);

            lock (handlersLock)
                handlers.Add(handler);
            return(handler);
        }
示例#2
0
        /// <summary>
        /// Add a handler
        /// </summary>
        /// <param name="identifier">The identifier of the handler</param>
        /// <param name="userSubscription">If it is a user subscription or a generic handler</param>
        /// <param name="dataHandler">The data handler</param>
        /// <returns></returns>
        /// <returns></returns>
        public SocketSubscription AddHandler(string identifier, bool userSubscription, Action <SocketConnection, JToken> dataHandler)
        {
            var handler = new SocketSubscription(identifier, null, userSubscription, dataHandler);

            lock (handlersLock)
                handlers.Add(handler);
            return(handler);
        }
示例#3
0
        private bool HandleData(JToken tokenData)
        {
            SocketSubscription currentSubscription = null;

            try
            {
                bool handled = false;
                var  sw      = Stopwatch.StartNew();
                lock (handlersLock)
                {
                    foreach (var handler in handlers)
                    {
                        currentSubscription = handler;
                        if (handler.Request == null)
                        {
                            if (socketClient.MessageMatchesHandler(tokenData, handler.Identifier))
                            {
                                handled = true;
                                handler.MessageHandler(this, tokenData);
                            }
                        }
                        else
                        {
                            if (socketClient.MessageMatchesHandler(tokenData, handler.Request))
                            {
                                handled   = true;
                                tokenData = socketClient.ProcessTokenData(tokenData);
                                handler.MessageHandler(this, tokenData);
                            }
                        }
                    }
                }

                sw.Stop();
                if (sw.ElapsedMilliseconds > 500)
                {
                    log.Write(LogVerbosity.Warning, $"Socket {Socket.Id} message processing slow ({sw.ElapsedMilliseconds}ms), consider offloading data handling to another thread. " +
                              "Data from this socket may arrive late or not at all if message processing is continuously slow.");
                }
                return(handled);
            }
            catch (Exception ex)
            {
                log.Write(LogVerbosity.Error, $"Socket {Socket.Id} Exception during message processing\r\nException: {ex}\r\nData: {tokenData}");
                currentSubscription?.InvokeExceptionHandler(ex);
                return(false);
            }
        }
示例#4
0
        /// <summary>
        /// Close the subscription
        /// </summary>
        /// <param name="subscription">Subscription to close</param>
        /// <returns></returns>
        public async Task Close(SocketSubscription subscription)
        {
            if (subscription.Confirmed)
            {
                await socketClient.Unsubscribe(this, subscription).ConfigureAwait(false);
            }

            bool shouldCloseWrapper = false;

            lock (handlersLock)
            {
                handlers.Remove(subscription);
                if (handlers.Count(r => r.UserSubscription) == 0)
                {
                    shouldCloseWrapper = true;
                }
            }

            if (shouldCloseWrapper)
            {
                await Close().ConfigureAwait(false);
            }
        }
示例#5
0
 /// <summary>
 /// Add handler
 /// </summary>
 /// <param name="handler"></param>
 public void AddHandler(SocketSubscription handler)
 {
     lock (handlersLock)
         handlers.Add(handler);
 }