blob: cc9ca1b7db81a9314c1be5835aee4c2bf037015e [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001%%
2%% Licensed to the Apache Software Foundation (ASF) under one
3%% or more contributor license agreements. See the NOTICE file
4%% distributed with this work for additional information
5%% regarding copyright ownership. The ASF licenses this file
6%% to you under the Apache License, Version 2.0 (the
7%% "License"); you may not use this file except in compliance
8%% with the License. You may obtain a copy of the License at
9%%
10%% http://www.apache.org/licenses/LICENSE-2.0
11%%
12%% Unless required by applicable law or agreed to in writing,
13%% software distributed under the License is distributed on an
14%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15%% KIND, either express or implied. See the License for the
16%% specific language governing permissions and limitations
17%% under the License.
18%%
19
David Reissac549552008-06-10 22:56:59 +000020-module(thrift_transport).
21
alisdair sullivana559f8d2014-11-17 20:28:35 -080022%% constructors
23-export([new/1, new/2]).
24%% transport callbacks
25-export([read/2, read_exact/2, write/2, flush/1, close/1]).
David Reissac549552008-06-10 22:56:59 +000026
Sergei Elin45764092022-09-23 23:21:31 +030027-record(t_transport, {
28 module :: module(),
29 state :: term()
30}).
31-type t_transport() :: #t_transport{}.
alisdair sullivana559f8d2014-11-17 20:28:35 -080032-export_type([t_transport/0]).
33
Sergei Elin45764092022-09-23 23:21:31 +030034%%%=========================================================================
35%%% API
36%%%=========================================================================
37-type state() :: term().
38-export_type([state/0]).
39-type reason() :: term().
40-export_type([reason/0]).
David Reissac549552008-06-10 22:56:59 +000041
Sergei Elin45764092022-09-23 23:21:31 +030042-callback write(state(), iolist() | binary()) -> {state(), ok | {error, reason()}}.
43-callback read(state(), non_neg_integer()) -> {state(), {ok, binary()} | {error, reason()}}.
44-callback flush(state()) -> {state(), ok | {error, reason()}}.
45-callback close(state()) -> {state(), ok | {error, reason()}}.
David Reissac549552008-06-10 22:56:59 +000046
David Reiss9b170eb2010-08-30 22:05:44 +000047-ifdef(transport_wrapper_module).
48-define(debug_wrap(Transport),
Sergei Elin45764092022-09-23 23:21:31 +030049 case Transport#t_transport.module of
50 ?transport_wrapper_module ->
51 Transport;
52 _Else ->
53 {ok, Result} = ?transport_wrapper_module:new(Transport),
54 Result
55 end
alisdair sullivana559f8d2014-11-17 20:28:35 -080056).
David Reiss9b170eb2010-08-30 22:05:44 +000057-else.
58-define(debug_wrap(Transport), Transport).
59-endif.
60
alisdair sullivana559f8d2014-11-17 20:28:35 -080061-type wrappable() ::
Sergei Elin45764092022-09-23 23:21:31 +030062 binary()
63 | list()
64 | {membuffer, binary() | list()}
65 | {membuffer, binary() | list(), list()}
66 | {tcp, port()}
67 | {tcp, port(), list()}
68 | {file, file:io_device()}
69 | {file, file:io_device(), list()}
70 | {file, file:filename()}
71 | {file, file:filename(), list()}.
David Reissac549552008-06-10 22:56:59 +000072
alisdair sullivana559f8d2014-11-17 20:28:35 -080073-spec new(wrappable()) -> {ok, #t_transport{}}.
David Reiss90b40832008-06-10 22:58:52 +000074
Sergei Elin45764092022-09-23 23:21:31 +030075new({membuffer, Membuffer}) ->
76 new({membuffer, Membuffer, []});
77new({membuffer, Membuffer, Opts}) when is_binary(Membuffer); is_list(Membuffer) ->
78 thrift_membuffer_transport:new(Membuffer, Opts);
alisdair sullivana559f8d2014-11-17 20:28:35 -080079new({tcp, Socket}) when is_port(Socket) ->
Sergei Elin45764092022-09-23 23:21:31 +030080 new({tcp, Socket, []});
alisdair sullivana559f8d2014-11-17 20:28:35 -080081new({tcp, Socket, Opts}) when is_port(Socket) ->
Sergei Elin45764092022-09-23 23:21:31 +030082 thrift_socket_transport:new(Socket, Opts);
alisdair sullivana559f8d2014-11-17 20:28:35 -080083new({file, Filename}) when is_list(Filename); is_binary(Filename) ->
Sergei Elin45764092022-09-23 23:21:31 +030084 new({file, Filename, []});
alisdair sullivana559f8d2014-11-17 20:28:35 -080085new({file, Filename, Opts}) when is_list(Filename); is_binary(Filename) ->
Sergei Elin45764092022-09-23 23:21:31 +030086 {ok, File} = file:open(Filename, [raw, binary]),
87 new({file, File, Opts});
alisdair sullivana559f8d2014-11-17 20:28:35 -080088new({file, File, Opts}) ->
Sergei Elin45764092022-09-23 23:21:31 +030089 thrift_file_transport:new(File, Opts).
David Reissc11734e2008-06-11 00:59:48 +000090
Sergei Elin45764092022-09-23 23:21:31 +030091-spec new(Module :: module(), State :: any()) -> {ok, t_transport()}.
alisdair sullivana559f8d2014-11-17 20:28:35 -080092
93new(Module, State) when is_atom(Module) ->
Sergei Elin45764092022-09-23 23:21:31 +030094 {ok, ?debug_wrap(#t_transport{module = Module, state = State})}.
alisdair sullivana559f8d2014-11-17 20:28:35 -080095
Sergei Elin45764092022-09-23 23:21:31 +030096read(Transport = #t_transport{module = Module}, Len) when
97 is_integer(Len), Len >= 0
98->
99 {NewState, Result} = Module:read(Transport#t_transport.state, Len),
100 {Transport#t_transport{state = NewState}, Result}.
alisdair sullivana559f8d2014-11-17 20:28:35 -0800101
Sergei Elin45764092022-09-23 23:21:31 +0300102read_exact(Transport = #t_transport{module = Module}, Len) when
103 is_integer(Len), Len >= 0
104->
105 case lists:keyfind(read_exact, 1, Module:module_info(exports)) of
106 {read_exact, 2} ->
107 {NewState, Result} = Module:read_exact(Transport#t_transport.state, Len),
108 {Transport#t_transport{state = NewState}, Result};
109 _ ->
110 read(Transport, Len)
111 end.
alisdair sullivana559f8d2014-11-17 20:28:35 -0800112
113write(Transport = #t_transport{module = Module}, Data) ->
Sergei Elin45764092022-09-23 23:21:31 +0300114 {NewState, Result} = Module:write(Transport#t_transport.state, Data),
115 {Transport#t_transport{state = NewState}, Result}.
alisdair sullivana559f8d2014-11-17 20:28:35 -0800116
117flush(Transport = #t_transport{module = Module}) ->
Sergei Elin45764092022-09-23 23:21:31 +0300118 {NewState, Result} = Module:flush(Transport#t_transport.state),
119 {Transport#t_transport{state = NewState}, Result}.
alisdair sullivana559f8d2014-11-17 20:28:35 -0800120
121close(Transport = #t_transport{module = Module}) ->
Sergei Elin45764092022-09-23 23:21:31 +0300122 {NewState, Result} = Module:close(Transport#t_transport.state),
123 {Transport#t_transport{state = NewState}, Result}.