Example #1
0
 private bool Request(string msg, ref int retry)
 {
     if (++retry > 5)
     {
         LogRecorder.Error($"数据事件服务(EntityEvent)无法连接!\r\n{msg}");
         return(false);
     }
     try
     {
         lock (this)
         {
             _socket.Request(msg);
         }
         return(true);
     }
     catch (Exception e)
     {
         LogRecorder.Exception(e);
         StationProgram.WriteLine($"【EntityEvent】publish error =>{e.Message}");
         retry++;
         Thread.Sleep(10);
         CreateSocket();
         return(Request(msg, ref retry));
     }
 }
Example #2
0
        /// <inheritdoc />
        /// <summary>
        /// 命令轮询
        /// </summary>
        /// <returns></returns>
        public sealed override bool Run()
        {
            lock (this)
            {
                if (RunState != StationState.Run)
                {
                    RunState = StationState.Start;
                }
                if (_socket != null)
                {
                    return(false);
                }
            }
            _inPoll  = false;
            _inHeart = false;

            _socket = new RequestSocket();
            try
            {
                _socket.Options.Identity          = RealName.ToAsciiBytes();
                _socket.Options.ReconnectInterval = new TimeSpan(0, 0, 0, 0, 200);
                _socket.Connect(Config.InnerAddress);
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
                RunState = StationState.Failed;
                CloseSocket();
                StationProgram.WriteLine($"【{StationName}】connect error =>{e.Message}");
                return(false);
            }
            try
            {
                var word = _socket.Request("@", JsonConvert.SerializeObject(Config));
                if (word != "wecome")
                {
                    CloseSocket();
                    RunState = StationState.Failed;
                    StationProgram.WriteLine($"【{StationName}】proto error");
                    return(false);
                }
            }
            catch (Exception e)
            {
                LogRecorder.Exception(e);
                RunState = StationState.Failed;
                CloseSocket();
                StationProgram.WriteLine($"【{StationName}】request error =>{e.Message}");
                return(false);
            }

            RunState = StationState.Run;
            _items   = MulitToOneQueue <List <string> > .Load(CacheFileName);

            Task.Factory.StartNew(CommandTask);

            var task1 = Task.Factory.StartNew(PollTask);

            task1.ContinueWith(OnTaskStop);

            var task2 = Task.Factory.StartNew(HeartbeatTask);

            task2.ContinueWith(OnTaskStop);

            while (!_inHeart || !_inPoll)
            {
                Thread.Sleep(50);
            }
            StationProgram.WriteLine($"【{StationName}:{RealName}】runing...");
            return(true);
        }