Skip to content

Commit

Permalink
Introduces tcp transport and get rids of IS_SSL macro
Browse files Browse the repository at this point in the history
  • Loading branch information
tank-bohr committed Aug 1, 2021
1 parent c997012 commit 7e3acde
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/bookish_spork_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,5 @@ detect_transport(Options) ->
true ->
bookish_spork_ssl;
_ ->
gen_tcp
bookish_spork_tcp
end.
10 changes: 9 additions & 1 deletion src/bookish_spork_ssl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
recv/2,
send/2,
close/1,
shutdown/2
shutdown/2,
setopts/2,
connection_information/1
]).

-define(SSL_OPTIONS, [
Expand Down Expand Up @@ -50,3 +52,9 @@ close(Socket) ->

shutdown(Socket, How) ->
ssl:shutdown(Socket, How).

setopts(Socket, Options) ->
ssl:setopts(Socket, Options).

connection_information(Socket) ->
ssl:connection_information(Socket).
38 changes: 38 additions & 0 deletions src/bookish_spork_tcp.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-module(bookish_spork_tcp).

-behaviour(bookish_spork_transport).

-export([
listen/2,
accept/1,
recv/2,
send/2,
close/1,
shutdown/2,
setopts/2,
connection_information/1
]).

listen(Port, Options) ->
gen_tcp:listen(Port, Options).

accept(ListenSocket) ->
gen_tcp:accept(ListenSocket).

recv(Socket, Length) ->
gen_tcp:recv(Socket, Length).

send(Socket, Data) ->
gen_tcp:send(Socket, Data).

close(Socket) ->
gen_tcp:close(Socket).

shutdown(Socket, How) ->
gen_tcp:shutdown(Socket, How).

setopts(Socket, Options) ->
inet:setopts(Socket, Options).

connection_information(_Socket) ->
{ok, []}.
40 changes: 20 additions & 20 deletions src/bookish_spork_transport.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
connection_id/1
]).

-type callback_module() :: gen_tcp | bookish_spork_ssl.
-type callback_module() :: bookish_spork_tcp | bookish_spork_ssl.
-type socket() :: gen_tcp:socket() | ssl:sslsocket().

-record(listen, {
Expand All @@ -26,10 +26,10 @@
}).

-record(transport, {
id :: binary(),
module :: callback_module(),
socket :: socket(),
ssl_ext :: undefined | ssl:protocol_extensions()
id :: binary(),
module :: callback_module(),
socket :: socket(),
ssl_ext = #{} :: ssl:protocol_extensions()
}).

-callback listen(Port, Options) -> Result when
Expand Down Expand Up @@ -58,6 +58,14 @@
Socket :: socket(),
How :: read | write | read_write,
Reason :: inet:posix().
-callback setopts(Socket, Options) -> ok | {error, Reason} when
Socket :: socket(),
Options :: [gen_tcp:socket_setopt()],
Reason :: any().
-callback connection_information(Socket) -> {ok, Result} | {error, Reason} when
Socket :: socket(),
Result :: ssl:connection_info(),
Reason :: any().

-opaque t() :: #transport{}.
-opaque listen() :: #listen{}.
Expand Down Expand Up @@ -97,13 +105,9 @@ recv(#transport{socket = Socket, module = Module}) ->
read_raw(_, 0) ->
<<>>;
read_raw(#transport{socket = Socket, module = Module}, ContentLength) ->
SockModule = case ?IS_SSL_SOCKET(Socket) of
true -> ssl;
false -> inet
end,
SockModule:setopts(Socket, [{packet, raw}]),
Module:setopts(Socket, [{packet, raw}]),
{ok, Body} = Module:recv(Socket, ContentLength),
SockModule:setopts(Socket, [{packet, http}]),
Module:setopts(Socket, [{packet, http}]),
Body.

-spec send(t(), iodata()) -> ok.
Expand All @@ -128,18 +132,14 @@ socket(#transport{socket = Socket}) ->
connection_id(#transport{id = Id}) ->
Id.

-spec ssl_ext(t()) -> undefined | ssl:protocol_extensions().
ssl_ext(#transport{ssl_ext = undefined}) ->
nil;
-spec ssl_ext(t()) -> ssl:protocol_extensions().
ssl_ext(#transport{ssl_ext = Ext}) ->
Ext.

-spec ssl_info(t()) -> undefined | proplists:proplist().
ssl_info(#transport{socket = Socket}) when ?IS_SSL_SOCKET(Socket) ->
{ok, Info} = ssl:connection_information(Socket),
Info;
ssl_info(_) ->
nil.
-spec ssl_info(t()) -> proplists:proplist().
ssl_info(#transport{socket = Socket, module = Module}) ->
{ok, Info} = Module:connection_information(Socket),
Info.

-spec generate_id() -> Id :: binary().
%% @doc generates unique id to be a connection id
Expand Down

0 comments on commit 7e3acde

Please sign in to comment.