Пример #1
0
        /// <summary>
        /// 具体执行
        /// </summary>
        private void RunSignle()
        {
            var socket = ZSocket.CreateClientSocket(Config.WorkerResultAddress, ZSocketType.DEALER);

            {
                using (var pool = ZmqPool.CreateZmqPool())
                {
                    pool.Prepare(new[] { ZSocket.CreateClientSocket(Config.WorkerCallAddress, ZSocketType.PULL, Identity) }, ZPollEvent.In);
                    State = StationState.Run;
                    while (CanLoop)
                    {
                        if (!pool.Poll() || !pool.CheckIn(0, out var message))
                        {
                            continue;
                        }
                        Interlocked.Increment(ref RecvCount);
                        using (message)
                        {
                            if (!Unpack(message, out var item))
                            {
                                SendLayoutErrorResult(ref socket, item.Caller);
                                continue;
                            }
                            ApiCall(ref socket, item);
                        }
                    }
                }
            }
            socket.Dispose();
            _processSemaphore?.Release();
        }
Пример #2
0
        /// <summary>
        /// 命令轮询
        /// </summary>
        /// <returns></returns>
        private void Run()
        {
            _inprocPollSocket =
                ZSocket.CreateServiceSocket($"inproc://{Config.StationName}_Proxy", ZSocketType.ROUTER);
            _inprocPollSocket.Backlog = 4096;
            _callPollSocket           = ZSocket.CreateClientSocket(Config.RequestAddress, ZSocketType.DEALER);

            WaitCount = 0;
            ZeroTrace.SystemLog($"{Config.StationName}(proxy)", "Listen");
            _waitToken.Release();
            using (var pool = ZmqPool.CreateZmqPool())
            {
                pool.Prepare(new[] { _inprocPollSocket, _callPollSocket }, ZPollEvent.In);
                //SystemManager.Instance.HeartReady(StationName, RealName);
                while (!RunTaskCancel.Token.IsCancellationRequested)
                {
                    if (!pool.Poll())
                    {
                        continue;
                    }
                    CheckCall(pool);

                    CheckResult(pool);
                }
            }
            _waitToken.Release();
        }
Пример #3
0
 /// <summary>
 ///     进入系统侦听
 /// </summary>
 internal static void Monitor()
 {
     using (OnceScope.CreateScope(ZeroApplication.Config))
     {
     }
     using (var poll = ZmqPool.CreateZmqPool())
     {
         poll.Prepare(new[] { ZSocket.CreateSubscriberSocket(ZeroApplication.Config.ZeroMonitorAddress) }, ZPollEvent.In);
         ZeroTrace.SystemLog("Zero center in monitor...");
         TaskEndSem.Release();
         while (ZeroApplication.IsAlive)
         {
             if (!poll.Poll() || !poll.CheckIn(0, out var message))
             {
                 continue;
             }
             if (!message.Unpack(out var item))
             {
                 continue;
             }
             OnMessagePush(item.ZeroEvent, item.SubTitle, item.Content);
         }
     }
     TaskEndSem.Release();
 }
Пример #4
0
        //private string inporcName;

        /// <summary>
        /// 具体执行
        /// </summary>
        /// <returns>返回False表明需要重启</returns>
        protected override bool RunInner(CancellationToken token)
        {
            SystemManager.Instance.HeartReady(StationName, RealName);
            //using (var socket = ZSocket.CreateClientSocket(inporcName, ZSocketType.PAIR))
            using (var pool = ZmqPool.CreateZmqPool())
            {
                pool.Prepare(ZPollEvent.In,
                             ZSocket.CreateClientSocket(Config.WorkerCallAddress, ZSocketType.SUB, Identity, Subscribe));
                State = StationState.Run;
                while (CanLoop)
                {
                    if (!pool.Poll())
                    {
                        Idle();
                    }
                    else if (pool.CheckIn(0, out var message))
                    {
                        if (Unpack(message, out var item))
                        {
                            DoHandle(item);
                        }

                        //socket.SendTo(message);
                    }
                }
            }

            SystemManager.Instance.HeartLeft(StationName, RealName);
            return(true);
        }
