Browse Source

recursive message decoding

Anton Lebedevich 13 years ago
parent
commit
5f6a73b7ad
1 changed files with 12 additions and 3 deletions
  1. 12 3
      src/pgsql_wire.erl

+ 12 - 3
src/pgsql_wire.erl

@@ -1,7 +1,7 @@
 -module(pgsql_wire).
 -module(pgsql_wire).
 
 
 -export([init/1,
 -export([init/1,
-         decode/2,
+         decode_messages/2,
          encode/2,
          encode/2,
          encode/3]).
          encode/3]).
 
 
@@ -12,8 +12,8 @@
 init(Options) ->
 init(Options) ->
     #state{options = Options}.
     #state{options = Options}.
 
 
-decode(Data, State = #state{}) ->
-    {ok, State}.
+decode_messages(Bin, #state{tail = Tail} = State) ->
+    decode_messages([], <<Bin/binary, Tail/binary>>, State).
 
 
 encode(Data, State = #state{}) ->
 encode(Data, State = #state{}) ->
     Bin = iolist_to_binary(Data),
     Bin = iolist_to_binary(Data),
@@ -22,3 +22,12 @@ encode(Data, State = #state{}) ->
 encode(Type, Data, State = #state{}) ->
 encode(Type, Data, State = #state{}) ->
     Bin = iolist_to_binary(Data),
     Bin = iolist_to_binary(Data),
     <<Type:8, (byte_size(Bin) + 4):?int32, Bin/binary>>.
     <<Type:8, (byte_size(Bin) + 4):?int32, Bin/binary>>.
+
+decode_messages(Acc, <<Type:8, Len:?int32, Rest/binary>> = Bin, State) ->
+    Len2 = Len - 4,
+    case Rest of
+        <<Data:Len2/binary, Tail/binary>> ->
+            decode([{Type, Data} | Acc], Tail, State);
+        _Other ->
+            {lists:reverse(Acc), State#state{tail = Bin}}
+    end;