Exemplo n.º 1
0
        public Relay Sync(int timeout = Timeout.Infinite)
        {
            log.Debug("synchronize processes");

            log.Debug("clear stop and sync events and clear ports exceptions");
            stop.Reset();
            sync.Reset();
            portsExceptions = new List <Exception>();

            string message = new JObject(new JProperty("sync", true)).ToString(Formatting.None);

            log.DebugFormat("sync message\n{0}", message);

            log.Debug("begin asynchronous write port B");
            portB.BeginWrite(message, delegate(IAsyncResult _ar) { ((Relay)_ar.AsyncState).SyncWriteCallback(_ar); }, this);

            log.DebugFormat("wait for stop event (timeout={0})", timeout);
            if (!stop.WaitOne(timeout))
            {
                throw new TimeoutException("Processes synchronization timeout.");
            }
            log.Debug("stop event");

            log.Debug("check ports exceptions");
            if (portsExceptions.Count > 0)
            {
                log.Debug("generate aggregated exception");
                throw new AggregateException(portsExceptions);
            }

            log.Debug("check sync event");
            if (!sync.WaitOne(0))
            {
                throw new SynchronizationException();
            }

            log.Debug("processes are synchronized");
            return(this);
        }
Exemplo n.º 2
0
        private void SyncReadCallback(IAsyncResult ar)
        {
            try
            {
                log.Debug("end asynchronous read port");
                string message = port.EndRead(ar);

                log.DebugFormat("received message\n{0}", message);

                log.Debug("begin asynchronous write port");
                port.BeginWrite(message, delegate(IAsyncResult _ar) { ((Scan)_ar.AsyncState).SyncWriteCallback(_ar); }, this);
            }
            catch (Exception ex)
            {
                log.Error("unhandled exception port", ex);
                portException = ex;
                stop.Set();
            }
        }
Exemplo n.º 3
0
        private void ReadPortBCallback(IAsyncResult ar)
        {
            log.Debug("read port B callback");
            try
            {
                string message = portB.EndRead(ar);
                log.DebugFormat("received message\n{0}", message);

                if (!stop.WaitOne(0))
                {
                    try
                    {
                        log.Debug("start asynchronous write port A");
                        portA.BeginWrite(message, delegate(IAsyncResult _ar) { ((Relay)_ar.AsyncState).WritePortACallback(_ar); }, this);
                    }
                    catch (Exception ex)
                    {
                        log.Error("unhandled exception port A", ex);
                        portsExceptions.Add(ex);
                        stop.Set();
                    }
                }
                else
                {
                    log.Debug("stop signalled");
                }
            }
            catch (EndOfInputStreamException)
            {
                log.Debug("end of native messages stream port B");
                stop.Set();
            }
            catch (Exception ex)
            {
                log.Error("unhandled exception port B", ex);
                portsExceptions.Add(ex);
                stop.Set();
            }
        }