Пример #5
0
        /// <summary>
        /// 具体执行
        /// </summary>
        private void RunWait()
        {
            ZeroTrace.SystemLog(StationName, "run", Config.WorkerCallAddress, Name, RealName);
            var socket = ZSocket.CreateClientSocket(Config.WorkerResultAddress, ZSocketType.DEALER);

            {
                using (var pool = ZmqPool.CreateZmqPool())
                {
                    pool.Prepare(ZPollEvent.In, ZSocket.CreateClientSocket(Config.WorkerCallAddress, ZSocketType.PULL, Identity));
                    State = StationState.Run;
                    while (CanLoop)
                    {
                        if (!pool.Poll() || !pool.CheckIn(0, out var message))
                        {
                            continue;
                        }
                        Interlocked.Increment(ref RecvCount);
                        using (message)
                        {
                            if (!Unpack(message, out var item))
                            {
                                SendLayoutErrorResult(ref socket, item.Caller);
                                continue;
                            }

                            Interlocked.Increment(ref waitCount);
                            if (waitCount > ZeroApplication.Config.MaxWait)
                            {
                                item.Result = ApiResult.UnavailableJson;
                                SendResult(ref socket, item, ZeroOperatorStateType.Unavailable);
                            }
                            else
                            {
                                Task.Factory.StartNew(ApiCallTask, item);
                            }
                        }
                    }
                }
            }
            ZeroTrace.SystemLog(StationName, "end", Config.WorkerCallAddress, Name, RealName);
            socket.Dispose();
        }
Пример #6
0
 /// <summary>
 /// 具体执行
 /// </summary>
 /// <returns>返回False表明需要重启</returns>
 protected sealed override bool RunInner(CancellationToken token)
 {
     using (var socket = ZSocket.CreateRequestSocket(Config.RequestAddress, Identity))
     {
         foreach (var data in datas)
         {
             socket.Publish(data);
         }
         datas.Clear();
         File.Delete(CacheFileName);
         using (var poll = ZmqPool.CreateZmqPool())
         {
             poll.Prepare(ZPollEvent.In,
                          ZSocket.CreateServiceSocket(inporcName, ZSocketType.PULL));
             for (int i = 0; i < 64; i++)
             {
                 sockets.Add(ZSocket.CreateClientSocket(inporcName, ZSocketType.PUSH));
             }
             SystemManager.Instance.HeartReady(StationName, RealName);
             State = StationState.Run;
             Thread.Sleep(5);
             foreach (var data in datas)
             {
                 socket.Publish(data);
             }
             datas.Clear();
             while (true)
             {
                 if (poll.CheckIn(0, out var message))
                 {
                     socket.SendTo(message);
                 }
                 else if (!CanLoop)//保证发送完成
                 {
                     break;
                 }
             }
             SystemManager.Instance.HeartLeft(StationName, RealName);
         }
     }
     return(true);
 }
Пример #7
0
        /// <summary>
        /// 具体执行
        /// </summary>
        private void RunThread()
        {
            var realName = ZeroIdentityHelper.CreateRealName(IsService, Name == Config.StationName ? null : Name);
            var identity = realName.ToAsciiBytes();

            ZeroTrace.SystemLog(StationName, "run", Config.WorkerCallAddress, Name, realName);
            var socket = ZSocket.CreateClientSocket(Config.WorkerResultAddress, ZSocketType.DEALER);

            using (var pool = ZmqPool.CreateZmqPool())
            {
                pool.Prepare(new[] { ZSocket.CreateClientSocket(Config.WorkerCallAddress, ZSocketType.PULL, identity) },
                             ZPollEvent.In);
                State = StationState.Run;
                while (CanLoop)
                {
                    if (!pool.Poll() || !pool.CheckIn(0, out var message))
                    {
                        continue;
                    }

                    Interlocked.Increment(ref RecvCount);
                    using (message)
                    {
                        if (!Unpack(message, out var item))
                        {
                            SendLayoutErrorResult(ref socket, item.Caller);
                            continue;
                        }

                        ApiCall(ref socket, item);
                    }
                }
            }

            ZeroTrace.SystemLog(StationName, "end", Config.WorkerCallAddress, Name, realName);
            socket.Dispose();
            _processSemaphore?.Release();
        }
Пример #8
0
        /// <summary>
        /// 命令轮询
        /// </summary>
        /// <returns></returns>
        private void Run()
        {
            var configs = ZeroApplication.Config.GetConfigs(p => p.StationType != ZeroStationType.Dispatcher);

            ZSocket[] sockets = new ZSocket[configs.Length * 2];
            int       idx     = 0;

            foreach (var config in configs)
            {
                sockets[idx++] = ZSocket.CreateServiceSocket($"inproc://{config.StationName}_Proxy", ZSocketType.ROUTER);
                sockets[idx++] = ZSocket.CreateClientSocket(config.RequestAddress, ZSocketType.DEALER);
            }

            WaitCount = 0;
            ZeroTrace.SystemLog("ConnectionProxy", "Listen");

            using (var pool = ZmqPool.CreateZmqPool())
            {
                pool.Prepare(sockets, ZPollEvent.In);
                _waitToken.Release();
                //SystemManager.Instance.HeartReady(StationName, RealName);
                while (!RunTaskCancel.Token.IsCancellationRequested)
                {
                    if (!pool.Poll())
                    {
                        continue;
                    }
                    Parallel.For(0, configs.Length, index =>
                    {
                        CheckCall(pool, index * 2);
                        CheckResult(pool, index * 2 + 1);
                    });
                }
            }
            _waitToken.Release();
        }