anchor.diff:  %
   
   % @hidden send a Piqi-RPC request
  -send_rpc_request(Port, Name, ArgsData) ->
  -    Request = #piqi_rpc_request{ name = Name, data = ArgsData },
  -    Data = piqi_rpc_piqi:gen_request(Request),
  -    send_rpc_packet(Port, Data).
  -
  -
  --ifdef(USE_GEN_SERVER2).
  -% When we use gen_server2, responses from the port are received through the
  -% gen_server2 receive loop and handle_info() function. They are also prioretized
  -% to be received before all calls and casts that may be sitting in the queue.
  -receive_rpc_response(State) -> State.
  --else.
  -receive_rpc_response(State) when State#state.queue_len < ?MAX_PIPELINE_SIZE ->
  -    % we can send more requests before receiving responses
  -    State;
  -receive_rpc_response(State = #state{port = Port}) ->
  -    receive
  -        {Port, {data, Data}} ->
  -            handle_rpc_response(Data, State);
  -        {'EXIT', Port, Reason} ->
  -            StopReason = {?PIQI_TOOLS_ERROR, {'port_command_exited', Reason}},
  -            exit(StopReason)
  -    end.
  --endif.
  +% NOTE: responses will be handled by a separate port_receiver() process
  +send_rpc_request(Port, CallerRef, Request) ->
  +    send_rpc_packet(Port, CallerRef, Request).
   
   
   % @hidden send a Piqi-RPC packet
  -send_rpc_packet(Port, Data) ->
  -    Packet = piqirun:gen_block(Data),
  +send_rpc_packet(Port, CallerRef, Payload) ->
  +    CallerRefSize = size(CallerRef),
  +    Command = [<<CallerRefSize:16>>, CallerRef, Payload],
       % NOTE: this is a blocking call. If the port is busy, the process will be
       % suspended until the port becomes available.
  -    true = erlang:port_command(Port, Packet).
  -
  -    % XXX: testing has proven that use of non-blocking version doesn't give any
  -    % benefits. The hypothesis was that, when if we use gen_server2, instead of
  -    % blocking we can do something useful, such as receive and process responses
  -    % and in the meantime keep trying to resend the port command. It turns out
  -    % that this is fruitless, because instead of processing responses we are
  -    % burning CPU cicles trying to re-send port_command (we did that by passing
  -    % the port command through our own even loop)
  -    %
  -    % erlang:port_command(Port, Packet, [nosuspend]) -> true | false
  +    true = erlang:port_command(Port, Command).
  +
  +
  +parse_rpc_packet(Packet) ->
  +    try
  +        <<CallerRefSize:16, Rest/binary>> = Packet,
  +        % return a tuple of two binaries: {CallerRef, Payload}
  +        split_binary(Rest, CallerRefSize)
  +    catch
  +        _:_ ->
  +            Reason = {?PIQI_TOOLS_ERROR, {'received_invalid_rpc_packet', Packet}},
  +            exit(Reason)
  +    end.
   
   
   %
  
positive.code: 
  -define(PIQI_TOOLS_ERROR, 'piqi_tools_error').
  
  
  % gen_server state
  -record(state, {
      % Erlang port id
      port :: port(),
  
      % data remaining from the previous packet received from the port
      prev_data = <<>> :: binary(),
  
      % queue of gen_server Clients or request continuations
      queue = queue:new() :: any(),
  
      % queue length
      queue_len = 0 :: non_neg_integer()
  }).
  
  
  
negative.code: -include("piqi_tools_piqi.hrl").
  
  
  %-define(DEBUG, 1).
  -ifdef(DEBUG).
  -include("debug.hrl").
  -endif.
  
  
  