THRIFT-599. erl: Don't use unnecessary processes in the Erlang transports and clients

The only user-visible changes are to the client. Every thrift call now returns {NewClient, Result} instead of just Result.

Patch: David Reiss (assist to Anthony Molinaro)

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@987018 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/erl/src/thrift_transport.erl b/lib/erl/src/thrift_transport.erl
index 20c4b5d..39f8c05 100644
--- a/lib/erl/src/thrift_transport.erl
+++ b/lib/erl/src/thrift_transport.erl
@@ -37,21 +37,42 @@
 
 -record(transport, {module, data}).
 
-new(Module, Data) when is_atom(Module) ->
-    {ok, #transport{module = Module,
-                    data = Data}}.
+-ifdef(transport_wrapper_module).
+-define(debug_wrap(Transport),
+        case Transport#transport.module of
+            ?transport_wrapper_module ->
+                Transport;
+            _Else ->
+                {ok, Result} = ?transport_wrapper_module:new(Transport),
+                Result
+        end).
+-else.
+-define(debug_wrap(Transport), Transport).
+-endif.
 
-%% Data :: iolist()
+new(Module, Data) when is_atom(Module) ->
+    Transport0 = #transport{module = Module, data = Data},
+    Transport1 = ?debug_wrap(Transport0),
+    {ok, Transport1}.
+
+-spec write(#transport{}, iolist() | binary()) -> {#transport{}, ok | {error, _Reason}}.
 write(Transport, Data) ->
     Module = Transport#transport.module,
-    Module:write(Transport#transport.data, Data).
+    {NewTransData, Result} = Module:write(Transport#transport.data, Data),
+    {Transport#transport{data = NewTransData}, Result}.
 
+-spec read(#transport{}, non_neg_integer()) -> {#transport{}, {ok, binary()} | {error, _Reason}}.
 read(Transport, Len) when is_integer(Len) ->
     Module = Transport#transport.module,
-    Module:read(Transport#transport.data, Len).
+    {NewTransData, Result} = Module:read(Transport#transport.data, Len),
+    {Transport#transport{data = NewTransData}, Result}.
 
-flush(#transport{module = Module, data = Data}) ->
-    Module:flush(Data).
+-spec flush(#transport{}) -> {#transport{}, ok | {error, _Reason}}.
+flush(Transport = #transport{module = Module, data = Data}) ->
+    {NewTransData, Result} = Module:flush(Data),
+    {Transport#transport{data = NewTransData}, Result}.
 
-close(#transport{module = Module, data = Data}) ->
-    Module:close(Data).
+-spec close(#transport{}) -> {#transport{}, ok | {error, _Reason}}.
+close(Transport = #transport{module = Module, data = Data}) ->
+    {NewTransData, Result} = Module:close(Data),
+    {Transport#transport{data = NewTransData}, Result}.