diff options
Diffstat (limited to 'p11p-daemon/src/p11p_server.erl')
-rw-r--r-- | p11p-daemon/src/p11p_server.erl | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/p11p-daemon/src/p11p_server.erl b/p11p-daemon/src/p11p_server.erl index accc86a..684c6e1 100644 --- a/p11p-daemon/src/p11p_server.erl +++ b/p11p-daemon/src/p11p_server.erl @@ -8,7 +8,7 @@ %% API. -export([start_link/1]). --export([reply/2]). +-export([add_to_clientbuf/2, reply/2]). %% Genserver callbacks. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, @@ -19,7 +19,8 @@ tokname :: string(), sockpath :: string(), % FIXME: filename(3erl) socket :: gen_tcp:socket(), - msg :: p11rpc_msg() | undefined + msg :: p11rpc_msg() | undefined, + clientbuf = <<>> :: binary() }). %% API. @@ -27,8 +28,13 @@ start_link(Args) -> gen_server:start_link(?MODULE, Args, []). -reply(Pid, Data) -> - gen_server:cast(Pid, {response, Data}). +-spec add_to_clientbuf(pid(), binary()) -> binary(). +add_to_clientbuf(Pid, Data) -> + gen_server:call(Pid, {add_to_clientbuf, Data}). + +-spec reply(pid(), p11rpc_msg()) -> ok. +reply(Pid, Response) -> + gen_server:cast(Pid, {response, Response}). %% Genserver callbacks. init([Token, SocketPath, Socket]) -> @@ -37,9 +43,11 @@ init([Token, SocketPath, Socket]) -> gen_server:cast(self(), accept), % Perform accept in gen-server loop. {ok, #state{tokname = Token, sockpath = SocketPath, socket = Socket}}. -handle_call(Request, _From, State) -> - - lager:debug("~p: Unhandled call: ~p~n", [self(), Request]), +handle_call({add_to_clientbuf, Data}, _From, #state{clientbuf = Buf} = State) -> + NewBuf = <<Buf/binary, Data/binary>>, + {reply, NewBuf, State#state{clientbuf = NewBuf}}; +handle_call(Call, _From, State) -> + lager:debug("~p: Unhandled call: ~p~n", [self(), Call]), {reply, unhandled, State}. handle_cast(accept, State = #state{tokname = TokName, sockpath = SocketPath, socket = ListenSocket}) -> @@ -57,10 +65,12 @@ handle_cast(accept, State = #state{tokname = TokName, sockpath = SocketPath, soc lager:debug("~p: listening socket closed", [self()]), {stop, normal, State} end; -handle_cast({response, Data}, #state{socket = ClientPort} = State) -> - lager:debug("~p: received ~B octets from remote", [self(), length(Data)]), - ok = gen_tcp:send(ClientPort, Data), - {noreply, State}; +handle_cast({response, Response}, #state{socket = ClientPort, clientbuf = Buf} = State) -> + %%lager:debug("~p: received ~B octets from remote", [self(), length(Data)]), + Data = p11p_rpc:serialise(Response), + NewBuf = <<Buf/binary, Data/binary>>, + ok = gen_tcp:send(ClientPort, NewBuf), % TODO: what about short writes? + {noreply, State#state{clientbuf = <<>>}}; handle_cast(Cast, State) -> lager:debug("~p: Unhandled cast: ~p~n", [self(), Cast]), {noreply, State}. @@ -69,7 +79,7 @@ handle_info({tcp, Port, Data}, #state{tokname = TokName, msg = Msg} = State) whe lager:debug("~p: received ~B octets from client on socket ~p, from new client", [self(), size(Data), Port]), <<Version:8, NewData/binary>> = Data, - p11p_remote:add_to_outbuf(p11p_remote_manager:remote_for_token(TokName), <<Version>>), + p11p_remote:add_to_outbuf(p11p_remote_manager:remote_for_token(TokName), <<Version>>), % FIXME: token reference needs to be cached, for consistancy at least across this and the next function head NewState = handle_client_data(State, p11p_rpc:new(), NewData), {noreply, NewState}; handle_info({tcp, Port, Data}, #state{msg = Msg} = State) -> |