Exemplo n.º 1
0
        public void TestChannelEventList()
        {
            using (IpcEventChannel channel = new IpcEventChannel(_registrar, _channel))
            {
                Assert.AreEqual("Test", channel["Test"].LocalName);
                Assert.AreEqual("TEST", channel["TEST"].LocalName);
                Assert.AreEqual("test", channel["test"].LocalName);

                int count = 0;
                foreach (IpcEvent e in channel.GetEvents())
                {
                    Assert.AreEqual("test", e.LocalName.ToLower());
                    count++;
                }
                Assert.AreEqual(3, count);
            }
        }
Exemplo n.º 2
0
        void Listen()
        {
            if (!_listener.WaitOne(_channel.DefaultTimeout, false))
            {
                return;
            }
            try
            {
                _channel.Registrar.RegisterInstance(_channel.ChannelName, _identity, _instanceName);

                IpcEvent[]   events  = null;
                WaitHandle[] waiting = null;
                while (true)
                {
                    if (events == null)
                    {
                        int ix = 0;
                        Dictionary <string, WaitHandle> temp = new Dictionary <string, WaitHandle>(_handles);
                        events  = new List <IpcEvent>(_channel.GetEvents()).ToArray();
                        waiting = new WaitHandle[events.Length + 2];

                        foreach (IpcEvent e in events)
                        {
                            WaitHandle h;
                            if (!_handles.TryGetValue(e.LocalName, out h))
                            {
                                _handles.Add(e.LocalName, h = new EventWaitHandle(false, EventResetMode.ManualReset, String.Format("{0}.{1}.{2}", _channel.ChannelName, _identity, e.LocalName)));
                            }
                            else
                            {
                                temp.Remove(e.LocalName);
                            }
                            waiting[ix++] = h;
                        }
                        foreach (KeyValuePair <string, WaitHandle> e in temp)
                        {
                            _handles.Remove(e.Key);
                            e.Value.Close();
                        }
                        waiting[ix++] = _modified;
                        waiting[ix]   = _quit;
                    }

                    _ready.Set();
                    int handle = WaitHandle.WaitAny(waiting);
                    if (handle < events.Length)
                    {
                        ((EventWaitHandle)waiting[handle]).Reset();
                        string[] args = _channel.Registrar.ReadParameters(_channel.ChannelName, _identity, events[handle].LocalName);
                        _channel.RaiseLocal(events[handle].LocalName, args);
                    }
                    else if (handle == waiting.Length - 1)
                    {
                        break;
                    }
                    else
                    {
                        events = null;
                    }
                }
            }
            finally
            {
                _channel.Registrar.UnregisterInstance(_channel.ChannelName, _identity);
                _listener.ReleaseMutex();
            }
        }