Software /
code /
prosody
Comparison
net/connect.lua @ 12411:e132a4279914
net.connect: Support for multiple pending connection attempts
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 18 Mar 2022 16:16:01 +0000 |
parent | 12205:a2e6605303fa |
child | 12412:18a3a6218100 |
comparison
equal
deleted
inserted
replaced
12410:596625eed326 | 12411:e132a4279914 |
---|---|
26 | 26 |
27 local pending_connection_listeners = {}; | 27 local pending_connection_listeners = {}; |
28 | 28 |
29 local function attempt_connection(p) | 29 local function attempt_connection(p) |
30 p:log("debug", "Checking for targets..."); | 30 p:log("debug", "Checking for targets..."); |
31 if p.conn then | |
32 pending_connections_map[p.conn] = nil; | |
33 p.conn = nil; | |
34 end | |
35 p.target_resolver:next(function (conn_type, ip, port, extra) | 31 p.target_resolver:next(function (conn_type, ip, port, extra) |
36 if not conn_type then | 32 if not conn_type then |
37 -- No more targets to try | 33 -- No more targets to try |
38 p:log("debug", "No more connection targets to try", p.target_resolver.last_error); | 34 p:log("debug", "No more connection targets to try", p.target_resolver.last_error); |
39 if p.listeners.onfail then | 35 if p.listeners.onfail then |
47 if not conn then | 43 if not conn then |
48 log("debug", "Connection attempt failed immediately: %s", err); | 44 log("debug", "Connection attempt failed immediately: %s", err); |
49 p.last_error = err or "unknown reason"; | 45 p.last_error = err or "unknown reason"; |
50 return attempt_connection(p); | 46 return attempt_connection(p); |
51 end | 47 end |
52 p.conn = conn; | 48 p.conns[conn] = true; |
53 pending_connections_map[conn] = p; | 49 pending_connections_map[conn] = p; |
54 end); | 50 end); |
55 end | 51 end |
56 | 52 |
57 function pending_connection_listeners.onconnect(conn) | 53 function pending_connection_listeners.onconnect(conn) |
60 log("warn", "Successful connection, but unexpected! Closing."); | 56 log("warn", "Successful connection, but unexpected! Closing."); |
61 conn:close(); | 57 conn:close(); |
62 return; | 58 return; |
63 end | 59 end |
64 pending_connections_map[conn] = nil; | 60 pending_connections_map[conn] = nil; |
61 if p.connected then | |
62 -- We already succeeded in connecting | |
63 p.conns[conn] = nil; | |
64 conn:close(); | |
65 return; | |
66 end | |
67 p.connected = true; | |
65 p:log("debug", "Successfully connected"); | 68 p:log("debug", "Successfully connected"); |
66 conn:setlistener(p.listeners, p.data); | 69 conn:setlistener(p.listeners, p.data); |
67 return p.listeners.onconnect(conn); | 70 return p.listeners.onconnect(conn); |
68 end | 71 end |
69 | 72 |
71 local p = pending_connections_map[conn]; | 74 local p = pending_connections_map[conn]; |
72 if not p then | 75 if not p then |
73 log("warn", "Failed connection, but unexpected!"); | 76 log("warn", "Failed connection, but unexpected!"); |
74 return; | 77 return; |
75 end | 78 end |
79 p.conns[conn] = nil; | |
76 p.last_error = reason or "unknown reason"; | 80 p.last_error = reason or "unknown reason"; |
77 p:log("debug", "Connection attempt failed: %s", p.last_error); | 81 p:log("debug", "Connection attempt failed: %s", p.last_error); |
78 attempt_connection(p); | 82 attempt_connection(p); |
79 end | 83 end |
80 | 84 |
83 id = new_id(); | 87 id = new_id(); |
84 target_resolver = target_resolver; | 88 target_resolver = target_resolver; |
85 listeners = assert(listeners); | 89 listeners = assert(listeners); |
86 options = options or {}; | 90 options = options or {}; |
87 data = data; | 91 data = data; |
92 conns = {}; | |
88 }, pending_connection_mt); | 93 }, pending_connection_mt); |
89 | 94 |
90 p:log("debug", "Starting connection process"); | 95 p:log("debug", "Starting connection process"); |
91 attempt_connection(p); | 96 attempt_connection(p); |
92 end | 97 end |