77 Try and perform a series of non-blocking connects to the given host:port using the given family and socktype, |
77 Try and perform a series of non-blocking connects to the given host:port using the given family and socktype, |
78 yielding a series of socket objects. |
78 yielding a series of socket objects. |
79 """ |
79 """ |
80 |
80 |
81 for ai in address.getaddrinfo(host, port, family, socktype) : |
81 for ai in address.getaddrinfo(host, port, family, socktype) : |
82 log_info("sock_connect: ai=%s", ai) |
82 log_info("sock_connect: ai: %s", ai) |
83 |
83 |
84 # build socket |
84 # build socket |
85 try : |
85 try : |
86 # construct |
86 # construct |
87 log_debug("sock_connect: socket(%d, %d, %d)", ai.family, ai.socktype, ai.protocol) |
87 log_debug("sock_connect: socket(%d, %d, %d)", ai.family, ai.socktype, ai.protocol) |
111 |
111 |
112 else : |
112 else : |
113 # yay |
113 # yay |
114 yield sock |
114 yield sock |
115 |
115 |
116 def on_connect (fd, events, sock) : |
116 def on_connect (ev, events, sock, sock_iter) : |
117 """ |
117 """ |
118 Outbound connect EV_WRITE callback, i.e. connection failed or was established |
118 Outbound connect EV_WRITE callback, i.e. connection failed or was established |
119 """ |
119 """ |
120 |
120 |
121 log_info("on_connect: %x", events) |
121 log_debug("on_connect: ev=%r, events=%#x", ev, events) |
|
122 |
|
123 # test for timeout |
|
124 if events & EV_TIMEOUT : |
|
125 log_warn("on_connect: connect failed, timeout") |
|
126 |
|
127 # keep trying |
|
128 return client_connect_next(sock_iter) |
|
129 |
|
130 |
|
131 # test for errno |
|
132 err = sock.getsockopt_int(SOL_SOCKET, SO_ERROR) |
|
133 |
|
134 if err : |
|
135 # fail |
|
136 log_warn("on_connect: connect failed, errno=%d", err) |
|
137 |
|
138 # keep trying |
|
139 return client_connect_next(sock_iter) |
|
140 |
|
141 |
|
142 # ok, connected |
|
143 log_info("on_connect: connected") |
|
144 |
122 |
145 |
123 def client_connect_next (sock_iter) : |
146 def client_connect_next (sock_iter) : |
124 """ |
147 """ |
125 Attempt to run the given connect operation, on the given iterable of sockets. |
148 Attempt to run the given connect operation, on the given iterable of sockets. |
126 """ |
149 """ |
127 |
150 |
128 for sock in sock_iter : |
151 for sock in sock_iter : |
129 # pend for writing |
152 # pend for writing |
130 log_debug("client_connect_next: cb_event(%d, EV_WRITE, on_connect, %r)", sock.fd, sock) |
153 log_debug("client_connect_next: cb_event(%d, EV_WRITE, on_connect, %r)", sock.fd, sock) |
131 ev = cb_event(ev_base, sock.fd, EV_WRITE, on_connect, sock) |
154 ev = cb_event(ev_base, sock.fd, EV_WRITE, on_connect, sock, sock_iter) |
132 |
155 |
133 # wait specified timeout |
156 # wait specified timeout |
134 log_debug("client_connect_next: %r: add(%s)", ev, options.timeout) |
157 log_debug("client_connect_next: %r: add(%s)", ev, options.timeout) |
135 ev.add(options.timeout) |
158 ev.add(options.timeout) |
136 |
159 |