Example #1
0
 public void Invoke(LazyNotification lazyNotification)
 {
     eventLoopThread = new Thread(() => dispatcher.Invoke(lazyNotification))
     {
         Name = "TS3 MessageDispatcher"
     };
     eventLoopThread.Start();
 }
Example #2
0
 public void SetNotification(LazyNotification notification)
 {
     if (DependsOn != null && Array.IndexOf(DependsOn, notification.NotifyType) < 0)
     {
         throw new ArgumentException("The notification does not match this waitblock");
     }
     this.notification = notification;
     notificationWaiter.Set();
 }
Example #3
0
 public void SetNotification(LazyNotification notification)
 {
     if (notification.NotifyType != DependsOn)
     {
         throw new ArgumentException();
     }
     this.notification = notification;
     notificationWaiter.Set();
 }
        public LazyNotification?PushMessage(string message)
        {
            string notifyname;
            int    splitindex = message.IndexOf(' ');

            if (splitindex < 0)
            {
                notifyname = message.TrimEnd();
            }
            else
            {
                notifyname = message.Substring(0, splitindex);
            }

            var ntfyType = MessageHelper.GetNotificationType(notifyname);

            if (ntfyType == NotificationType.Unknown)
            {
                cmdLineBuffer = message;
                return(null);
            }

            var lineDataPart = splitindex < 0 ? "" : message.Substring(splitindex);

            // if it's not an error it is a notification
            if (ntfyType != NotificationType.Error)
            {
                var notification     = CommandDeserializer.GenerateNotification(lineDataPart, ntfyType);
                var lazyNotification = new LazyNotification(notification, ntfyType);
                if (dependingBlocks[(int)ntfyType] != null)
                {
                    foreach (var item in dependingBlocks[(int)ntfyType])
                    {
                        if (!item.Closed)
                        {
                            item.SetNotification(lazyNotification);
                        }
                    }
                    dependingBlocks[(int)ntfyType].Clear();
                }

                return(lazyNotification);
            }

            var errorStatus = (CommandError)CommandDeserializer.GenerateSingleNotification(lineDataPart, NotificationType.Error);

            if (synchronQueue)
            {
                if (requestQueue.Count > 0)
                {
                    var waitBlock = requestQueue.Dequeue();
                    waitBlock.SetAnswer(errorStatus, cmdLineBuffer);
                    cmdLineBuffer = null;
                }
                else /* ??? */ } {
        }
 public void Invoke(LazyNotification lazyNotification)
 {
     eventQueue.Enqueue(lazyNotification);
     eventBlock.Set();
 }
 public void Invoke(LazyNotification lazyNotification) => ThreadPool.QueueUserWorkItem(Call, lazyNotification);
 public void Invoke(LazyNotification lazyNotification)
 {
 }
Example #8
0
        public LazyNotification?PushMessage(ReadOnlyMemory <byte> message)
        {
            var    msgSpan = message.Span;
            string notifyname;
            int    splitindex = msgSpan.IndexOf(AsciiSpace);

            if (splitindex < 0)
            {
                notifyname = msgSpan.TrimEnd(AsciiSpace).NewUtf8String();
            }
            else
            {
                notifyname = msgSpan.Slice(0, splitindex).NewUtf8String();
            }

            bool             hasEqual;
            NotificationType ntfyType;

            if ((hasEqual = notifyname.IndexOf('=') >= 0) ||
                (ntfyType = findTypeOfNotification(notifyname)) == NotificationType.Unknown)
            {
                if (!hasEqual)
                {
                    Log.Debug("Maybe unknown notification: {0}", notifyname);
                }
                cmdLineBuffer = message;
                return(null);
            }

            var lineDataPart = splitindex < 0 ? ReadOnlySpan <byte> .Empty : msgSpan.Slice(splitindex);

            // if it's not an error it is a notification
            if (ntfyType != NotificationType.CommandError)
            {
                var notification = Deserializer.GenerateNotification(lineDataPart, ntfyType);
                if (!notification.Ok)
                {
                    Log.Warn("Got unparsable message. ({0})", msgSpan.NewUtf8String());
                    return(null);
                }

                var lazyNotification = new LazyNotification(notification.Value, ntfyType);
                lock (waitBlockLock)
                {
                    var dependantList = dependingBlocks[(int)ntfyType];
                    if (dependantList != null)
                    {
                        foreach (var item in dependantList)
                        {
                            item.SetNotification(lazyNotification);
                            if (item.DependsOn != null)
                            {
                                foreach (var otherDepType in item.DependsOn)
                                {
                                    if (otherDepType == ntfyType)
                                    {
                                        continue;
                                    }
                                    dependingBlocks[(int)otherDepType]?.Remove(item);
                                }
                            }
                        }
                        dependantList.Clear();
                    }
                }

                return(lazyNotification);
            }

            var result      = Deserializer.GenerateSingleNotification(lineDataPart, NotificationType.CommandError);
            var errorStatus = result.Ok ? (CommandError)result.Value : Util.CustomError("Invalid Error code");

            return(PushMessageInternal(errorStatus, ntfyType));
        }
Example #9
0
        public LazyNotification?PushMessage(string message)
        {
            string notifyname;
            int    splitindex = message.IndexOf(' ');

            if (splitindex < 0)
            {
                notifyname = message.TrimEnd();
            }
            else
            {
                notifyname = message.Substring(0, splitindex);
            }

            var ntfyType = MessageHelper.GetNotificationType(notifyname);

            if (ntfyType == NotificationType.Unknown)
            {
                cmdLineBuffer = message;
                return(null);
            }

            var lineDataPart = splitindex < 0 ? "" : message.Substring(splitindex);

            // if it's not an error it is a notification
            if (ntfyType != NotificationType.Error)
            {
                var notification     = CommandDeserializer.GenerateNotification(lineDataPart, ntfyType);
                var lazyNotification = new LazyNotification(notification, ntfyType);
                var dependantList    = dependingBlocks[(int)ntfyType];
                if (dependantList != null)
                {
                    lock (dependantBlockLock)
                    {
                        foreach (var item in dependantList)
                        {
                            if (!item.Closed)
                            {
                                item.SetNotification(lazyNotification);
                            }
                            if (item.DependsOn != null)
                            {
                                foreach (var otherDepType in item.DependsOn)
                                {
                                    if (otherDepType == ntfyType)
                                    {
                                        continue;
                                    }
                                    dependingBlocks[(int)otherDepType]?.Remove(item);
                                }
                            }
                        }
                        dependantList.Clear();
                    }
                }

                return(lazyNotification);
            }

            var errorStatus = (CommandError)CommandDeserializer.GenerateSingleNotification(lineDataPart, NotificationType.Error);

            if (synchronQueue)
            {
                WaitBlock waitBlock;
                if (!requestQueue.IsEmpty && requestQueue.TryDequeue(out waitBlock))
                {
                    waitBlock.SetAnswer(errorStatus, cmdLineBuffer);
                    cmdLineBuffer = null;
                }
                else /* ??? */ } {
        }
Example #10
0
        public LazyNotification?PushMessage(string message)
        {
            string notifyname;
            int    splitindex = message.IndexOf(' ');

            if (splitindex < 0)
            {
                notifyname = message.TrimEnd();
            }
            else
            {
                notifyname = message.Substring(0, splitindex);
            }

            bool             hasEqual;
            NotificationType ntfyType;

            if ((hasEqual = notifyname.IndexOf('=') >= 0) ||
                (ntfyType = MessageHelper.GetNotificationType(notifyname)) == NotificationType.Unknown)
            {
                if (!hasEqual)
                {
                    Log.Debug("Maybe unknown notification: {0}", notifyname);
                }
                cmdLineBuffer = message;
                return(null);
            }

            var lineDataPart = splitindex < 0 ? "" : message.Substring(splitindex);

            // if it's not an error it is a notification
            if (ntfyType != NotificationType.CommandError)
            {
                var notification     = Deserializer.GenerateNotification(lineDataPart, ntfyType);
                var lazyNotification = new LazyNotification(notification, ntfyType);
                lock (waitBlockLock)
                {
                    var dependantList = dependingBlocks[(int)ntfyType];
                    if (dependantList != null)
                    {
                        foreach (var item in dependantList)
                        {
                            item.SetNotification(lazyNotification);
                            if (item.DependsOn != null)
                            {
                                foreach (var otherDepType in item.DependsOn)
                                {
                                    if (otherDepType == ntfyType)
                                    {
                                        continue;
                                    }
                                    dependingBlocks[(int)otherDepType]?.Remove(item);
                                }
                            }
                        }
                        dependantList.Clear();
                    }
                }

                return(lazyNotification);
            }

            var result      = Deserializer.GenerateSingleNotification(lineDataPart, NotificationType.CommandError);
            var errorStatus = result.Ok ? (CommandError)result.Value : Util.CustomError("Invalid Error code");

            return(PushMessageInternal(errorStatus, ntfyType));
        }
Example #11
0
 public void Invoke(LazyNotification lazyNotification) => dispatcher.Invoke(lazyNotification);