From bc1b3fdb77eac0e2d705a9d363d6f3062bb146ee Mon Sep 17 00:00:00 2001 From: David Reiss Date: Mon, 30 Aug 2010 22:05:47 +0000 Subject: [PATCH] erlang: Don't use a separate process for framed_transport git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@990992 13f79535-47bb-0310-9956-ffa450edef68 --- lib/erl/src/thrift_framed_transport.erl | 165 ++++-------------------- 1 file changed, 28 insertions(+), 137 deletions(-) diff --git a/lib/erl/src/thrift_framed_transport.erl b/lib/erl/src/thrift_framed_transport.erl index c1c8850c..5c042469 100644 --- a/lib/erl/src/thrift_framed_transport.erl +++ b/lib/erl/src/thrift_framed_transport.erl @@ -19,16 +19,11 @@ -module(thrift_framed_transport). --behaviour(gen_server). -behaviour(thrift_transport). %% API -export([new/1]). -%% gen_server callbacks --export([init/1, handle_call/3, handle_cast/2, handle_info/2, - terminate/2, code_change/3]). - %% thrift_transport callbacks -export([write/2, read/2, flush/1, close/1]). @@ -36,90 +31,41 @@ read_buffer, % iolist() write_buffer % iolist() }). --type state() :: pid(). +-type state() :: #framed_transport{}. -include("thrift_transport_impl.hrl"). -%%==================================================================== -%% API -%%==================================================================== -%%-------------------------------------------------------------------- -%% Function: start_link() -> {ok,Pid} | ignore | {error,Error} -%% Description: Starts the server -%%-------------------------------------------------------------------- new(WrappedTransport) -> - case gen_server:start_link(?MODULE, [WrappedTransport], []) of - {ok, Pid} -> - thrift_transport:new(?MODULE, Pid); - Else -> - Else - end. + State = #framed_transport{wrapped = WrappedTransport, + read_buffer = [], + write_buffer = []}, + thrift_transport:new(?MODULE, State). -%%-------------------------------------------------------------------- -%% Function: write(Transport, Data) -> ok -%% -%% Data = iolist() -%% -%% Description: Writes data into the buffer -%%-------------------------------------------------------------------- -write(Transport, Data) -> - {Transport, gen_server:call(Transport, {write, Data})}. +%% Writes data into the buffer +write(State = #framed_transport{write_buffer = WBuf}, Data) -> + {State#framed_transport{write_buffer = [WBuf, Data]}, ok}. -%%-------------------------------------------------------------------- -%% Function: flush(Transport) -> ok -%% -%% Description: Flushes the buffer through to the wrapped transport -%%-------------------------------------------------------------------- -flush(Transport) -> - {Transport, gen_server:call(Transport, flush)}. +%% Flushes the buffer through to the wrapped transport +flush(State0 = #framed_transport{write_buffer = Buffer, + wrapped = Wrapped0}) -> + FrameLen = iolist_size(Buffer), + Data = [<>, Buffer], -%%-------------------------------------------------------------------- -%% Function: close(Transport) -> ok -%% -%% Description: Closes the transport and the wrapped transport -%%-------------------------------------------------------------------- -close(Transport) -> - {Transport, gen_server:cast(Transport, close)}. + {Wrapped1, Response} = thrift_transport:write(Wrapped0, Data), -%%-------------------------------------------------------------------- -%% Function: Read(Transport, Len) -> {ok, Data} -%% -%% Data = binary() -%% -%% Description: Reads data through from the wrapped transoprt -%%-------------------------------------------------------------------- -read(Transport, Len) when is_integer(Len) -> - {Transport, gen_server:call(Transport, {read, Len})}. + {Wrapped2, _} = thrift_transport:flush(Wrapped1), -%%==================================================================== -%% gen_server callbacks -%%==================================================================== + State1 = State0#framed_transport{wrapped = Wrapped2, write_buffer = []}, + {State1, Response}. -%%-------------------------------------------------------------------- -%% Function: init(Args) -> {ok, State} | -%% {ok, State, Timeout} | -%% ignore | -%% {stop, Reason} -%% Description: Initiates the server -%%-------------------------------------------------------------------- -init([Wrapped]) -> - {ok, #framed_transport{wrapped = Wrapped, - read_buffer = [], - write_buffer = []}}. +%% Closes the transport and the wrapped transport +close(State = #framed_transport{wrapped = Wrapped0}) -> + {Wrapped1, Result} = thrift_transport:close(Wrapped0), + NewState = State#framed_transport{wrapped = Wrapped1}, + {NewState, Result}. -%%-------------------------------------------------------------------- -%% Function: %% handle_call(Request, From, State) -> {reply, Reply, State} | -%% {reply, Reply, State, Timeout} | -%% {noreply, State} | -%% {noreply, State, Timeout} | -%% {stop, Reason, Reply, State} | -%% {stop, Reason, State} -%% Description: Handling call messages -%%-------------------------------------------------------------------- -handle_call({write, Data}, _From, State = #framed_transport{write_buffer = WBuf}) -> - {reply, ok, State#framed_transport{write_buffer = [WBuf, Data]}}; - -handle_call({read, Len}, _From, State = #framed_transport{wrapped = Wrapped0, - read_buffer = RBuf}) -> +%% Reads data through from the wrapped transoprt +read(State0 = #framed_transport{wrapped = Wrapped0, read_buffer = RBuf}, + Len) when is_integer(Len) -> {Wrapped1, {RBuf1, RBuf1Size}} = %% if the read buffer is empty, read another frame %% otherwise, just read from what's left in the buffer @@ -141,68 +87,13 @@ handle_call({read, Len}, _From, State = #framed_transport{wrapped = Wrapped0, <> = iolist_to_binary(RBuf1), Response = {ok, Data}, - State1 = State#framed_transport{wrapped = Wrapped1, read_buffer=RBuf2}, - - {reply, Response, State1}; - -handle_call(flush, _From, State) -> - {Response, State1} = do_flush(State), - {reply, Response, State1}. - -%%-------------------------------------------------------------------- -%% Function: handle_cast(Msg, State) -> {noreply, State} | -%% {noreply, State, Timeout} | -%% {stop, Reason, State} -%% Description: Handling cast messages -%%-------------------------------------------------------------------- -handle_cast(close, State) -> - %% Wrapped is closed by terminate/2 - %% error_logger:info_msg("thrift_framed_transport ~p: closing", [self()]), - {stop, normal, State}; -handle_cast(Msg, State=#framed_transport{}) -> - {noreply, State}. - -%%-------------------------------------------------------------------- -%% Function: handle_info(Info, State) -> {noreply, State} | -%% {noreply, State, Timeout} | -%% {stop, Reason, State} -%% Description: Handling all non call/cast messages -%%-------------------------------------------------------------------- -handle_info(_Info, State) -> - {noreply, State}. - -%%-------------------------------------------------------------------- -%% Function: terminate(Reason, State) -> void() -%% Description: This function is called by a gen_server when it is about to -%% terminate. It should be the opposite of Module:init/1 and do any necessary -%% cleaning up. When it returns, the gen_server terminates with Reason. -%% The return value is ignored. -%%-------------------------------------------------------------------- -terminate(_Reason, State = #framed_transport{wrapped=Wrapped}) -> - thrift_transport:close(Wrapped), - ok. + State1 = State0#framed_transport{wrapped = Wrapped1, read_buffer=RBuf2}, -%%-------------------------------------------------------------------- -%% Func: code_change(OldVsn, State, Extra) -> {ok, NewState} -%% Description: Convert process state when code is changed -%%-------------------------------------------------------------------- -code_change(_OldVsn, State, _Extra) -> - {ok, State}. + {State1, Response}. %%-------------------------------------------------------------------- -%%% Internal functions +%% Internal functions %%-------------------------------------------------------------------- -do_flush(State = #framed_transport{write_buffer = Buffer, - wrapped = Wrapped0}) -> - FrameLen = iolist_size(Buffer), - Data = [<>, Buffer], - - {Wrapped1, Response} = thrift_transport:write(Wrapped0, Data), - - {Wrapped2, _} = thrift_transport:flush(Wrapped1), - - State1 = State#framed_transport{wrapped = Wrapped2, write_buffer = []}, - {Response, State1}. min(A,B) when A A; min(_,B) -> B. -- 2.17.1