/// <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); }
/// <summary> /// 广播 /// </summary> /// <param name="data"></param> public void Publish(TData data) { if (data == null) { return; } if (!CanLoop) { datas.Add(data); return; } int idx = random.Next(0, 64); var socket = sockets[idx]; lock (socket) { try { if (TsonOperator != null) { byte[] buf; using (TsonSerializer serializer = new TsonSerializer()) { TsonOperator.ToTson(serializer, data); buf = serializer.Close(); } socket.SendTo(ZeroPublishExtend.PubDescriptionTson, data.Title.ToZeroBytes(), ApiContext.RequestContext.RequestId.ToZeroBytes(), ZeroApplication.Config.RealName.ToZeroBytes(), buf); } else { socket.SendTo(ZeroPublishExtend.PubDescriptionJson, data.Title.ToZeroBytes(), ApiContext.RequestContext.RequestId.ToZeroBytes(), ZeroApplication.Config.RealName.ToZeroBytes(), data.ToZeroBytes()); } } catch (Exception e) { ZeroTrace.WriteException("Pub", e, socket.Connects.LinkToString(','), $"Socket Ptr:{socket.SocketPtr}"); datas.Add(data); } } }
/// <summary> /// 记录日志 /// </summary> /// <param name="info"> 日志消息 </param> void ILogRecorder.RecordLog(RecordInfo info) { if (_socket != null) { byte[] buf; using (TsonSerializer serializer = new TsonSerializer()) { RecordInfoTson.ToTson(serializer, info); buf = serializer.Close(); } if (_socket.SendTo(LogDescription, _logsByte, buf)) { return; } } LogRecorder.BaseRecorder.RecordLog(info); }
/// <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); }