Current section

Files

Jump to
stratus src stratus@internal@socket.erl
Raw

src/stratus@internal@socket.erl

-module(stratus@internal@socket).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([convert_options/1, selector/0, get_certs/0, get_custom_matcher/0]).
-export_type([socket/0, socket_reason/0, receive_mode/0, packet_type/0, options/0, shutdown/0, socket_message/0, erlang_socket_message/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(false).
-type socket() :: any().
-type socket_reason() :: closed | not_owner | badarg | {posix, binary()}.
-type receive_mode() :: {count, integer()} | once | pull | all.
-type packet_type() :: binary | list.
-type options() :: {'receive', receive_mode()} |
{packets_of, packet_type()} |
{send_timeout, integer()} |
{send_timeout_close, boolean()} |
{reuseaddr, boolean()} |
{nodelay, boolean()} |
{cacerts, gleam@dynamic:dynamic_()} |
{customize_hostname_check, gleam@dynamic:dynamic_()}.
-type shutdown() :: read | write | read_write.
-type socket_message() :: {data, bitstring()} | {err, socket_reason()}.
-type erlang_socket_message() :: ssl |
ssl_closed |
ssl_error |
tcp |
tcp_closed |
tcp_error.
-file("src/stratus/internal/socket.gleam", 52).
?DOC(false).
-spec convert_options(list(options())) -> list({gleam@erlang@atom:atom_(),
gleam@dynamic:dynamic_()}).
convert_options(Options) ->
Active = erlang:binary_to_atom(<<"active"/utf8>>),
gleam@list:map(Options, fun(Opt) -> case Opt of
{'receive', {count, Count}} ->
{Active, gleam_stdlib:identity(Count)};
{'receive', once} ->
{Active,
gleam_stdlib:identity(
erlang:binary_to_atom(<<"once"/utf8>>)
)};
{'receive', pull} ->
{Active, gleam_stdlib:identity(false)};
{'receive', all} ->
{Active, gleam_stdlib:identity(true)};
{packets_of, binary} ->
{erlang:binary_to_atom(<<"mode"/utf8>>),
gleam_stdlib:identity(binary)};
{packets_of, list} ->
{erlang:binary_to_atom(<<"mode"/utf8>>),
gleam_stdlib:identity(list)};
{cacerts, Data} ->
{erlang:binary_to_atom(<<"cacerts"/utf8>>), Data};
{nodelay, Bool} ->
{erlang:binary_to_atom(<<"nodelay"/utf8>>),
gleam_stdlib:identity(Bool)};
{reuseaddr, Bool@1} ->
{erlang:binary_to_atom(<<"reuseaddr"/utf8>>),
gleam_stdlib:identity(Bool@1)};
{send_timeout, Int} ->
{erlang:binary_to_atom(<<"send_timeout"/utf8>>),
gleam_stdlib:identity(Int)};
{send_timeout_close, Bool@2} ->
{erlang:binary_to_atom(<<"send_timeout_close"/utf8>>),
gleam_stdlib:identity(Bool@2)};
{customize_hostname_check, Funcs} ->
{erlang:binary_to_atom(<<"customize_hostname_check"/utf8>>),
Funcs}
end end).
-file("src/stratus/internal/socket.gleam", 107).
?DOC(false).
-spec selector() -> gleam@erlang@process:selector(socket_message()).
selector() ->
_pipe = gleam_erlang_ffi:new_selector(),
_pipe@2 = gleam@erlang@process:selecting_record3(
_pipe,
tcp,
fun(_, Msg) ->
_assert_subject = begin
_pipe@1 = gleam@dynamic@decode:run(
Msg,
{decoder, fun gleam@dynamic@decode:decode_bit_array/1}
),
gleam@result:map(_pipe@1, fun(Field@0) -> {data, Field@0} end)
end,
{ok, Msg@1} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail,
module => <<"stratus/internal/socket"/utf8>>,
function => <<"selector"/utf8>>,
line => 110})
end,
Msg@1
end
),
_pipe@4 = gleam@erlang@process:selecting_record3(
_pipe@2,
ssl,
fun(_, Msg@2) ->
_assert_subject@1 = begin
_pipe@3 = gleam@dynamic@decode:run(
Msg@2,
{decoder, fun gleam@dynamic@decode:decode_bit_array/1}
),
gleam@result:map(_pipe@3, fun(Field@0) -> {data, Field@0} end)
end,
{ok, Msg@3} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Pattern match failed, no pattern matched the value."/utf8>>,
value => _assert_fail@1,
module => <<"stratus/internal/socket"/utf8>>,
function => <<"selector"/utf8>>,
line => 116})
end,
Msg@3
end
),
_pipe@5 = gleam@erlang@process:selecting_record2(
_pipe@4,
ssl_closed,
fun(_) -> {err, closed} end
),
_pipe@6 = gleam@erlang@process:selecting_record2(
_pipe@5,
tcp_closed,
fun(_) -> {err, closed} end
),
_pipe@7 = gleam@erlang@process:selecting_record3(
_pipe@6,
tcp_error,
fun(_, Reason) -> {err, {posix, gleam@string:inspect(Reason)}} end
),
gleam@erlang@process:selecting_record3(
_pipe@7,
ssl_error,
fun(_, Reason@1) -> {err, {posix, gleam@string:inspect(Reason@1)}} end
).
-file("src/stratus/internal/socket.gleam", 132).
?DOC(false).
-spec get_certs() -> gleam@dynamic:dynamic_().
get_certs() ->
public_key:cacerts_get().
-file("src/stratus/internal/socket.gleam", 135).
?DOC(false).
-spec get_custom_matcher() -> options().
get_custom_matcher() ->
stratus_ffi:custom_sni_matcher().