Refactor send_fun in protocol and other connection module
Prior to this change, in the send function, the packet is forced to use emqx:serialize to serialize packet, it is a wrong design because other plugins which need to transform the mqtt packet to other packets can not use their own serialize function to serialize packet. This change solve the problem issued above.
This commit is contained in:
parent
599121052a
commit
a748e8f1d8
|
@ -165,7 +165,8 @@ init_limiter({Rate, Burst}) ->
|
|||
esockd_rate_limit:new(Rate, Burst).
|
||||
|
||||
send_fun(Transport, Socket, Peername) ->
|
||||
fun(Data) ->
|
||||
fun(Serialize, Packet, Options) ->
|
||||
Data = Serialize(Packet, Options),
|
||||
try Transport:async_send(Socket, Data) of
|
||||
ok ->
|
||||
?LOG(debug, "SEND ~p", [iolist_to_binary(Data)], #state{peername = Peername}),
|
||||
|
@ -408,4 +409,3 @@ maybe_gc(#state{}, {publish, _PacketId, #message{payload = Payload}}) ->
|
|||
ok = emqx_gc:inc(1, Oct);
|
||||
maybe_gc(_, _) ->
|
||||
ok.
|
||||
|
||||
|
|
|
@ -634,4 +634,3 @@ fixqos(?PUBREL, 0) -> 1;
|
|||
fixqos(?SUBSCRIBE, 0) -> 1;
|
||||
fixqos(?UNSUBSCRIBE, 0) -> 1;
|
||||
fixqos(_Type, QoS) -> QoS.
|
||||
|
||||
|
|
|
@ -555,7 +555,7 @@ deliver({disconnect, _ReasonCode}, PState) ->
|
|||
-spec(send(emqx_mqtt_types:packet(), state()) -> {ok, state()} | {error, term()}).
|
||||
send(Packet = ?PACKET(Type), PState = #pstate{proto_ver = Ver, sendfun = SendFun}) ->
|
||||
trace(send, Packet, PState),
|
||||
case SendFun(emqx_frame:serialize(Packet, #{version => Ver})) of
|
||||
case SendFun(fun emqx_frame:serialize/2, Packet, #{version => Ver}) of
|
||||
ok ->
|
||||
emqx_metrics:sent(Packet),
|
||||
{ok, inc_stats(send, Type, PState)};
|
||||
|
|
|
@ -144,7 +144,8 @@ websocket_init(#state{request = Req, options = Options}) ->
|
|||
idle_timeout = IdleTimout}}.
|
||||
|
||||
send_fun(WsPid) ->
|
||||
fun(Data) ->
|
||||
fun(Serialize, Packet, Options) ->
|
||||
Data = Serialize(Packet, Options),
|
||||
BinSize = iolist_size(Data),
|
||||
emqx_metrics:inc('bytes/sent', BinSize),
|
||||
put(send_oct, get(send_oct) + BinSize),
|
||||
|
@ -299,4 +300,3 @@ stop(Error, State) ->
|
|||
|
||||
wsock_stats() ->
|
||||
[{Key, get(Key)} || Key <- ?SOCK_STATS].
|
||||
|
||||
|
|
Loading…
Reference in New Issue