blob: 0769f56d0056941446830b1598b7ae6e8603a301 [file] [log] [blame]
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -07001/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ANDROID_ASYNC_SOCKET_CONNECTOR_H_
18#define ANDROID_ASYNC_SOCKET_CONNECTOR_H_
19
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070020#include "android/async-io-common.h"
21
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070022/*
23 * Contains declaration of an API that allows asynchronous connection to a
24 * socket with retries.
25 *
26 * The typical usage of this API is as such:
27 *
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070028 * 1. The client creates an asynchronous connector instance by calling
29 * async_socket_connector_new routine, supplying there address of the socket
30 * to connect, and a callback to invoke on connection events.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070031 * 2. The client then proceeds with calling async_socket_connector_connect that
32 * would initiate connection attempts.
33 *
34 * The main job on the client side falls on the client's callback routine that
35 * serves the connection events. Once connection has been initiated, the connector
36 * will invoke that callback to report current connection status.
37 *
38 * In general, there are three connection events passed to the callback:
39 * 1. Success.
40 * 2. Failure.
41 * 3. Retry.
42 *
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070043 * Typically, when client's callback is called for a successful connection, the
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070044 * client will pull connected socket's FD from the connector, and then this FD
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070045 * will be used by the client for I/O on the connected socket.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070046 *
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070047 * When client's callback is invoked with an error (ASIO_STATE_FAILED event), the
48 * client has an opportunity to review the error (available in 'errno'), and
49 * either abort the connection by returning ASIO_ACTION_ABORT, or schedule a retry
50 * by returning ASIO_ACTION_RETRY from the callback. If client returns ASIO_ACTION_ABORT
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070051 * from the callback, the connector will stop connection attempts, and will
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070052 * self-destruct. If ASIO_ACTION_RETRY is returned from the callback, the connector
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070053 * will retry connection attempt after timeout that was set by the caller in the
54 * call to async_socket_connector_new routine.
55 *
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070056 * When client's callback is invoked with ASIO_STATE_RETRYING (indicating that
57 * connector is about to retry a connection attempt), the client has an opportunity
58 * to cancel further connection attempts by returning ASIO_ACTION_ABORT, or it
59 * can allow another connection attempt by returning ASIO_ACTION_RETRY.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070060 *
Vladimir Chtchetkineef4ccd32012-04-03 10:27:12 -070061 * Since it's hard to control lifespan of an object in asynchronous environment,
62 * we make AsyncSocketConnector a referenced object, that will self-destruct when
63 * its reference count drops to zero, indicating that the last client has
64 * abandoned that object.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070065 */
66
67/* Declares async socket connector descriptor. */
68typedef struct AsyncSocketConnector AsyncSocketConnector;
69
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070070/* Declares callback that connector's client uses to monitor connection
71 * status / progress.
72 * Param:
73 * opaque - An opaque pointer associated with the client.
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070074 * connector - AsyncSocketConnector instance.
75 * event - Event that has occurred. If event is set to ASIO_STATE_FAILED,
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070076 * errno contains connection error.
77 * Return:
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070078 * One of AsyncIOAction values.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070079 */
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070080typedef AsyncIOAction (*asc_event_cb)(void* opaque,
81 AsyncSocketConnector* connector,
82 AsyncIOState event);
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070083
84/* Creates and initializes AsyncSocketConnector instance.
Vladimir Chtchetkineef4ccd32012-04-03 10:27:12 -070085 * Note that upon exit from this routine the reference count to the returned
86 * object is set to 1.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070087 * Param:
88 * address - Initialized socket address to connect to.
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070089 * retry_to - Timeout to retry a failed connection attempt in milliseconds.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070090 * cb, cb_opaque - Callback to invoke on connection events. This callback is
91 * required, and must not be NULL.
92 * Return:
93 * Initialized AsyncSocketConnector instance. Note that AsyncSocketConnector
94 * instance returned from this routine will be destroyed by the connector itself,
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -070095 * when its work on connecting to the socket is completed. Typically, connector
96 * will destroy its descriptor after client's callback routine returns with a
97 * status other than ASIO_ACTION_RETRY.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -070098 */
99extern AsyncSocketConnector* async_socket_connector_new(const SockAddress* address,
100 int retry_to,
101 asc_event_cb cb,
102 void* cb_opaque);
103
Vladimir Chtchetkineef4ccd32012-04-03 10:27:12 -0700104/* References AsyncSocketConnector object.
105 * Param:
106 * connector - Initialized AsyncSocketConnector instance.
107 * Return:
108 * Number of outstanding references to the object.
109 */
110extern int async_socket_connector_reference(AsyncSocketConnector* connector);
111
112/* Releases AsyncSocketConnector object.
113 * Note that upon exit from this routine the object might be destroyed, even if
114 * the routine returns value other than zero.
115 * Param:
116 * connector - Initialized AsyncSocketConnector instance.
117 * Return:
118 * Number of outstanding references to the object.
119 */
120extern int async_socket_connector_release(AsyncSocketConnector* connector);
121
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -0700122/* Initiates asynchronous connection.
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -0700123 * Note that connection result will be reported via callback set with the call to
124 * async_socket_connector_new routine.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -0700125 * Param:
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -0700126 * connector - Initialized AsyncSocketConnector instance. Note that this
127 * connector descriptor might be destroyed asynchronously, before this
128 * routine returns.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -0700129 */
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -0700130extern void async_socket_connector_connect(AsyncSocketConnector* connector);
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -0700131
132/* Pulls socket's file descriptor from the connector.
133 * This routine should be called from the connection callback on successful
Vladimir Chtchetkine6dc5c2c2012-04-02 07:48:19 -0700134 * connection status. This will provide the connector's client with an operational
135 * socket FD, and at the same time this will tell the connector not to close the
136 * FD when connector descriptor gets destroyed.
Vladimir Chtchetkine9d36fe72012-03-26 10:29:20 -0700137 * Param:
138 * connector - Initialized AsyncSocketConnector instance.
139 * Return:
140 * File descriptor for the connected socket.
141 */
142extern int async_socket_connector_pull_fd(AsyncSocketConnector* connector);
143
144#endif /* ANDROID_ASYNC_SOCKET_CONNECTOR_H_ */