Commits

Anonymous committed c211532

process many responses in one tcp packet

Comments (0)

Files changed (2)

src/emongo_packet.erl

 				documents = Documents
 			},
 			{Resp, Tail}
-	end.
+	end;
+
+decode_response(_) ->
+    undefined.
 
 index_name([], Bin) -> Bin;
 index_name([{Key, Val}|Tail], Bin) ->

src/emongo_server.erl

 
 handle_info({tcp, _Socket, Data}, State) ->
     Leftover = <<(State#state.leftover)/binary, Data/binary>>,
+    {noreply, process_bin(State#state{leftover= <<>>}, Leftover)};
 
-    case emongo_packet:decode_response(Leftover) of
+handle_info({tcp_closed, _Socket}, _State) ->
+    exit(tcp_closed);
+
+handle_info({tcp_error, _Socket, Reason}, _State) ->
+    exit({tcp_error, Reason}).
+
+
+terminate(_, State) -> gen_tcp:close(State#state.socket).
+
+
+code_change(_Old, State, _Extra) -> {ok, State}.
+
+%% internal
+
+
+process_bin(State, <<>>) ->
+    State;
+
+process_bin(State, Bin) ->
+    case emongo_packet:decode_response(Bin) of
         undefined ->
-            {noreply, State#state{leftover=Leftover}};
+            State#state{leftover=Bin};
         
         {Resp, Tail} ->
             ResponseTo = (Resp#response.header)#header.response_to,
             case lists:keytake(ResponseTo, 1, State#state.requests) of
                 false ->
                     cleanup_cursor(Resp, ResponseTo, State),
-                    {noreply, State#state{leftover=Tail}};
-
+                    process_bin(State, Tail);
+                
                 {value, {_, From}, Requests} ->
                     case is_aborted(ResponseTo) of
                         false ->
                         true ->
                             cleanup_cursor(Resp, ResponseTo, State)
                     end,
-                    {noreply, State#state{requests=Requests, leftover=Tail}}
+                    process_bin(State#state{requests=Requests}, Tail)
             end
-    end;
-
-handle_info({tcp_closed, _Socket}, _State) ->
-    exit(tcp_closed);
-
-handle_info({tcp_error, _Socket, Reason}, _State) ->
-    exit({tcp_error, Reason}).
-
-
-terminate(_, State) -> gen_tcp:close(State#state.socket).
-
-
-code_change(_Old, State, _Extra) -> {ok, State}.
-
-%% internal
+    end.
 
 
 is_aborted(ReqId) ->