blob: 2414bde36368805e68271b3be5385c00f3f300fc [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
David Reiss3b9c3422008-06-11 00:59:19 +000022-export([behaviour_info/1]).
alisdair sullivana559f8d2014-11-17 20:28:35 -080023%% constructors
24-export([new/1, new/2]).
25%% transport callbacks
26-export([read/2, read_exact/2, write/2, flush/1, close/1]).
David Reissac549552008-06-10 22:56:59 +000027
alisdair sullivana559f8d2014-11-17 20:28:35 -080028-export_type([t_transport/0]).
29
David Reissac549552008-06-10 22:56:59 +000030
31behaviour_info(callbacks) ->
alisdair sullivana559f8d2014-11-17 20:28:35 -080032 [{read, 2}, {write, 2}, {flush, 1}, {close, 1}].
David Reissac549552008-06-10 22:56:59 +000033
alisdair sullivana559f8d2014-11-17 20:28:35 -080034
35-record(t_transport, {
36 module,
37 state
38}).
39
40-type state() :: #t_transport{}.
41-type t_transport() :: #t_transport{}.
42
David Reissac549552008-06-10 22:56:59 +000043
David Reiss9b170eb2010-08-30 22:05:44 +000044-ifdef(transport_wrapper_module).
45-define(debug_wrap(Transport),
alisdair sullivana559f8d2014-11-17 20:28:35 -080046 case Transport#t_transport.module of
47 ?transport_wrapper_module -> Transport;
48 _Else ->
49 {ok, Result} = ?transport_wrapper_module:new(Transport),
50 Result
51 end
52).
David Reiss9b170eb2010-08-30 22:05:44 +000053-else.
54-define(debug_wrap(Transport), Transport).
55-endif.
56
Bryan Duxburyd3879f82010-08-19 05:06:02 +000057
alisdair sullivana559f8d2014-11-17 20:28:35 -080058-type wrappable() ::
59 binary() |
60 list() |
61 {membuffer, binary() | list()} |
62 {tcp, port()} |
63 {tcp, port(), list()} |
64 {file, file:io_device()} |
65 {file, file:io_device(), list()} |
66 {file, file:filename()} |
67 {file, file:filename(), list()}.
David Reissac549552008-06-10 22:56:59 +000068
alisdair sullivana559f8d2014-11-17 20:28:35 -080069-spec new(wrappable()) -> {ok, #t_transport{}}.
David Reiss90b40832008-06-10 22:58:52 +000070
alisdair sullivana559f8d2014-11-17 20:28:35 -080071new({membuffer, Membuffer}) when is_binary(Membuffer); is_list(Membuffer) ->
72 thrift_membuffer_transport:new(Membuffer);
73new({membuffer, Membuffer, []}) when is_binary(Membuffer); is_list(Membuffer) ->
74 thrift_membuffer_transport:new(Membuffer);
75new({tcp, Socket}) when is_port(Socket) ->
76 new({tcp, Socket, []});
77new({tcp, Socket, Opts}) when is_port(Socket) ->
78 thrift_socket_transport:new(Socket, Opts);
79new({file, Filename}) when is_list(Filename); is_binary(Filename) ->
80 new({file, Filename, []});
81new({file, Filename, Opts}) when is_list(Filename); is_binary(Filename) ->
82 {ok, File} = file:open(Filename, [raw, binary]),
83 new({file, File, Opts});
84new({file, File, Opts}) ->
85 thrift_file_transport:new(File, Opts).
David Reissc11734e2008-06-11 00:59:48 +000086
alisdair sullivana559f8d2014-11-17 20:28:35 -080087-spec new(Module::module(), State::any()) -> {ok, #t_transport{}}.
88
89new(Module, State) when is_atom(Module) ->
90 {ok, ?debug_wrap(#t_transport{module = Module, state = State})}.
91
92
93-include("thrift_transport_behaviour.hrl").
94
95
96read(Transport = #t_transport{module = Module}, Len)
97when is_integer(Len), Len >= 0 ->
98 {NewState, Result} = Module:read(Transport#t_transport.state, Len),
99 {Transport#t_transport{state = NewState}, Result}.
100
101
102read_exact(Transport = #t_transport{module = Module}, Len)
103when is_integer(Len), Len >= 0 ->
Nobuaki Sukegawa97a48982015-11-14 18:49:45 +0900104 case lists:keyfind(read_exact, 1, Module:module_info(exports)) of
105 {read_exact, 2} ->
Nobuaki Sukegawa97a48982015-11-14 18:49:45 +0900106 {NewState, Result} = Module:read_exact(Transport#t_transport.state, Len),
107 {Transport#t_transport{state = NewState}, Result};
108 _ ->
Nobuaki Sukegawa97a48982015-11-14 18:49:45 +0900109 read(Transport, Len)
Nobuaki Sukegawa149ecc12015-10-31 12:17:31 +0900110 end.
alisdair sullivana559f8d2014-11-17 20:28:35 -0800111
112
113write(Transport = #t_transport{module = Module}, Data) ->
114 {NewState, Result} = Module:write(Transport#t_transport.state, Data),
115 {Transport#t_transport{state = NewState}, Result}.
116
117
118flush(Transport = #t_transport{module = Module}) ->
119 {NewState, Result} = Module:flush(Transport#t_transport.state),
120 {Transport#t_transport{state = NewState}, Result}.
121
122
123close(Transport = #t_transport{module = Module}) ->
124 {NewState, Result} = Module:close(Transport#t_transport.state),
125 {Transport#t_transport{state = NewState}, Result}.
126