コード例 #1
0
        static void Main(string[] args)
        {
            Log.Info("SharpInit starting");

            PlatformUtilities.RegisterImplementations();
            PlatformUtilities.GetImplementation <IPlatformInitialization>().Initialize();

            Log.Info("Platform initialization complete");

            UnitRegistry.InitializeTypes();
            UnitRegistry.ScanDefaultDirectories();

            Log.Info($"Loaded {UnitRegistry.Units.Count} units");

            UnitRegistry.UnitStateChange += StateChangeHandler;

            Log.Info("Registering IPC context...");

            var context = new ServerIpcContext();

            IpcFunctionRegistry.AddFunction(DynamicIpcFunction.FromContext(context));

            Log.Info("Starting IPC listener...");

            var ipc_listener = new IpcListener();

            ipc_listener.StartListening();

            Log.Info($"Listening on {ipc_listener.SocketEndPoint}");

            if (UnitRegistry.GetUnit("default.target") != null)
            {
                Log.Info("Activating default.target...");
                var result = UnitRegistry.CreateActivationTransaction("default.target").Execute();

                if (result.Type == Tasks.ResultType.Success)
                {
                    Log.Info("Successfully activated default.target.");
                }
                else
                {
                    Log.Info($"Error while activating default.target: {result.Type}, {result.Message}");
                }
            }

            Console.CancelKeyPress += (s, e) =>
            {
                ipc_listener.StopListening();
            };

            Thread.Sleep(-1);
        }
コード例 #2
0
ファイル: SocketBase.cs プロジェクト: bbqchickenrobot/netmq
        public void Bind([NotNull] string addr)
        {
            CheckContextTerminated();

            //  Process pending commands, if any.
            ProcessCommands(0, false);

            string protocol;
            string address;

            DecodeAddress(addr, out address, out protocol);

            CheckProtocol(protocol);

            if (protocol.Equals(Address.InProcProtocol))
            {
                var endpoint = new Ctx.Endpoint(this, m_options);

                bool addressRegistered = RegisterEndpoint(addr, endpoint);

                if (!addressRegistered)
                {
                    string xMsg = string.Format("Cannot bind address ( {0} ) - already in use.", addr);
                    throw new AddressAlreadyInUseException(xMsg);
                }

                // Save last endpoint URI
                m_options.LastEndpoint = addr;

                return;
            }
            if ((protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol)) && (
                m_options.SocketType == ZmqSocketType.Pub || m_options.SocketType == ZmqSocketType.Xpub))
            {
                //  For convenience's sake, bind can be used interchangeable with
                //  connect for PGM and EPGM transports.
                Connect(addr);
                return;
            }

            //  Remaining transports require to be run in an I/O thread, so at this
            //  point we'll choose one.
            IOThread ioThread = ChooseIOThread(m_options.Affinity);
            if (ioThread == null)
            {
                throw NetMQException.Create(ErrorCode.EmptyThread);
            }

            if (protocol.Equals(Address.TcpProtocol))
            {
                var listener = new TcpListener(ioThread, this, m_options);

                try
                {
                    listener.SetAddress(address);
                    m_port = listener.Port;

                    // Recreate the address string (localhost:1234) in case the port was system-assigned
                    var host = address.Substring(0, address.IndexOf(':'));
                    addr = "tcp://" + host + ":" + m_port;
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);

                return;
            }

            if (protocol.Equals(Address.PgmProtocol) || protocol.Equals(Address.EpgmProtocol))
            {
                var listener = new PgmListener(ioThread, this, m_options);

                try
                {
                    listener.Init(address);
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                m_options.LastEndpoint = addr;

                AddEndpoint(addr, listener);

                return;
            }

            if (protocol.Equals(Address.IpcProtocol))
            {
                var listener = new IpcListener(ioThread, this, m_options);

                try
                {
                    listener.SetAddress(address);
                    m_port = listener.Port;
                }
                catch (NetMQException ex)
                {
                    listener.Destroy();
                    EventBindFailed(addr, ex.ErrorCode);

                    throw;
                }

                // Save last endpoint URI
                m_options.LastEndpoint = listener.Address;

                AddEndpoint(addr, listener);
                return;
            }

            Debug.Assert(false);
            throw new FaultException(string.Format("SocketBase.Bind({0}) failure.", addr));
        }