/// <summary> /// 转换为ByteRange /// </summary> /// <returns></returns> public override IByteRange ToByteRange() { var builder = new ByteBuilder(Endians.Big); builder.Add((byte)((byte)this.Frame + 128)); if (this.Content.Length > UInt16.MaxValue) { builder.Add((byte)127); builder.Add((ulong)this.Content.Length); } else if (this.Content.Length > 125) { builder.Add((byte)126); builder.Add((ushort)this.Content.Length); } else { builder.Add((byte)this.Content.Length); } builder.Add(this.Content); return builder.ToByteRange(); }
/// <summary> /// 调试视图 /// </summary> /// <param name="buidler">查看的对象</param> public DebugView(ByteBuilder buidler) { this.buidler = buidler; }
/// <summary> /// 输出文本内容 /// </summary> /// <param name="content">内容</param> public bool Write(string content) { if (content == null) { content = string.Empty; } var buffer = new ByteBuilder(Endians.Little); var contentBytes = this.Charset.GetBytes(content); if (this.wroteHeader == false) { this.wroteHeader = true; var headerByes = this.GetHeaderBytes(contentBytes.Length); buffer.Add(headerByes); } buffer.Add(contentBytes); return this.TrySend(buffer.ToByteRange()); }
/// <summary> /// 将参数序列化并写入为Body /// </summary> /// <param name="serializer">序列化工具</param> /// <param name="parameters">参数</param> /// <exception cref="SerializerException"></exception> public void SetBodyParameters(ISerializer serializer, params object[] parameters) { if (parameters == null || parameters.Length == 0) { return; } var builder = new ByteBuilder(Endians.Big); foreach (var item in parameters) { // 序列化参数为二进制内容 var paramBytes = serializer.Serialize(item); // 添加参数内容长度 builder.Add(paramBytes == null ? 0 : paramBytes.Length); // 添加参数内容 builder.Add(paramBytes); } this.Body = builder.ToArray(); }
/// <summary> /// 转换为ByteRange /// </summary> /// <returns></returns> public ByteRange ToByteRange() { var apiNameBytes = Encoding.UTF8.GetBytes(this.ApiName); var headLength = apiNameBytes.Length + 16; this.TotalBytes = this.Body == null ? headLength : headLength + this.Body.Length; this.ApiNameLength = (byte)apiNameBytes.Length; var builder = new ByteBuilder(Endians.Big); builder.Add(FastPacket.Mark); builder.Add(this.TotalBytes); builder.Add(this.ApiNameLength); builder.Add(apiNameBytes); builder.Add(this.Id); builder.Add(this.IsFromClient); builder.Add(this.IsException); builder.Add(this.Body); return builder.ToByteRange(); }