From 65f59c0568860079f64f71ae55b02f4f9edb6cdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Fri, 22 Mar 2024 13:17:36 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=E6=A0=87=E5=87=86=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E5=B0=81=E5=8C=85=E6=96=B0=E5=A2=9Ewireshark=E5=8D=8F=E8=AE=AE?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=8C=E4=BE=BF=E4=BA=8E=E5=88=86=E6=9E=90?= =?UTF-8?q?=E7=BD=91=E7=BB=9C=E5=8C=85=EF=BC=9BStandardCodec=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=BC=96=E7=A0=81=E5=9F=BA=E7=A1=80=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Doc/newlife.lua | 71 ++++++++++++++++++++++ NewLife.Core/Net/Handlers/StandardCodec.cs | 16 +++++ 2 files changed, 87 insertions(+) create mode 100644 Doc/newlife.lua diff --git a/Doc/newlife.lua b/Doc/newlife.lua new file mode 100644 index 000000000..ec675e156 --- /dev/null +++ b/Doc/newlife.lua @@ -0,0 +1,71 @@ +do + local p_newlife = Proto("newlife", "新生命标准网络封包") + + -- https://www.wireshark.org/docs/wsdg_html_chunked/lua_module_Proto.html#lua_class_ProtoField + local FF_flag = { + [8] = "[Reply]", + [7] = "[Error/Oneway]", + [3] = "[Encrypted]", + [2] = "[Compressed]", + [1] = "[Binary]" + } + + local f_flag = ProtoField.uint8("NewLife.flag", "标记", base.HEX, FF_flag, 0xFF) + -- local f_flag = ProtoField.uint8("NewLife.flag", "标记", base.HEX) + local f_seq = ProtoField.uint8("NewLife.seq", "序列号", base.DEC) + local f_length = ProtoField.uint16("NewLife.length", "长度", base.DEC) + -- local f_data = ProtoField.string("NewLife.data", "内容", base.UNICODE) + local f_data = ProtoField.bytes("NewLife.data", "数据", base.SPACE) + + p_newlife.fields = {f_flag, f_seq, f_length, f_data} + + local data_dis = Dissector.get("data") + + local function NewLife_dissector(buf, pkt, root) + local buf_len = buf:len(); + if buf_len < 4 then + return false + end + + local tvb = buf:range() + local v_flag = tvb(0, 1) + local v_seq = tvb(1, 1) + local v_length = tvb(2, 2) + local flag = tvb(0, 1):uint() + + local len = tvb(2, 2):le_uint() + local v_data = tvb(4, len) + + pkt.cols.protocol = "NewLife" + + local t = root:add(p_newlife, buf) + t:add(f_flag, v_flag) + t:add(f_seq, v_seq) + t:add_le(f_length, v_length) + + -- t:add_packet_field(f_data, v_data, ENC_UTF_8 + ENC_STRING) + t:add(f_data, v_data) + + return true + end + + function p_newlife.dissector(buf, pkt, root) + if NewLife_dissector(buf, pkt, root) then + -- valid NewLife diagram + else + data_dis:call(buf, pkt, root) + end + end + + local udp_encap_table = DissectorTable.get("udp.port") + udp_encap_table:add(5500, p_newlife) + udp_encap_table:add(9999, p_newlife) + udp_encap_table:add(777, p_newlife) + udp_encap_table:add(12345, p_newlife) + + local tcp_encap_table = DissectorTable.get("tcp.port") + tcp_encap_table:add(5500, p_newlife) + tcp_encap_table:add(9999, p_newlife) + tcp_encap_table:add(777, p_newlife) + tcp_encap_table:add(12345, p_newlife) +end diff --git a/NewLife.Core/Net/Handlers/StandardCodec.cs b/NewLife.Core/Net/Handlers/StandardCodec.cs index 6d10c1249..8b6be1d1f 100644 --- a/NewLife.Core/Net/Handlers/StandardCodec.cs +++ b/NewLife.Core/Net/Handlers/StandardCodec.cs @@ -1,6 +1,8 @@ using NewLife.Data; using NewLife.Messaging; using NewLife.Model; +using NewLife.Reflection; +using NewLife.Serialization; namespace NewLife.Net.Handlers; @@ -18,6 +20,20 @@ public class StandardCodec : MessageCodec /// public override Object? Write(IHandlerContext context, Object message) { + // 基础类型 + if (message is String str) + { + message = new Packet(str.GetBytes()); + } + else if (message.GetType().GetTypeCode() != TypeCode.Object) + { + message = Binary.FastWrite(message); + } + else if (message is Byte[] buf) + { + message = new Packet(buf); + } + if (UserPacket && message is Packet pk) message = new DefaultMessage { Payload = pk, Sequence = (Byte)Interlocked.Increment(ref _gid) }; else if (message is DefaultMessage msg && !msg.Reply && msg.Sequence == 0)