r1319@opsdev009 (orig r70531): cpiro | 2007-11-17 18:10:20 -0800
[amiethrift.git] / lib / erl / src / transport / tTransport.erl
blob73bcf72d7210d07e6de74592825fa0ee4dde048d
1 %%% Copyright (c) 2007- Facebook
2 %%% Distributed under the Thrift Software License
3 %%%
4 %%% See accompanying file LICENSE or visit the Thrift site at:
5 %%% http://developers.facebook.com/thrift/
7 -module(tTransport).
9 -include("oop.hrl").
11 -include("thrift.hrl").
12 -include("transport/tTransport.hrl").
14 -behavior(oop).
16 -export([attr/4, super/0, inspect/1]).
18 -export([new/0, isOpen/1, effectful_open/1, effectful_close/1, read/2, readAll/2, effectful_write/2, effectful_flush/1]).
20 %%%
21 %%% define attributes
22 %%% 'super' is required unless ?MODULE is a base class
23 %%%
25 ?ATTR_DUMMY.
27 %%%
28 %%% behavior callbacks
29 %%%
31 %%% super() -> SuperModule = atom()
32 %%% | none
34 super() ->
35 none.
37 %%% inspect(This) -> string()
39 inspect(_This) ->
40 "".
42 %%%
43 %%% class methods
44 %%%
46 new() ->
47 #?MODULE{}.
49 %%%
50 %%% instance methods
51 %%%
53 e() ->
54 exit('tTransport is abstract').
56 isOpen(_This) ->
57 e(),
58 nil.
60 effectful_open(This) ->
61 e(),
62 {nil, This}.
64 effectful_close(This) ->
65 e(),
66 {nil, This}.
68 read(_This, _Sz) ->
69 e(),
70 nil.
72 readAll(This, Sz) ->
73 readAll_loop(This, Sz, "", 0).
75 readAll_loop(This, Sz, Buff, Have) ->
77 Have < Sz ->
78 Chunk = ?L1(read, Sz - Have),
80 %% man gen_tcp:
81 %% exactly Length bytes are returned, or an error;
82 %% possibly discarding less than Length bytes of data when
83 %% the socket gets closed from the other side.
85 %% error_logger:info_msg("READ |~p|", [Chunk]),
87 Have1 = Have + (Sz-Have), % length(Chunk)
88 Buff1 = Buff ++ Chunk, % TODO: ++ efficiency?
89 readAll_loop(This, Sz, Buff1, Have1);
90 true ->
91 Buff
92 end.
94 effectful_write(This, _Buf) ->
95 e(),
96 {nil, This}.
98 effectful_flush(This) ->
99 {nil, This}.