Пример #1
0
        IDisposable SubscribeLocal(SecsEventSubscription subscription)
        {
            var handler = new Action <SecsMessage>(msg =>
            {
                #region event action
                var filter = subscription.Filter;
                try
                {
                    if (msg.IsMatch(filter))
                    {
                        EapLogger.Info("event[" + filter.Name + "] >> EAP");
                        msg.Name = filter.Name;
                        subscription.Handle(msg);
                    }
                }
                catch (Exception ex)
                {
                    EapLogger.Error("event[" + filter.Name + "] EAP process Error!", ex);
                }
                #endregion
            });

            _eventHandlers.AddHandler(subscription.GetKey(), handler);
            EapLogger.Notice("EAP subscribe event " + subscription.Filter.Name);
            return(new LocalDisposable(() =>
            {
                _eventHandlers.RemoveHandler(subscription.GetKey(), handler);
                EapLogger.Notice("EAP unsubscribe event " + subscription.Filter.Name);
            }));
        }
Пример #2
0
        IDisposable SubscribeRemote(SecsEventSubscription subscription)
        {
            var filter      = subscription.Filter;
            var description = filter.Name;
            var handler     = new Action <SecsMessage>(msg =>
            {
                try
                {
                    if (msg.IsMatch(filter))
                    {
                        EapLogger.Info($"event[{description}] >> Z");
                        msg.Name = filter.Name;
                        subscription.Handle(msg);
                    }
                }
                catch (Exception ex)
                {
                    EapLogger.Error($"event[{description}] Z process Error!", ex);
                }
            });
            int    key = subscription.GetKey();
            string recoverQueuePath = $"FormatName:DIRECT=TCP:{subscription.ClientAddress} \\private$\\{subscription.Id}";

            if (subscription.Recoverable)
            {
                return(new SerializableDisposable(subscription,
                                                  new RemoteDisposable(subscription,
                                                                       subscribeAction: () =>
                {
                    #region recover complete
                    _eventHandlers.AddHandler(key, handler);
                    EapLogger.Notice("Z subscribe event " + description);

                    Action <SecsMessage> recover = null;
                    if (_recoverEventHandlers.TryRemove(recoverQueuePath, out recover))
                    {
                        _eventHandlers.RemoveHandler(key, recover);
                        EapLogger.Notice($"Z recover completely event[{description}]");
                    }
                    #endregion
                },
                                                                       disposeAction: () =>
                {
                    #region Dispose
                    _eventHandlers.RemoveHandler(key, handler);
                    EapLogger.Notice("Z unsubscribe event " + description);

                    var queue = new MessageQueue(recoverQueuePath, QueueAccessMode.Send)
                    {
                        Formatter = new BinaryMessageFormatter()
                    };
                    queue.DefaultPropertiesToSend.Recoverable = true;

                    var recover = new Action <SecsMessage>(msg =>
                    {
                        #region recover action
                        try
                        {
                            if (msg.IsMatch(filter))
                            {
                                EapLogger.Info("recoverable event[" + description + "]");
                                msg.Name = filter.Name;
                                queue.Send(msg);
                            }
                        }
                        catch (Exception ex)
                        {
                            EapLogger.Error(ex);
                        }
                        #endregion
                    });
                    if (_recoverEventHandlers.TryAdd(recoverQueuePath, recover))
                    {
                        EapLogger.Notice("Z subscribe event[" + description + "] for recovering.");
                        _eventHandlers.AddHandler(key, recover);
                    }
                    #endregion
                })));
            }
            _eventHandlers.AddHandler(key, handler);
            EapLogger.Notice("Z subscribe event " + description);
            return(new SerializableDisposable(subscription,
                                              new RemoteDisposable(subscription, null, () =>
            {
                _eventHandlers.RemoveHandler(key, handler);
                EapLogger.Notice("Z unsubscribe event " + description);
            })));
        }