Skip to content

Commit

Permalink
decode incoming messages
Browse files Browse the repository at this point in the history
  • Loading branch information
mabrek committed Sep 4, 2011
1 parent 8e78752 commit f39e396
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/pgsql_sock.erl
Expand Up @@ -67,17 +67,18 @@ handle_cast(cancel, State = #state{backend = {Pid, Key}}) ->
gen_tcp:close(Sock),
{noreply, State}.

handle_info({_, _Sock, Data}, #state{tail = Tail} = State) ->
State2 = decode(<<Tail/binary, Data/binary>>, State),
{noreply, State2};

handle_info({Closed, _Sock}, State)
when Closed == tcp_closed; Closed == ssl_closed ->
{stop, sock_closed, State};

handle_info({Error, _Sock, Reason}, State)
when Error == tcp_error; Error == ssl_error ->
{stop, {sock_error, Reason}, State}.
{stop, {sock_error, Reason}, State};

handle_info({_, _Sock, Data}, #state{decoder = Decoder} = State) ->
{Messages, Decoder2} = pgsql_wire:decode_messages(Data, Decoder),
State2 = State#{decoder = Decoder2},
{noreply, lists:foldl(fun on_mesage/2, State2, Messages)}.

terminate(_Reason, _State) ->
ok.
Expand Down Expand Up @@ -112,6 +113,9 @@ send(Data, State#state{mod = Mod, sock = Sock, decoder = Decoder}) ->
send(Type, Data, State#state{mod = Mod, sock = Sock, decoder = Decoder}) ->
Mod:send(Sock, pgsql_wire:encode(Type, Data, Decoder)).

on_message(_Msg, State) ->
State.

decode(<<Type:8, Len:?int32, Rest/binary>> = Bin, #state{c = C} = State) ->
Len2 = Len - 4,
case Rest of
Expand Down

0 comments on commit f39e396

Please sign in to comment.