|
31 | 31 | %%-------------------------------------------------------------------- |
32 | 32 | %% External exports (should only be used by the 'p1_mysql_conn' module) |
33 | 33 | %%-------------------------------------------------------------------- |
34 | | --export([start_link/5 |
| 34 | +-export([start_link/5, |
| 35 | + start_link/6 |
35 | 36 | ]). |
36 | 37 |
|
37 | 38 | -include_lib("kernel/include/inet.hrl"). |
|
56 | 57 | %% Port = integer() |
57 | 58 | %% LogFun = undefined | function() of arity 3 |
58 | 59 | %% Parent = pid(), process that should get received frames |
| 60 | +%% Options = [atom() | {atom{}, any{}}] gen_tcp options |
59 | 61 | %% Descrip.: Start a process that connects to Host:Port and waits for |
60 | 62 | %% data. When it has received a MySQL frame, it sends it to |
61 | 63 | %% Parent and waits for the next frame. |
|
65 | 67 | %% Socket = term(), gen_tcp socket |
66 | 68 | %% Reason = atom() | string() |
67 | 69 | %%-------------------------------------------------------------------- |
68 | | -start_link(Host, Port, ConnectTimeout, LogFun, Parent) when is_list(Host), |
| 70 | +start_link(Host, Port, ConnectTimeout, LogFun, Parent) -> |
| 71 | + start_link(Host, Port, ConnectTimeout, LogFun, Parent, []). |
| 72 | + |
| 73 | +start_link(Host, Port, ConnectTimeout, LogFun, Parent, Options) when is_list(Host), |
69 | 74 | is_integer(Port) -> |
70 | 75 | RecvPid = |
71 | 76 | spawn_link(fun () -> |
72 | | - init(Host, Port, LogFun, Parent) |
| 77 | + init(Host, Port, LogFun, Parent, Options) |
73 | 78 | end), |
74 | 79 | %% wait for the socket from the spawned pid |
75 | 80 | receive |
@@ -97,8 +102,8 @@ start_link(Host, Port, ConnectTimeout, LogFun, Parent) when is_list(Host), |
97 | 102 | %% Descrip.: Connect to Host:Port and then enter receive-loop. |
98 | 103 | %% Returns : error | never returns |
99 | 104 | %%-------------------------------------------------------------------- |
100 | | -init(Host, Port, LogFun, Parent) -> |
101 | | - case connect(Host, Port) of |
| 105 | +init(Host, Port, LogFun, Parent, Options) -> |
| 106 | + case connect(Host, Port, Options) of |
102 | 107 | {ok, Sock} -> |
103 | 108 | Parent ! {p1_mysql_recv, self(), init, {ok, Sock}}, |
104 | 109 | State = #state{socket = Sock, |
@@ -182,22 +187,27 @@ sendpacket(Parent, Data) -> |
182 | 187 | %%-------------------------------------------------------------------- |
183 | 188 | %% Connecting stuff |
184 | 189 | %%-------------------------------------------------------------------- |
185 | | -connect(Host, Port) -> |
| 190 | +connect(Host, Port, Options) -> |
186 | 191 | case lookup(Host) of |
187 | 192 | {ok, AddrsFamilies} -> |
188 | | - do_connect(AddrsFamilies, Port, {error, nxdomain}); |
| 193 | + do_connect(AddrsFamilies, Port, Options, {error, nxdomain}); |
189 | 194 | {error, _} = Err -> |
190 | 195 | Err |
191 | 196 | end. |
192 | 197 |
|
193 | | -do_connect([{IP, Family}|AddrsFamilies], Port, _Err) -> |
194 | | - case gen_tcp:connect(IP, Port, [binary, {packet, 0}, Family]) of |
| 198 | +do_connect([{IP, Family}|AddrsFamilies], Port, Options, _Err) -> |
| 199 | + SupportedOptions = inet:options() -- [binary,packet,inet,inet6], |
| 200 | + OtherOpts = lists:filter(fun(Opt) -> |
| 201 | + OptKey = case Opt of {K, _} -> K; K -> K end, |
| 202 | + lists:member(OptKey, SupportedOptions) |
| 203 | + end, Options), |
| 204 | + case gen_tcp:connect(IP, Port, [binary, {packet, 0}, Family | OtherOpts]) of |
195 | 205 | {ok, Sock} -> |
196 | 206 | {ok, Sock}; |
197 | 207 | {error, _} = Err -> |
198 | | - do_connect(AddrsFamilies, Port, Err) |
| 208 | + do_connect(AddrsFamilies, Port, Options, Err) |
199 | 209 | end; |
200 | | -do_connect([], _Port, Err) -> |
| 210 | +do_connect([], _Port, _Options, Err) -> |
201 | 211 | Err. |
202 | 212 |
|
203 | 213 | lookup(Host) -> |
|
0 commit comments