示例#1
0
 /// <summary>
 ///   记录日志
 /// </summary>
 /// <param name="infos"> 日志消息 </param>
 void ILogRecorder.RecordLog(List <RecordInfo> infos)
 {
     if (_socket != null)
     {
         int idx = 0;
         while (idx <= infos.Count)
         {
             byte[] buf;
             using (TsonSerializer serializer = new TsonSerializer(TsonDataType.Array))
             {
                 serializer.WriteType(TsonDataType.Object);
                 int size = infos.Count - idx;
                 if (size > 255)
                 {
                     size = 255;
                 }
                 serializer.WriteLen(size);
                 for (; size > 0 && idx < infos.Count; idx++, --size)
                 {
                     serializer.Begin();
                     RecordInfoTson.ToTson(serializer, infos[idx]);
                     serializer.End();
                 }
                 buf = serializer.Close();
             }
             if (_socket.SendTo(LogDescription, _logsByte, buf))
             {
                 return;
             }
         }
     }
     LogRecorder.BaseRecorder.RecordLog(infos);
 }
示例#2
0
        /// <summary>
        /// 具体执行
        /// </summary>
        /// <returns>返回False表明需要重启</returns>
        protected sealed override bool RunInner(CancellationToken token)
        {
            _socket = ZSocket.CreateRequestSocket(Config.RequestAddress, Identity);
            SystemManager.Instance.HeartReady(StationName, RealName);
            State = StationState.Run;
            int cnt = 0;

            while (CanLoop)
            {
                Thread.Sleep(10);
                if (token.IsCancellationRequested)
                {
                    break;
                }
                if (++cnt == 36)
                {
                    GC.Collect();
                    cnt = 0;
                }
                var array = Items.Switch();
                if (array == null)
                {
                    Thread.Sleep(10);
                    continue;
                }
                if (TsonOperator == null)
                {
                    do
                    {
                        var datas = array.Count > 300 ? array.Take(255).ToArray() : array.ToArray();
                        while (!_socket.Publish(Name, datas) && CanLoop)
                        {
                            Thread.Sleep(10);
                        }
                        if (array.Count > 300)
                        {
                            array.RemoveRange(0, 255);
                        }
                    } while (array.Count > 300);
                    continue;
                }

                int idx = 0;
                while (idx < array.Count)
                {
                    byte[] buf;
                    using (TsonSerializer serializer = new TsonSerializer(TsonDataType.Array))
                    {
                        serializer.WriteType(TsonDataType.Object);
                        int size = array.Count - idx;
                        if (size > 255)
                        {
                            size = 255;
                        }
                        serializer.WriteLen(size);
                        for (; size > 0 && idx < array.Count; idx++, --size)
                        {
                            if (array[idx] == null)
                            {
                                serializer.WriteType(TsonDataType.Empty);
                                continue;
                            }
                            using (TsonObjectSerializeScope.CreateScope(serializer))
                            {
                                if (array[idx] != null)
                                {
                                    TsonOperator.ToTson(serializer, array[idx]);
                                }
                            }
                        }
                        buf = serializer.Close();
                    }
                    while (!_socket.Publish(ZeroPublishExtend.PubDescriptionTson2, Name, array.Count.ToString(), buf) && CanLoop)
                    {
                        Thread.Sleep(10);
                    }
                }
            }
            SystemManager.Instance.HeartLeft(StationName, RealName);
            _socket.TryClose();
            return(true);
        }