コード例 #1
0
ファイル: Channel.cs プロジェクト: kouweizhong/MessageHub
        /// <summary>
        /// Gets a value indicating whether there exists a receiver identified by the receiverGuid for the specified message type
        /// </summary>
        /// <param name="messageType">The message type</param>
        /// <param name="receiverGuid">The receiver Guid</param>
        /// <returns>[True] if the receiver exists for the message type, [False] otherwise</returns>
        public bool HasReceiver(string messageType, Guid receiverGuid)
        {
            if (Receivers.ContainsKey(messageType))
            {
                return(Receivers[messageType].Any(r => r.Id == receiverGuid));
            }

            return(false);
        }
コード例 #2
0
ファイル: Channel.cs プロジェクト: kouweizhong/MessageHub
        /// <summary>
        /// Gets the actions to execute for the specified message type
        /// </summary>
        /// <param name="messageType"></param>
        /// <returns></returns>
        private IList <Func <object, Task> > GetReceiverActions(string messageType)
        {
            IList <Func <object, Task> > actions = new List <Func <object, Task> >();

            if (Receivers.ContainsKey(messageType))
            {
                actions = Receivers[messageType].Select(r => r.OnMessageReceived).ToList();
            }

            return(actions);
        }
コード例 #3
0
ファイル: Channel.cs プロジェクト: kouweizhong/MessageHub
        /// <summary>
        /// Adds a receiver with the specified unique identifier to the channel for the specified message type
        /// </summary>
        /// <param name="messageType">The message type that the receiver will listen for</param>
        /// <param name="action">The action to execute when a message of the specified type is received</param>
        /// <param name="receiverGuid">The receiver's unique identifier</param>
        public void AddReceiver(string messageType, Func <object, Task> action, Guid receiverGuid)
        {
            ConcurrentBag <Receiver> recList = new ConcurrentBag <Receiver>();

            if (Receivers.ContainsKey(messageType))
            {
                recList = Receivers[messageType] as ConcurrentBag <Receiver>;
            }

            recList.Add(new Receiver {
                Id = receiverGuid, MessageType = messageType, OnMessageReceived = action
            });

            Receivers[messageType] = recList;
        }
コード例 #4
0
ファイル: Channel.cs プロジェクト: kouweizhong/MessageHub
        /// <summary>
        /// Removes the receiver identified by Id from notifications for the specified message type
        /// </summary>
        /// <param name="messageType">The message type</param>
        /// <param name="receiverGuid">The receiver Id</param>
        /// <returns>[True] if the receiver was found and removed, [False] otherwise</returns>
        public bool RemoveReceiver(string messageType, Guid receiverGuid)
        {
            if (Receivers.ContainsKey(messageType))
            {
                Receiver receiver = Receivers[messageType].FirstOrDefault(r => r.Id == receiverGuid);
                if (receiver != null)
                {
                    ConcurrentBag <Receiver> newReceivers = new ConcurrentBag <Receiver>(Receivers[messageType]);
                    if (newReceivers.TryTake(out receiver))
                    {
                        Receivers[messageType] = newReceivers;
                        return(true);
                    }
                }
            }

            return(false);
        }
        /// <summary>
        /// Adds a <paramref name="receiver"/> to the <see cref="ImportManager"/>
        /// </summary>
        /// <param name="receiver">The receiver to be added</param>
        /// <returns>true if the receiver was successfully added</returns>
        public bool RegisterExisting(Receiver receiver)
        {
            string streamID = receiver.StreamId;

            if (Receivers.ContainsKey(streamID))
            {
                Debug.LogWarning($"Failed to add {typeof(Receiver)} because one already existed for {nameof(streamID)}:{streamID}");
                return(false);
            }

            if (!StreamFromID.ContainsKey(streamID))
            {
                Debug.LogWarning($"Failed to add {typeof(Receiver)} because no {typeof(SpeckleStream)} with {nameof(streamID)}:{streamID} could be found");
                return(false);
            }

            SpeckleStream stream = StreamFromID[streamID];


            Receivers.Add(streamID, receiver);
            OnReceiverAdd?.Invoke(stream, receiver);

            return(true);
        }