Ejemplo n.º 1
0
        private NetMQActor(PairSocket self, PairSocket shim, IShimHandler shimHandler)
        {
            m_shimHandler = shimHandler;

            m_self = self;
            m_shim = shim;

            EventHandler <NetMQSocketEventArgs> onReceive = (sender, e) =>
                                                            m_receiveEvent.Fire(this, new NetMQActorEventArgs(this));

            EventHandler <NetMQSocketEventArgs> onSend = (sender, e) =>
                                                         m_sendEvent.Fire(this, new NetMQActorEventArgs(this));

            m_receiveEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.ReceiveReady += onReceive,
                () => m_self.ReceiveReady -= onReceive);

            m_sendEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.SendReady += onSend,
                () => m_self.SendReady -= onSend);

            var random = new Random();

            // Bind and connect pipe ends
            string actorName;
            string endPoint;

            while (true)
            {
                try
                {
                    actorName = string.Format("NetMQActor-{0}-{1}", random.Next(0, 10000), random.Next(0, 10000));
                    endPoint  = string.Format("inproc://{0}", actorName);
                    m_self.Bind(endPoint);
                    break;
                }
                catch (AddressAlreadyInUseException)
                {
                    // Loop around and try another random address
                }
            }

            m_shim.Connect(endPoint);

            m_shimThread = new Thread(RunShim)
            {
                Name = actorName
            };
            m_shimThread.Start();

            // Mandatory handshake for new actor so that constructor returns only
            // when actor has also initialised. This eliminates timing issues at
            // application start up.
            m_self.ReceiveSignal();
        }
Ejemplo n.º 2
0
        public Actor(NetMQContext context,
                     IShimHandler <T> shimHandler, T state)
        {
            this.m_self = context.CreatePairSocket();
            this.m_shim = new Shim <T>(shimHandler, context.CreatePairSocket());
            this.m_self.Options.SendHighWatermark = 1000;
            this.m_self.Options.SendHighWatermark = 1000;
            this.m_state = state;

            m_receiveEventDelegatorHelper = new EventDelegatorHelper <NetMQActorEventArgs <T> >(() => m_self.ReceiveReady += OnReceive,
                                                                                                () => m_self.ReceiveReady += OnReceive);
            m_sendEventDelegatorHelper = new EventDelegatorHelper <NetMQActorEventArgs <T> >(() => m_self.SendReady += OnReceive,
                                                                                             () => m_self.SendReady += OnSend);

            //now binding and connect pipe ends
            string endPoint = string.Empty;

            while (true)
            {
                Action bindAction = () =>
                {
                    endPoint = GetEndPointName();
                    m_self.Bind(endPoint);
                };

                try
                {
                    bindAction();
                    break;
                }
                catch (NetMQException nex)
                {
                    if (nex.ErrorCode == ErrorCode.EFAULT)
                    {
                        bindAction();
                    }
                }
            }

            m_shim.Pipe.Connect(endPoint);

            //Initialise the shim handler
            this.m_shim.Handler.Initialise(state);

            //Create Shim thread handler
            CreateShimThread(state);

            //  Mandatory handshake for new actor so that constructor returns only
            //  when actor has also initialized. This eliminates timing issues at
            //  application start up.
            m_self.WaitForSignal();
        }
Ejemplo n.º 3
0
        private NetMQActor([NotNull] NetMQContext context, [NotNull] IShimHandler shimHandler)
        {
            m_shimHandler = shimHandler;

            m_self = context.CreatePairSocket();
            m_shim = context.CreatePairSocket();

            EventHandler <NetMQSocketEventArgs> onReceive = (sender, e) =>
                                                            m_receiveEvent.Fire(this, new NetMQActorEventArgs(this));

            EventHandler <NetMQSocketEventArgs> onSend = (sender, e) =>
                                                         m_sendEvent.Fire(this, new NetMQActorEventArgs(this));

            m_receiveEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.ReceiveReady += onReceive,
                () => m_self.ReceiveReady -= onReceive);

            m_sendEvent = new EventDelegator <NetMQActorEventArgs>(
                () => m_self.SendReady += onSend,
                () => m_self.SendReady -= onSend);

            var random = new Random();

            //now binding and connect pipe ends
            string endPoint;

            while (true)
            {
                try
                {
                    endPoint = string.Format("inproc://NetMQActor-{0}-{1}", random.Next(0, 10000), random.Next(0, 10000));
                    m_self.Bind(endPoint);
                    break;
                }
                catch (AddressAlreadyInUseException)
                {
                    // In case address already in use we continue searching for an address
                }
            }

            m_shim.Connect(endPoint);

            m_shimThread = new Thread(RunShim);
            m_shimThread.Start();

            // Mandatory handshake for new actor so that constructor returns only
            // when actor has also initialised. This eliminates timing issues at
            // application start up.
            m_self.ReceiveSignal();
        }
