Skip to content

Commit aa45101

Browse files
author
james
committed
add start_link variants
1 parent 7e57b57 commit aa45101

1 file changed

Lines changed: 49 additions & 33 deletions

File tree

‎src/gen_smtp_server.erl‎

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,53 @@
3030

3131
%% External API
3232
-export([
33-
start/3, start/2, start/1,
34-
stop/1,
35-
child_spec/3,
36-
sessions/1
37-
]).
33+
start_link/1, start_link/2, start_link/3,
34+
start/3, start/2, start/1,
35+
stop/1,
36+
child_spec/3,
37+
sessions/1
38+
]).
3839
-export_type([options/0]).
3940

4041
-type server_name() :: any().
4142
-type options() ::
42-
[
43-
{domain, string()}
43+
[
44+
{domain, string()}
4445
| {address, inet:ip4_address()}
4546
| {family, inet | inet6}
4647
| {port, inet:port_number()}
4748
| {protocol, 'tcp' | 'ssl'}
4849
| {ranch_opts, ranch:opts()}
4950
| {sessionoptions, gen_smtp_server_session:options()}
50-
].
51+
].
52+
53+
%% @doc Start the listener as a registered process with callback module `Module' on with options `Options' linked to the calling process.
54+
-spec start_link(ServerName :: {'local', atom()} | {'global', any()}, Module :: atom(), Options :: [options()]) -> {'ok', pid()} | 'ignore' | {'error', any()}.
55+
start_link(ServerName, Module, Options) when is_list(Options) ->
56+
gen_server:start_link(ServerName, ?MODULE, [Module, Options], []).
57+
58+
%% @doc Start the listener with callback module `Module' on with options `Options' linked to the calling process.
59+
-spec start_link(Module :: atom(), Options :: [options()]) -> {'ok', pid()} | 'ignore' | {'error', any()}.
60+
start_link(Module, Options) when is_list(Options) ->
61+
gen_server:start_link(?MODULE, [Module, Options], []).
62+
63+
%% @doc Start the listener with callback module `Module' with default options linked to the calling process.
64+
-spec start_link(Module :: atom()) -> {'ok', pid()} | 'ignore' | {'error', any()}.
65+
start_link(Module) ->
66+
start_link(Module, [[]]).
5167

5268
%% @doc Start the listener as a registered process with callback module `Module' with options `Options' linked to no process.
5369
-spec start(
54-
ServerName :: server_name(),
55-
CallbackModule :: module(),
56-
Options :: options()
57-
) -> {'ok', pid()} | {'error', any()}.
70+
ServerName :: server_name(),
71+
CallbackModule :: module(),
72+
Options :: options()
73+
) -> {'ok', pid()} | {'error', any()}.
5874
start(ServerName, CallbackModule, Options) when is_list(Options) ->
5975
case convert_options(CallbackModule, Options) of
6076
{ok, Transport, TransportOpts, ProtocolOpts} ->
6177
ranch:start_listener(
62-
ServerName, Transport, TransportOpts, gen_smtp_server_session, ProtocolOpts
63-
);
78+
ServerName, Transport, TransportOpts, gen_smtp_server_session, ProtocolOpts
79+
);
6480
{error, Reason} ->
6581
{error, Reason}
6682
end.
@@ -69,12 +85,12 @@ child_spec(ServerName, CallbackModule, Options) ->
6985
case convert_options(CallbackModule, Options) of
7086
{ok, Transport, TransportOpts, ProtocolOpts} ->
7187
ranch:child_spec(
72-
ServerName, Transport, TransportOpts, gen_smtp_server_session, ProtocolOpts
73-
);
88+
ServerName, Transport, TransportOpts, gen_smtp_server_session, ProtocolOpts
89+
);
7490
{error, Reason} ->
75-
% `supervisor:child_spec' is not compatible with ok/error tuples.
76-
% This error is likely to occur when starting the application,
77-
% so the user can sort out the configuration parameters and try again.
91+
% `supervisor:child_spec' is not compatible with ok/error tuples.
92+
% This error is likely to occur when starting the application,
93+
% so the user can sort out the configuration parameters and try again.
7894
erlang:error(Reason)
7995
end.
8096

@@ -93,31 +109,31 @@ convert_options(CallbackModule, Options) ->
93109
case {EmailTransferProtocol, Port} of
94110
{lmtp, 25} ->
95111
?LOG_ERROR("LMTP is different from SMTP, it MUST NOT be used on the TCP port 25", #{
96-
domain => [gen_smtp, server]
97-
}),
98-
% Error defined in section 5 of https://tools.ietf.org/html/rfc2033
112+
domain => [gen_smtp, server]
113+
}),
114+
% Error defined in section 5 of https://tools.ietf.org/html/rfc2033
99115
{error, invalid_lmtp_port};
100116
_ ->
101117
ProtocolOpts1 = {CallbackModule, [{hostname, Hostname} | ProtocolOpts]},
102118
RanchOpts = proplists:get_value(ranch_opts, Options, #{}),
103119
SocketOpts = maps:get(socket_opts, RanchOpts, []),
104120
TransportOpts = RanchOpts#{
105-
socket_opts =>
106-
[
107-
{port, Port},
108-
{ip, Address},
109-
{keepalive, true},
110-
%% binary, {active, false}, {reuseaddr, true} - ranch defaults
111-
Family
112-
| SocketOpts
113-
]
114-
},
121+
socket_opts =>
122+
[
123+
{port, Port},
124+
{ip, Address},
125+
{keepalive, true},
126+
%% binary, {active, false}, {reuseaddr, true} - ranch defaults
127+
Family
128+
| SocketOpts
129+
]
130+
},
115131
{ok, Transport, TransportOpts, ProtocolOpts1}
116132
end.
117133

118134
%% @doc Start the listener with callback module `Module' with options `Options' linked to no process.
119135
-spec start(CallbackModule :: module(), Options :: options()) ->
120-
{'ok', pid()} | 'ignore' | {'error', any()}.
136+
{'ok', pid()} | 'ignore' | {'error', any()}.
121137
start(CallbackModule, Options) when is_list(Options) ->
122138
start(?MODULE, CallbackModule, Options).
123139

0 commit comments

Comments
 (0)