Пример #1
0
        private void HandleUnsubscribeEventMessage(UnsubscribeEventMessage unsubscribeEventMessage,
                                                   IInputContext context)
        {
            if (!_objectsRepository.TryGetObject(unsubscribeEventMessage.ObjectId, out var objectAdapter) || objectAdapter == null)
            {
                WriteObjectNotFoundError(context, unsubscribeEventMessage.ObjectId);
                return;
            }

            if (!objectAdapter.Events.TryGetValue(unsubscribeEventMessage.EventName, out var ev))
            {
                WriteEventNotFoundError(context, unsubscribeEventMessage.ObjectId, unsubscribeEventMessage.EventName);
                return;
            }

            ISubscription subscription;

            lock (_subscriptions)
            {
                if (!_subscriptions.TryGetValue(ev, out subscription))
                {
                    //No exception or error is needed (same as for standard c# events when unsubscribed from a delegate that was not subscribed before).
                    return;
                }

                _subscriptions.Remove(ev);
            }

            subscription.Dispose();

            var ackMessage = new AckMessage(unsubscribeEventMessage.ClientHostAddress);

            context.Write(ackMessage.ToFrame());
        }
Пример #2
0
        public void Unsubscribe(string objectId, string eventName, Delegate handler)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(nameof(EventHandlersRepository));
            }

            var eventId = new EventId
            {
                ObjectId  = objectId,
                EventName = eventName
            };

            lock (_events)
            {
                var subscriptionRepository = _events[eventId];
                subscriptionRepository.Unsubscribe(handler);

                if (subscriptionRepository.IsEmpty)
                {
                    var unsubscribeMessage = new UnsubscribeEventMessage(_clientHostAddress, objectId, eventName);
                    _outputChannel.SendWithAck(unsubscribeMessage);

                    _events.Remove(eventId);
                }
            }
        }
Пример #3
0
        public static Message Create(IFrame frame, string side = "")
        {
            var messageType       = (MessageType)frame.TakeByte();
            var clientHostAddress = HostAddress.Read(frame);

            switch (messageType)
            {
            case MessageType.Hello:
                return(HelloMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Ack:
                return(AckMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Connect:
                return(ConnectMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Heartbeat:
                return(HeartbeatMessage.ReadInternal(clientHostAddress));

            case MessageType.TerminateSession:
                return(TerminateMessage.ReadInternal(clientHostAddress));

            case MessageType.ErrorMessage:
                return(ErrorMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Exception:
                return(ExceptionMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Event:
                return(EventMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Request:
                return(RequestMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Response:
                return(ResponseMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.SubscribeEvent:
                return(SubscribeEventMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.UnsubscribeEvent:
                return(UnsubscribeEventMessage.ReadInternal(clientHostAddress, frame));

            case MessageType.Notification:
                return(NotificationMessage.ReadInternal(clientHostAddress, frame));

            default:
                throw new IOException(
                          $"Unknown Message type {messageType} is received");
            }
        }
 protected virtual Task <IResponseMessage> HandleUnsubscribeEvent(UnsubscribeEventMessage request)
 {
     return(DefaultResponse());
 }