Ejemplo n.º 4
0
        protected WSSocket(NetMQContext context, IShimHandler<int> shimHandler )
        {
            int id = Interlocked.Increment(ref s_id);
            m_context = context;

            m_messagesPipe = context.CreatePairSocket();
            m_messagesPipe.Bind(string.Format("inproc://wsrouter-{0}", id));

            m_messagesPipe.ReceiveReady += OnMessagePipeReceiveReady;

            m_actor = new Actor<int>(context, shimHandler, id);

            m_messagesPipe.WaitForSignal();
        }
Ejemplo n.º 5
0
        protected WSSocket(NetMQContext context, IShimHandler <int> shimHandler)
        {
            int id = Interlocked.Increment(ref s_id);

            m_context = context;

            m_messagesPipe = context.CreatePairSocket();
            m_messagesPipe.Bind(string.Format("inproc://wsrouter-{0}", id));

            m_messagesPipe.ReceiveReady += OnMessagePipeReceiveReady;

            m_actor = new Actor <int>(context, shimHandler, id);

            m_messagesPipe.WaitForSignal();
        }
Ejemplo n.º 6
0
        public Actor(NetMQContext context,
                     IShimHandler <T> shimHandler, T state)
        {
            this.self = context.CreatePairSocket();
            this.shim = new Shim <T>(shimHandler, context.CreatePairSocket());
            this.self.Options.SendHighWatermark = 1000;
            this.self.Options.SendHighWatermark = 1000;
            this.state = state;

            //now binding and connect pipe ends
            string endPoint = string.Empty;

            while (true)
            {
                Action bindAction = () =>
                {
                    endPoint = GetEndPointName();
                    self.Bind(endPoint);
                };

                try
                {
                    bindAction();
                    break;
                }
                catch (NetMQException nex)
                {
                    if (nex.ErrorCode == ErrorCode.EFAULT)
                    {
                        bindAction();
                    }
                }
            }

            shim.Pipe.Connect(endPoint);

            //Initialise the shim handler
            this.shim.Handler.Initialise(state);

            //Create Shim thread handler
            CreateShimThread(state);
        }
Ejemplo n.º 7
0
        public Actor(NetMQContext context, IShimHandler shimHandler, object[] args)
        {
            this.self = context.CreatePairSocket();
            this.shim = new Shim(shimHandler, context.CreatePairSocket());
            this.self.Options.SendHighWatermark = 1000;
            this.self.Options.SendHighWatermark = 1000;

            //now binding and connect pipe ends
            string endPoint = string.Empty;
            while (true)
            {
                Action bindAction = () =>
                {
                    endPoint = GetEndPointName();
                    self.Bind(endPoint);
                };

                try
                {
                    bindAction();
                    break;
                }
                catch (NetMQException nex)
                {
                    if (nex.ErrorCode == ErrorCode.EFAULT)
                    {
                        bindAction();
                    }
                }

            }

            shim.Pipe.Connect(endPoint);

            //Create Shim thread handler
            CreateShimThread(args);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// Create a new <see cref="NetMQActor"/> with the given shimHandler.
 /// </summary>
 /// <param name="shimHandler">an <c>IShimHandler</c> that provides the Run method</param>
 /// <returns>the newly-created <c>NetMQActor</c></returns>
 public static NetMQActor Create(IShimHandler shimHandler)
 {
     return(new NetMQActor(new PairSocket(), new PairSocket(), shimHandler));
 }
Ejemplo n.º 9
0
 public static NetMQActor Create(NetMQContext context, IShimHandler shimHandler)
 {
     return(new NetMQActor(context.CreatePairSocket(), context.CreatePairSocket(), shimHandler));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Create a new Shim-object to encapsulate the given pipe
 /// and to use the given IShimHandler to service that pipe.
 /// </summary>
 /// <param name="shimHandler">the IShimHandler to initialize and run the pipe</param>
 /// <param name="pipe">the PairSocket that will be our pipe</param>
 public Shim(IShimHandler <T> shimHandler, PairSocket pipe)
 {
     this.Handler = shimHandler;
     this.Pipe    = pipe;
 }
Ejemplo n.º 11
0
 public static NetMQActor Create([NotNull] NetMQContext context, [NotNull] IShimHandler shimHandler)
 {
     return(new NetMQActor(context, shimHandler));
 }
Ejemplo n.º 12
0
 public static NetMQActor Create(NetMQContext context, IShimHandler shimHandler)
 {
     return(new NetMQActor(context, shimHandler));
 }
Ejemplo n.º 13
0
 public Shim(IShimHandler shimHandler, PairSocket pipe)
 {
     this.Handler = shimHandler;
     this.Pipe = pipe;
 }