From a96bfc7da12658a4daa81b5437bd7ef2255c0a96 Mon Sep 17 00:00:00 2001 From: anlicheng <244108715@qq.com> Date: Wed, 17 Sep 2025 17:48:03 +0800 Subject: [PATCH] fix codec --- apps/efka/src/efka_codec.erl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/apps/efka/src/efka_codec.erl b/apps/efka/src/efka_codec.erl index 0e0760d..71787cd 100644 --- a/apps/efka/src/efka_codec.erl +++ b/apps/efka/src/efka_codec.erl @@ -20,7 +20,6 @@ encode(PacketType, Packet) when is_integer(PacketType) -> Bin = encode0(Packet), <>. - encode0(#auth_request{uuid = UUID, username = Username, salt = Salt, token = Token, timestamp = Timestamp}) -> iolist_to_binary([ marshal(?Bytes, UUID), @@ -69,6 +68,7 @@ encode0(#event{service_id = ServiceId, event_type = EventType, params = Params}) marshal(?Bytes, Params) ]). +-spec decode(Bin :: binary()) -> any(). decode(<>) -> Fields = unmarshal(Packet), decode0(PacketType, Fields). @@ -93,15 +93,17 @@ decode0(?MESSAGE_EVENT, [ServiceId, EventType, Params]) -> %%% helper methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% -marshal(?I32, Field) -> +-spec marshal(Type :: integer(), Field :: any()) -> binary(). +marshal(?I32, Field) when is_integer(Field) -> <>; marshal(?Bytes, undefined) -> <>; -marshal(?Bytes, Field) -> +marshal(?Bytes, Field) when is_binary(Field) -> Len = byte_size(Field), <>. -unmarshal(Bin) -> +-spec unmarshal(Bin :: binary()) -> Components :: [any()]. +unmarshal(Bin) when is_binary(Bin) -> unmarshal(Bin, []). unmarshal(<<>>, Acc) -> lists:reverse(Acc);