Home | History | Annotate | Download | only in android

Lines Matching defs:connector

24 #include "android/async-socket-connector.h"
54 /* Asynchronous connector to the socket. */
55 AsyncConnector connector[1];
64 /* Number of outstanding references to the connector. */
66 /* Flags whether (1) or not (0) connector owns the looper. */
70 /* Asynchronous I/O looper callback invoked by the connector.
79 _asc_socket_string(AsyncSocketConnector* connector)
81 return sock_address_to_string(&connector->address);
86 * connector - Initialized AsyncSocketConnector instance.
89 _async_socket_connector_free(AsyncSocketConnector* connector)
91 if (connector != NULL) {
92 T("ASC %s: Connector is destroying...", _asc_socket_string(connector));
95 if (asyncConnector_stop(connector->connector) == 0) {
99 _asc_socket_string(connector));
100 loopIo_done(connector->connector_io);
104 if (connector->looper != NULL) {
105 loopTimer_done(connector->connector_timer);
106 if (connector->owns_looper) {
107 looper_free(connector->looper);
111 if (connector->fd >= 0) {
112 socket_close(connector->fd);
115 T("ASC %s: Connector is destroyed", _asc_socket_string(connector));
117 sock_address_done(&connector->address);
119 AFREE(connector);
125 * connector - Initialized AsyncSocketConnector instance.
130 _async_socket_connector_open_socket(AsyncSocketConnector* connector)
133 connector->fd = socket_create_inet(SOCKET_STREAM);
134 if (connector->fd < 0) {
136 _asc_socket_string(connector), errno, strerror(errno));
140 /* Prepare for async I/O on the connector. */
141 socket_set_nonblock(connector->fd);
143 T("ASC %s: Connector socket is opened with FD = %d",
144 _asc_socket_string(connector), connector->fd);
151 * connector - Initialized AsyncSocketConnector instance.
156 _async_socket_connector_close_socket(AsyncSocketConnector* connector)
158 if (connector->fd >= 0) {
159 socket_close(connector->fd);
160 T("ASC %s: Connector socket FD = %d is closed.",
161 _asc_socket_string(connector), connector->fd);
162 connector->fd = -1;
166 /* Asynchronous connector (AsyncConnector instance) has completed connection
169 * connector - Initialized AsyncSocketConnector instance. Note: When this
170 * callback is called, the caller has referenced passed connector object,
171 * So, it's guaranteed that this connector is not going to be destroyed
176 _on_async_socket_connector_connecting(AsyncSocketConnector* connector,
183 loopIo_done(connector->connector_io);
184 D("Socket '%s' is connected", _asc_socket_string(connector));
186 action = connector->on_connected_cb(connector->on_connected_cb_opaque,
187 connector, ASIO_STATE_SUCCEEDED);
191 loopIo_done(connector->connector_io);
193 _asc_socket_string(connector), errno, strerror(errno));
195 action = connector->on_connected_cb(connector->on_connected_cb_opaque,
196 connector, ASIO_STATE_FAILED);
200 T("ASC %s: Waiting on connection to complete. Connector FD = %d",
201 _asc_socket_string(connector), connector->fd);
206 D("ASC %s: Retrying connection. Connector FD = %d",
207 _asc_socket_string(connector), connector->fd);
208 loopTimer_startRelative(connector->connector_timer, connector->retry_to);
210 D("ASC %s: Client has aborted connection. Connector FD = %d",
211 _asc_socket_string(connector), connector->fd);
218 AsyncSocketConnector* const connector = (AsyncSocketConnector*)opaque;
220 /* Reference the connector while we're handing I/O. */
221 async_socket_connector_reference(connector);
225 connector->on_connected_cb(connector->on_connected_cb_opaque,
226 connector, ASIO_STATE_CONTINUES);
229 const AsyncStatus status = asyncConnector_run(connector->connector);
230 _on_async_socket_connector_connecting(connector, status);
232 D("ASC %s: Client has aborted connection. Connector FD = %d",
233 _asc_socket_string(connector), connector->fd);
236 /* Release the connector after we're done with handing I/O. */
237 async_socket_connector_release(connector);
248 AsyncSocketConnector* const connector = (AsyncSocketConnector*)opaque;
250 T("ASC %s: Reconnect timer expired. Connector FD = %d",
251 _asc_socket_string(connector), connector->fd);
253 /* Reference the connector while we're in callback. */
254 async_socket_connector_reference(connector);
258 connector->on_connected_cb(connector->on_connected_cb_opaque,
259 connector, ASIO_STATE_RETRYING);
263 _async_socket_connector_close_socket(connector);
266 if (_async_socket_connector_open_socket(connector) == 0) {
267 loopIo_init(connector->connector_io, connector->looper,
268 connector->fd, _on_async_socket_connector_io, connector);
269 status = asyncConnector_init(connector->connector,
270 &connector->address,
271 connector->connector_io);
276 _on_async_socket_connector_connecting(connector, status);
278 D("ASC %s: Client has aborted connection. Connector FD = %d",
279 _asc_socket_string(connector), connector->fd);
282 /* Release the connector after we're done with the callback. */
283 async_socket_connector_release(connector);
287 * Async connector implementation
297 AsyncSocketConnector* connector;
306 ANEW0(connector);
308 connector->fd = -1;
309 connector->retry_to = retry_to;
310 connector->on_connected_cb = cb;
311 connector->on_connected_cb_opaque = cb_opaque;
312 connector->ref_count = 1;
316 connector->address = *address;
319 sock_address_init_unix(&connector->address, sock_address_get_path(address));
321 connector->address = *address;
327 connector->looper = looper_newCore();
328 if (connector->looper == NULL) {
330 _asc_socket_string(connector));
331 cb(cb_opaque, connector, ASIO_STATE_FAILED);
332 _async_socket_connector_free(connector);
335 connector->owns_looper = 1;
337 connector->looper = looper;
338 connector->owns_looper = 0;
342 loopTimer_init(connector->connector_timer, connector->looper,
343 _on_async_socket_connector_retry, connector);
345 T("ASC %s: New connector object", _asc_socket_string(connector));
347 return connector;
351 async_socket_connector_reference(AsyncSocketConnector* connector)
353 assert(connector->ref_count > 0);
354 connector->ref_count++;
355 return connector->ref_count;
359 async_socket_connector_release(AsyncSocketConnector* connector)
361 assert(connector->ref_count > 0);
362 connector->ref_count--;
363 if (connector->ref_count == 0) {
365 _async_socket_connector_free(connector);
368 return connector->ref_count;
372 async_socket_connector_connect(AsyncSocketConnector* connector)
376 T("ASC %s: Handling connect request. Connector FD = %d",
377 _asc_socket_string(connector), connector->fd);
379 if (_async_socket_connector_open_socket(connector) == 0) {
381 connector->on_connected_cb(connector->on_connected_cb_opaque,
382 connector, ASIO_STATE_STARTED);
384 D("ASC %s: Client has aborted connection. Connector FD = %d",
385 _asc_socket_string(connector), connector->fd);
388 loopIo_init(connector->connector_io, connector->looper,
389 connector->fd, _on_async_socket_connector_io, connector);
390 status = asyncConnector_init(connector->connector,
391 &connector->address,
392 connector->connector_io);
398 _on_async_socket_connector_connecting(connector, status);
402 async_socket_connector_pull_fd(AsyncSocketConnector* connector)
404 const int fd = connector->fd;
406 connector->fd = -1;
409 T("ASC %s: Client has pulled connector FD %d", _asc_socket_string(connector), fd);