/// <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); }
private void ReadOldContentFileHashes(out string oldGlobalHash, out HashSet <string> oldTargetHashes) { oldGlobalHash = String.Empty; oldTargetHashes = new HashSet <string>(); if (File.Exists(buildContext.ContentFileHashesPath)) { try { var hashes = TsonSerializer.Deserialize <ContentFileHashesFile>( File.ReadAllText(buildContext.ContentFileHashesPath)); oldGlobalHash = hashes.Global; foreach (var hash in hashes.Targets) { oldTargetHashes.Add(hash); } } catch { // Bad file, don't use it again File.Delete(buildContext.ContentFileHashesPath); } } }
/// <summary> /// 序列化到Tson /// </summary> public void Serialize(TsonSerializer writer) { writer.Begin(TYPE_INDEX_STRINGARGUMENT, 1); //参数 if (!writer.IsEmpty(this._argument)) { writer.WriteIndex(FIELD_INDEX_STRINGARGUMENT_ARGUMENT); writer.Write(this._argument); } writer.End(); }
/// <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> public void WriteCommandToBuffer() { m_buffer_len = RpcEnvironment.NETCOMMAND_LEN; if (Command.Data != null) { m_buffer_len += Command.Data.SafeBufferLength; } m_bufer = new byte[m_buffer_len]; m_postion = 0; if (!string.IsNullOrEmpty(Command.command)) { var bytes = Encoding.ASCII.GetBytes(Command.command); for (int i = 0; i < bytes.Length && i < RpcEnvironment.GUID_LEN; i++) { m_bufer[m_postion++] = bytes[i]; } } if (!string.IsNullOrEmpty(Command.token)) { m_postion = RpcEnvironment.GUID_LEN; var bytes = Encoding.ASCII.GetBytes(Command.token); for (int i = 0; i < bytes.Length && i < RpcEnvironment.GUID_LEN; i++) { m_bufer[m_postion++] = bytes[i]; } } m_postion = RpcEnvironment.GUID_LEN << 1; Write(Command.cmdId); Write(Command.tryNum); Write(Command.cmdState); uint crc = CrcHelper.Crc(m_bufer, RpcEnvironment.NETCOMMAND_BODY_LEN); Write(crc); if (Command.Data == null) { Command.dataLen = 0; return; } TsonSerializer serializer = new TsonSerializer(m_bufer, m_buffer_len, RpcEnvironment.NETCOMMAND_HEAD_LEN); Command.Data.Serialize(serializer); Command.dataLen = serializer.DataLen; m_end_postion = serializer.EndPostion; m_data_len = m_end_postion; }
/// <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); }
private void WriteNewContentFileHashes(List <BuildTarget> buildTargets) { ContentFileHashesFile hashes = new ContentFileHashesFile() { Global = buildContext.GlobalHash, Targets = buildTargets.Select(t => t.Hash).ToArray() }; try { var tson = TsonSerializer.Serialize(hashes); File.WriteAllText(buildContext.ContentFileHashesPath, tson); } catch { WriteWarning("Unable to write content hash file '{0}'".CultureFormat(buildContext.ContentFileHashesPath)); } }
/// <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); }