diff options
author | Micael Karlberg <bmk@erlang.org> | 2023-04-28 09:31:41 +0200 |
---|---|---|
committer | Micael Karlberg <bmk@erlang.org> | 2023-04-28 09:31:41 +0200 |
commit | be7cf92b6f3d539253db7ec64060a885b5e3d167 (patch) | |
tree | f2721ffbac0eadee2d3b6d6ed367c72a9f546fcb /lib/kernel | |
parent | 432aae16404da21d75ae85889e413d9843333d5e (diff) | |
parent | c3c28aed77c4857023b1b8bbb9cb246488f94489 (diff) | |
download | erlang-be7cf92b6f3d539253db7ec64060a885b5e3d167.tar.gz |
Merge branch 'bmk/erts/esock/20230414/win_async_io_continue/OTP-18029'
Diffstat (limited to 'lib/kernel')
-rw-r--r-- | lib/kernel/Makefile | 2 | ||||
-rw-r--r-- | lib/kernel/doc/src/socket.xml | 131 | ||||
-rw-r--r-- | lib/kernel/src/gen_tcp_socket.erl | 16 | ||||
-rw-r--r-- | lib/kernel/src/socket.erl | 10 | ||||
-rw-r--r-- | lib/kernel/test/socket_SUITE.erl | 218 |
5 files changed, 259 insertions, 118 deletions
diff --git a/lib/kernel/Makefile b/lib/kernel/Makefile index 534b564c2c..7586a4c981 100644 --- a/lib/kernel/Makefile +++ b/lib/kernel/Makefile @@ -35,7 +35,7 @@ SPECIAL_TARGETS = # ---------------------------------------------------- include $(ERL_TOP)/make/otp_subdir.mk -DIA_PLT_APPS=crypto +DIA_PLT_APPS=crypto compiler TEST_NEEDS_RELEASE=true include $(ERL_TOP)/make/app_targets.mk diff --git a/lib/kernel/doc/src/socket.xml b/lib/kernel/doc/src/socket.xml index e8e77b8295..2861108da6 100644 --- a/lib/kernel/doc/src/socket.xml +++ b/lib/kernel/doc/src/socket.xml @@ -148,69 +148,8 @@ <c>Info</c> will be <c>{SelectHandle, closed}</c>. </p> </note> - <marker id="asynchronous-call"/> <note> - <p>OLD NOTE</p> - <p>Select-based <i>asynchronous</i> message interface. </p> - <p> - Some functions allow for an <i>asynchronous</i> call. - This is achieved by setting the <c>Timeout</c> argument to - <c>nowait</c>. - For instance, if calling the - <seeerl marker="#recv-nowait"><c>recv/3</c></seeerl> - function with Timeout set to <c>nowait</c> - (<c>recv(Sock, 0, nowait)</c>) - when there is actually nothing to read, it will return with - <c>{select, </c> - <seetype marker="#select_info"><c>SelectInfo</c></seetype><c>}</c> - (<c>SelectInfo</c> contains the - <seetype marker="socket#select_handle">SelectHandle</seetype>). - When data eventually arrives a 'select' message - will be sent to the caller: - </p> - <taglist> - <!-- NOTE THAT THE EMPTY TAG IS INTENTIONAL --> - <tag></tag> - <item><c>{'$socket', socket(), select, SelectHandle}</c></item> - </taglist> - <p> - The caller can now call the <c>recv</c> function again - and probably expect data - (it is really up to the OS network protocol implementation). - </p> - <p> - Note that all other users are <em>locked out</em> until the - 'current user' has called the function (<c>recv</c> in this case) - and its return value shows that the operation has completed. - An operation can also be cancelled with - <seemfa marker="#cancel/2"><c>cancel/2</c></seemfa>. - </p> - <p> - Instead of <c>Timeout = nowait</c> it is equivalent to create a - <seetype marker="socket#select_handle"><c>SelectHandle</c></seetype>) - with - <seemfa marker="erts:erlang#make_ref/0"><c>make_ref()</c></seemfa> - and give as <c>Timeout</c>. - This will then be the <c>SelectHandle</c> in the 'select' message, - which enables a compiler optimization for receiving - a message containing a newly created <c>reference()</c> - (ignore the part of the message queue that had arrived - before the the <c>reference()</c> was created). - </p> - <p>Another message the user must be prepared for (when making asynchronous - calls) is the <c>abort</c> message:</p> - <taglist> - <!-- NOTE THAT THE EMPTY TAG IS INTENTIONAL --> - <tag></tag> - <item><c>{'$socket', socket(), abort, Info}</c></item> - </taglist> - <p>This message indicates - that the (asynchronous) operation has been aborted. - If, for instance, the socket has been closed (by another process), - <c>Info</c> will be <c>{SelectHandle, closed}</c>. </p> - </note> - <note> - <p>There is currently <em>no</em> support for Windows. </p> + <p>The Windows support has currently <em>pre-release</em> status. </p> <p>Support for IPv6 has been implemented but not <em>fully</em> tested. </p> <p>SCTP has only been partly implemented (and not tested). </p> @@ -1652,7 +1591,72 @@ <func> <name name="cancel" arity="2" clause_i="1" since="OTP 22.1"/> - <name name="cancel" arity="2" clause_i="2" since="OTP-18029"/> + <fsummary>Cancel an asynchronous request.</fsummary> + <desc> + <p>Cancel an asynchronous (select) request.</p> + <p> + Call this function in order to cancel a previous + asynchronous call to, e.g. + <seemfa marker="#recv/3"><c>recv/3</c></seemfa>. + </p> + <p> + An ongoing asynchronous operation blocks the socket + until the operation has been finished in good order, + or until it has been cancelled by this function. + </p> + <p> + Any other process that tries an operation + of the same basic type (accept / send / recv) will be + enqueued and notified with the regular <c>select</c> + mechanism for asynchronous operations + when the current operation and all enqueued before it + has been completed. + </p> + <p> + If <c><anno>SelectInfo</anno></c> does not match an + operation in progress for the calling process, + this function returns + <c>{error, {invalid, SelectInfo}}</c>. + </p> + </desc> + </func> + + <func> + <name name="cancel" arity="2" clause_i="2" since="OTP 26.0"/> + <fsummary>Cancel an asynchronous request.</fsummary> + <desc> + <p>Cancel an asynchronous (completion) request.</p> + <p> + Call this function in order to cancel a previous + asynchronous call to, e.g. + <seemfa marker="#recv/3"><c>recv/3</c></seemfa>. + </p> + <p> + An ongoing asynchronous operation blocks the socket + until the operation has been finished in good order, + or until it has been cancelled by this function. + </p> + <p> + Any other process that tries an operation + of the same basic type (accept / send / recv) will be + enqueued and notified with the regular <c>select</c> + mechanism for asynchronous operations + when the current operation and all enqueued before it + has been completed. + </p> + <p> + If <c><anno>CompletionInfo</anno></c> does not match an + operation in progress for the calling process, + this function returns + <c>{error, {invalid, CompletionInfo}}</c>. + </p> + </desc> + </func> + + <!-- + <func> + <name name="cancel" arity="2" clause_i="1" since="OTP 22.1"/> + <name name="cancel" arity="2" clause_i="2" since="OTP 26.0"/> <fsummary>Cancel an asynchronous request.</fsummary> <desc> <p>Cancel an asynchronous request.</p> @@ -1683,6 +1687,7 @@ </p> </desc> </func> + --> <func> <name name="close" arity="1" since="OTP 22.0"/> diff --git a/lib/kernel/src/gen_tcp_socket.erl b/lib/kernel/src/gen_tcp_socket.erl index 84ea036c3f..fb0f807cc1 100644 --- a/lib/kernel/src/gen_tcp_socket.erl +++ b/lib/kernel/src/gen_tcp_socket.erl @@ -729,8 +729,10 @@ socket_close(Socket) -> -compile({inline, [socket_cancel/2]}). socket_cancel(Socket, SelectInfo) -> case socket:cancel(Socket, SelectInfo) of - ok -> ok; - {error, closed} -> ok + ok -> ok; + {error, closed} -> ok; + {error, _} = ERROR -> ERROR + end. %%% ======================================================================== @@ -1575,7 +1577,7 @@ handle_event( info = SelectInfo, from = From, listen_socket = ListenSocket}, {P, D}) -> - socket_cancel(ListenSocket, SelectInfo), + _ = socket_cancel(ListenSocket, SelectInfo), {next_state, 'closed', {P, D}, [{reply, From, {error, timeout}}]}; handle_event(Type, Content, #accept{} = State, P_D) -> @@ -1661,7 +1663,7 @@ handle_event( {timeout, connect}, connect, #connect{info = SelectInfo, from = From}, {#params{socket = Socket} = _P, _D} = P_D) -> - socket_cancel(Socket, SelectInfo), + _ = socket_cancel(Socket, SelectInfo), _ = socket_close(Socket), {next_state, 'closed', P_D, [{reply, From, {error, timeout}}]}; @@ -2268,11 +2270,11 @@ cleanup_close_read(P, D, State, Reason) -> case State of #accept{ info = SelectInfo, from = From, listen_socket = ListenSocket} -> - socket_cancel(ListenSocket, SelectInfo), + _ = socket_cancel(ListenSocket, SelectInfo), {D, [{reply, From, {error, Reason}}]}; #connect{info = SelectInfo, from = From} -> - socket_cancel(P#params.socket, SelectInfo), + _ = socket_cancel(P#params.socket, SelectInfo), {D, [{reply, From, {error, Reason}}]}; _ -> @@ -2283,7 +2285,7 @@ cleanup_recv(P, D, State, Reason) -> %% ?DBG({P#params.socket, State, Reason}), case State of #recv{info = SelectInfo} -> - socket_cancel(P#params.socket, SelectInfo), + _ = socket_cancel(P#params.socket, SelectInfo), cleanup_recv_reply(P, D, [], Reason); _ -> cleanup_recv_reply(P, D, [], Reason) diff --git a/lib/kernel/src/socket.erl b/lib/kernel/src/socket.erl index 25be82543f..02e199d088 100644 --- a/lib/kernel/src/socket.erl +++ b/lib/kernel/src/socket.erl @@ -4548,6 +4548,7 @@ ioctl(Socket, SetRequest, Arg1, Arg2) -> Socket :: socket(), SelectInfo :: select_info(), Reason :: 'closed' | invalid(); + (Socket, CompletionInfo) -> 'ok' | {'error', Reason} when Socket :: socket(), CompletionInfo :: completion_info(), @@ -4590,14 +4591,10 @@ cancel(Socket, Info) -> erlang:error(badarg, [Socket, Info]). -%% What about completion? There is no way to cancel a -%% I/O completion "request" once it has been issued. -%% But we may still have "stuff" in our own queues, -%% which needs to be cleared out. cancel(SockRef, Op, Handle) -> case prim_socket:cancel(SockRef, Op, Handle) of select_sent -> - flush_select_msg(SockRef, Handle), + _ = flush_select_msg(SockRef, Handle), _ = flush_abort_msg(SockRef, Handle), ok; not_found -> @@ -4605,6 +4602,9 @@ cancel(SockRef, Op, Handle) -> _ = flush_abort_msg(SockRef, Handle), invalid; Result -> + %% Since we do not actually if we are using + %% select or completion here, so flush both... + _ = flush_select_msg(SockRef, Handle), _ = flush_completion_msg(SockRef, Handle), _ = flush_abort_msg(SockRef, Handle), Result diff --git a/lib/kernel/test/socket_SUITE.erl b/lib/kernel/test/socket_SUITE.erl index 5f0fd15923..98d5209b18 100644 --- a/lib/kernel/test/socket_SUITE.erl +++ b/lib/kernel/test/socket_SUITE.erl @@ -744,8 +744,11 @@ -define(TPP_LARGE_NUM, 50). -define(TPP_NUM(Config, Base), (Base) div lookup(kernel_factor, 1, Config)). +-define(WINDOWS, {win32,nt}). + -define(TTEST_RUNTIME, ?SECS(1)). -define(TTEST_MIN_FACTOR, 3). +-define(TTEST_MIN_FACTOR_WIN, ?TTEST_MIN_FACTOR-1). -define(TTEST_DEFAULT_SMALL_MAX_OUTSTANDING, 50). -define(TTEST_DEFAULT_MEDIUM_MAX_OUTSTANDING, ?TTEST_MK_DEFAULT_MAX_OUTSTANDING( @@ -1404,7 +1407,12 @@ traffic_pp_sendmsg_recvmsg_cases() -> %% No point in running these cases unless the machine is %% reasonably fast. ttest_condition(Config) -> + OsType = os:type(), case ?config(kernel_factor, Config) of + Factor when (OsType =:= ?WINDOWS) andalso + is_integer(Factor) andalso + (Factor =< ?TTEST_MIN_FACTOR_WIN) -> + ok; Factor when is_integer(Factor) andalso (Factor =< ?TTEST_MIN_FACTOR) -> ok; Factor when is_integer(Factor) -> @@ -3492,7 +3500,10 @@ api_b_send_and_recv_seqpL(_Config) when is_list(_Config) -> api_b_sendmsg_and_recvmsg_tcp4(_Config) when is_list(_Config) -> ?TT(?SECS(10)), tc_try(api_b_sendmsg_and_recvmsg_tcp4, - fun() -> has_support_ipv4() end, + fun() -> + is_not_windows(), + has_support_ipv4() + end, fun() -> Send = fun(Sock, Data) -> Msg = #{iov => [Data]}, @@ -6926,7 +6937,8 @@ api_a_connect_tcp(InitState) -> {ok, State#{asynch_tag => select, connect_tag => ST}}; - {completion, {completion_info, CT, CompletionRef}} + {completion, + {completion_info, CT, CompletionRef}} when SR =:= nowait -> ?SEV_IPRINT("completion nowait ->" "~n tag: ~p" @@ -6935,7 +6947,8 @@ api_a_connect_tcp(InitState) -> {ok, State#{asynch_tag => completion, connect_tag => CT, connect_ref => CompletionRef}}; - {completion, {completion_info, CT, CR}} + {completion, + {completion_info, CT, CR}} when is_reference(CR) -> ?SEV_IPRINT("completion ref ->" "~n tag: ~p" @@ -7988,7 +8001,10 @@ api_a_sendmsg_and_recvmsg_tcp4(Config) when is_list(Config) -> ?TT(?SECS(10)), Nowait = nowait(Config), tc_try(api_a_sendmsg_and_recvmsg_tcp4, - fun() -> has_support_ipv4() end, + fun() -> + is_not_windows(), + has_support_ipv4() + end, fun() -> Send = fun(Sock, Data) -> Msg = #{iov => [Data]}, @@ -9488,6 +9504,7 @@ api_a_recv_cancel_tcp(InitState) -> cmd => fun(#{tester := Tester} = _State) -> ?SEV_AWAIT_CONTINUE(Tester, tester, recv) end}, + #{desc => "try recv request (with nowait, expect select|completion)", cmd => fun(#{csock := Sock, recv := Recv, @@ -9506,13 +9523,15 @@ api_a_recv_cancel_tcp(InitState) -> "~n Ref: ~p", [T, Ref]), {ok, State#{recv_select_info => SI}}; - {completion, {completion_info, T, R} = CI} + {completion, + {completion_info, T, R} = CI} when Ref =:= nowait -> ?SEV_IPRINT("recv completion nowait: " "~n Tag: ~p" "~n Ref: ~p", [T, R]), {ok, State#{recv_completion_info => CI}}; - {completion, {completion_info, T, Ref} = CI} + {completion, + {completion_info, T, Ref} = CI} when is_reference(Ref) -> ?SEV_IPRINT("recv completion ref: " "~n Tag: ~p" @@ -10913,6 +10932,7 @@ api_a_mrecvmsg_cancel_tcp4(Config) when is_list(Config) -> Nowait = nowait(Config), tc_try(?FUNCTION_NAME, fun() -> + is_not_windows(), has_support_ipv4() end, fun() -> @@ -10937,7 +10957,10 @@ api_a_mrecvmsg_cancel_tcp6(Config) when is_list(Config) -> ?TT(?SECS(20)), Nowait = nowait(Config), tc_try(?FUNCTION_NAME, - fun() -> has_support_ipv6() end, + fun() -> + is_not_windows(), + has_support_ipv6() + end, fun() -> Recv = fun(Sock) -> socket:recvmsg(Sock, Nowait) @@ -21126,6 +21149,37 @@ api_opt_ip_recvttl_udp(InitState) -> {skip, ?F("Cannot send with TTL: ~p", [Info])}; + {error, + {completion_status, + #{file := File, + function := Function, + line := Line, + raw_info := RawInfo, + info := invalid_parameter = Info}}} -> + %% IF we can't send it the test will not work + ?SEV_EPRINT("Cannot send TTL: " + "~p => SKIP: " + "~n File: ~s" + "~n Function: ~s" + "~n Line: ~p" + "~n Raw Info: ~p", + [Info, + File, Function, Line, + RawInfo]), + (catch socket:close(SSock)), + (catch socket:close(DSock)), + {skip, + ?F("Cannot send with TTL: ~p", [Info])}; + {error, {completion_status, + invalid_parameter = Info}} -> + %% IF we can't send it the test will not work + ?SEV_EPRINT("Cannot send TTL: " + "~p => SKIP", [Info]), + (catch socket:close(SSock)), + (catch socket:close(DSock)), + {skip, + ?F("Cannot send with TTL: ~p", [Info])}; + {error, _Reason} = ERROR -> ERROR end @@ -23420,7 +23474,7 @@ api_opt_ipv6_tclass_udp(InitState) -> #{desc => "send req (to dst) (w explicit tc = 1)", cmd => fun(#{sock_src := Sock, sa_dst := Dst, send := Send}) -> case Send(Sock, ?BASIC_REQ, Dst, 1) of - {error, + {error, {get_overlapped_result, #{file := File, function := Function, @@ -23449,6 +23503,35 @@ api_opt_ipv6_tclass_udp(InitState) -> {skip, ?F("Cannot send with TClass: ~p", [Info])}; + {error, + {completion_status, + #{file := File, + function := Function, + line := Line, + raw_info := RawInfo, + info := invalid_parameter = Info}}} -> + %% IF we can't send it the test will not work + ?SEV_EPRINT("Cannot send TClass: " + "~p => SKIP: " + "~n File: ~s" + "~n Function: ~s" + "~n Line: ~p" + "~n Raw Info: ~p", + [Info, + File, Function, Line, + RawInfo]), + (catch socket:close(Sock)), + {skip, + ?F("Cannot send with TClass: ~p", [Info])}; + {error, {completion_status, + invalid_parameter = Info}} -> + %% IF we can't send it the test will not work + ?SEV_EPRINT("Cannot send TClass: " + "~p => SKIP", [Info]), + (catch socket:close(Sock)), + {skip, + ?F("Cannot send with TClass: ~p", [Info])}; + Other -> Other end @@ -32985,8 +33068,11 @@ sc_lc_receive_response_udp(InitState) -> sc_lc_recvmsg_response_tcp4(_Config) when is_list(_Config) -> ?TT(?SECS(10)), - tc_try(sc_lc_recvmsg_response_tcp4, - fun() -> has_support_ipv4() end, + tc_try(?FUNCTION_NAME, + fun() -> + is_not_windows(), + has_support_ipv4() + end, fun() -> Recv = fun(Sock) -> socket:recvmsg(Sock) end, InitState = #{domain => inet, @@ -33003,8 +33089,11 @@ sc_lc_recvmsg_response_tcp4(_Config) when is_list(_Config) -> sc_lc_recvmsg_response_tcp6(_Config) when is_list(_Config) -> ?TT(?SECS(10)), - tc_try(sc_recvmsg_response_tcp6, - fun() -> has_support_ipv6() end, + tc_try(?FUNCTION_NAME, + fun() -> + is_not_windows(), + has_support_ipv6() + end, fun() -> Recv = fun(Sock) -> socket:recvmsg(Sock) end, InitState = #{domain => inet6, @@ -36503,7 +36592,10 @@ traffic_send_and_recv_counters_tcpL(_Config) when is_list(_Config) -> traffic_sendmsg_and_recvmsg_counters_tcp4(_Config) when is_list(_Config) -> ?TT(?SECS(15)), tc_try(traffic_sendmsg_and_recvmsg_counters_tcp4, - fun() -> has_support_ipv4() end, + fun() -> + is_not_windows(), + has_support_ipv4() + end, fun() -> InitState = #{domain => inet, proto => tcp, @@ -36532,7 +36624,10 @@ traffic_sendmsg_and_recvmsg_counters_tcp4(_Config) when is_list(_Config) -> traffic_sendmsg_and_recvmsg_counters_tcp6(_Config) when is_list(_Config) -> ?TT(?SECS(15)), tc_try(traffic_sendmsg_and_recvmsg_counters_tcp6, - fun() -> has_support_ipv6() end, + fun() -> + is_not_windows(), + has_support_ipv6() + end, fun() -> InitState = #{domain => inet6, proto => tcp, @@ -48842,7 +48937,7 @@ otp16359_maccept_tcp(InitState) -> %% This test case is to verify that we do not leak monitors. otp18240_accept_mon_leak_tcp4(Config) when is_list(Config) -> - ?TT(?SECS(10)), + ?TT(?SECS(30)), tc_try(?FUNCTION_NAME, fun() -> has_support_ipv4() end, fun() -> @@ -48857,7 +48952,7 @@ otp18240_accept_mon_leak_tcp4(Config) when is_list(Config) -> %% This test case is to verify that we do not leak monitors. otp18240_accept_mon_leak_tcp6(Config) when is_list(Config) -> - ?TT(?SECS(10)), + ?TT(?SECS(30)), tc_try(?FUNCTION_NAME, fun() -> has_support_ipv6() end, fun() -> @@ -48883,26 +48978,42 @@ otp18240_accept_tcp(#{domain := Domain, otp18240_await_acceptor(Pid, Mon) -> receive - {'DOWN', Mon, process, Pid, Info} -> - i("acceptor terminated: " - "~n ~p", [Info]) + {'DOWN', Mon, process, Pid, ok} -> + i("acceptor successfully terminated"), + ok; + {'DOWN', Mon, process, Pid, {skip, _} = SKIP} -> + i("acceptor successfully terminated"), + exit(SKIP); + {'DOWN', Mon, process, Pid, Info} -> + i("acceptor unexpected termination: " + "~n ~p", [Info]), + exit({unexpected_result, Info}) after 5000 -> - i("acceptor info" + i("acceptor process (~p) info" "~n Refs: ~p" "~n Info: ~p", - [monitored_by(Pid), erlang:process_info(Pid)]), + [Pid, monitored_by(Pid), erlang:process_info(Pid)]), otp18240_await_acceptor(Pid, Mon) end. otp18240_acceptor(Parent, Domain, Proto, NumSocks) -> i("[acceptor] begin with: " + "~n Parent: ~p" "~n Domain: ~p" - "~n Protocol: ~p", [Domain, Proto]), + "~n Protocol: ~p", [Parent, Domain, Proto]), MonitoredBy0 = monitored_by(), - {ok, LSock} = socket:open(Domain, stream, Proto, - #{use_registry => false}), - ok = socket:bind(LSock, #{family => Domain, port => 0, addr => any}), + ?SLEEP(?SECS(5)), + Addr = case ?LIB:which_local_host_info(Domain) of + {ok, #{addr := A}} -> + A; + {error, Reason} -> + exit({skip, Reason}) + end, + {ok, LSock} = socket:open(Domain, stream, Proto, + #{use_registry => false}), + ok = socket:bind(LSock, #{family => Domain, addr => Addr, port => 0}), ok = socket:listen(LSock, NumSocks), + ?SLEEP(?SECS(5)), MonitoredBy1 = monitored_by(), i("[acceptor]: listen socket created" "~n 'Montored By' before listen socket: ~p" @@ -48922,7 +49033,7 @@ otp18240_acceptor(Parent, Domain, Proto, NumSocks) -> _Clients = [spawn_link(fun() -> otp18240_client(CID, Domain, Proto, - Port) + Addr, Port) end) || CID <- lists:seq(1, NumSocks)], i("[acceptor] accept ~w connections", [NumSocks]), @@ -48956,26 +49067,49 @@ otp18240_acceptor(Parent, Domain, Proto, NumSocks) -> end. -otp18240_client(ID, Domain, Proto, PortNo) -> +otp18240_client(ID, Domain, Proto, Addr, PortNo) -> i("[connector ~w] try create connector socket", [ID]), {ok, Sock} = socket:open(Domain, stream, Proto, #{use_registry => false}), - ok = socket:bind(Sock, #{family => Domain, port => 0, addr => any}), + ok = socket:bind(Sock, #{family => Domain, addr => Addr, port => 0}), %% ok = socket:setopt(Sock, otp, debug, true), i("[connector ~w] try connect", [ID]), - ok = socket:connect(Sock, #{family => Domain, addr => any, port => PortNo}), - i("[connector ~w] connected - now try recv", [ID]), - case socket:recv(Sock) of - {ok, Data} -> - i("[connector ~w] received unexpected data: " - "~n ~p", [ID, Data]), - (catch socket:close(Sock)), - exit('unexpected data'); - {error, closed} -> - i("[connector ~w] expected socket close", [ID]); - {error, Reason} -> - i("[connector ~w] unexpected error when reading: " - "~n ~p", [ID, Reason]), - (catch socket:close(Sock)) + case socket:connect(Sock, + #{family => Domain, addr => Addr, port => PortNo}) of + ok -> + i("[connector ~w] connected - now try recv", [ID]), + case socket:recv(Sock) of + {ok, Data} -> + i("[connector ~w] received unexpected data: " + "~n ~p", [ID, Data]), + (catch socket:close(Sock)), + exit('unexpected data'); + {error, closed} -> + i("[connector ~w] expected socket close", [ID]); + {error, Reason} -> + i("[connector ~w] unexpected error when reading: " + "~n ~p", [ID, Reason]), + (catch socket:close(Sock)) + end; + {error, {completion_status, #{info := invalid_netname = R} = Reason}} -> + i("[connector ~w] failed connecting: " + "~n ~p", [ID, Reason]), + (catch socket:close(Sock)), + exit({skip, R}); + {error, {completion_status, invalid_netname = Reason}} -> + i("[connector ~w] failed connecting: " + "~n ~p", [ID, Reason]), + (catch socket:close(Sock)), + exit({skip, Reason}); + {error, enetunreach = Reason} -> + i("[connector ~w] failed connecting: " + "~n ~p", [ID, Reason]), + (catch socket:close(Sock)), + exit({skip, Reason}); + + {error, Reason} -> + i("[connector ~w] failed connecting: " + "~n ~p", [ID, Reason]), + (catch socket:close(Sock)) end, i("[connector ~w] done", [ID]), ok. |