| Vladimir Chtchetkine | 9d36fe7 | 2012-03-26 10:29:20 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 20 | /* |
| 21 | * Contains declaration of an API that allows asynchronous connection to a |
| 22 | * socket with retries. |
| 23 | * |
| 24 | * The typical usage of this API is as such: |
| 25 | * |
| 26 | * 1. The client creates an async connector instance by calling async_socket_connector_new |
| 27 | * routine, supplying there address of the socket to connect, and a callback |
| 28 | * to invoke on connection events. |
| 29 | * 2. The client then proceeds with calling async_socket_connector_connect that |
| 30 | * would initiate connection attempts. |
| 31 | * |
| 32 | * The main job on the client side falls on the client's callback routine that |
| 33 | * serves the connection events. Once connection has been initiated, the connector |
| 34 | * will invoke that callback to report current connection status. |
| 35 | * |
| 36 | * In general, there are three connection events passed to the callback: |
| 37 | * 1. Success. |
| 38 | * 2. Failure. |
| 39 | * 3. Retry. |
| 40 | * |
| 41 | * Typically, when client's callback is called for successful connection, the |
| 42 | * client will pull connected socket's FD from the connector, and then this FD |
| 43 | * will be used by the client for I/O on the connected socket. If socket's FD |
| 44 | * is pulled by the client, it must return ASC_CB_KEEP from the callback. |
| 45 | * |
| 46 | * When client's callback is invoked with an error (ASC_CONNECTION_FAILED event), |
| 47 | * the client has an opportunity to review the error (available in 'errno'), and |
| 48 | * either abort the connection by returning ASC_CB_ABORT, or schedule a retry |
| 49 | * by returning ASC_CB_RETRY from the callback. If client returns ASC_CB_ABORT |
| 50 | * from the callback, the connector will stop connection attempts, and will |
| 51 | * self-destruct. If ASC_CB_RETRY is returned from the callback, the connector |
| 52 | * will retry connection attempt after timeout that was set by the caller in the |
| 53 | * call to async_socket_connector_new routine. |
| 54 | * |
| 55 | * When client's callback is invoked with ASC_CONNECTION_RETRY, the client has an |
| 56 | * opportunity to cancel further connection attempts by returning ASC_CB_ABORT, |
| 57 | * or it can allow another connection attempt by returning ASC_CB_RETRY. |
| 58 | * |
| 59 | * The client has no control over the lifespan of initialized connector instance. |
| 60 | * It always self-destructs after client's cllback returns with a status other |
| 61 | * than ASC_CB_RETRY. |
| 62 | */ |
| 63 | |
| 64 | /* Declares async socket connector descriptor. */ |
| 65 | typedef struct AsyncSocketConnector AsyncSocketConnector; |
| 66 | |
| 67 | /* Enumerates connection events. |
| 68 | * Values from this enum are passed to the callback that connector's client uses |
| 69 | * to monitor connection status / progress. |
| 70 | */ |
| 71 | typedef enum ASCEvent { |
| 72 | /* Connection with the socket has been successfuly established. */ |
| 73 | ASC_CONNECTION_SUCCEEDED, |
| 74 | /* A failure has occured while establising connection, with errno containing |
| 75 | * the actual error. */ |
| 76 | ASC_CONNECTION_FAILED, |
| 77 | /* Async socket connector is about to retry the connection. */ |
| 78 | ASC_CONNECTION_RETRY, |
| 79 | } ASCEvent; |
| 80 | |
| 81 | /* Enumerates return values from the callback to the connector's client. |
| 82 | */ |
| 83 | typedef enum ASCCbRes { |
| 84 | /* Keep established connection. */ |
| 85 | ASC_CB_KEEP, |
| 86 | /* Abort connection attempts. */ |
| 87 | ASC_CB_ABORT, |
| 88 | /* Retry connection attempt. */ |
| 89 | ASC_CB_RETRY, |
| 90 | } ASCCbRes; |
| 91 | |
| 92 | /* Enumerates values returned from the connector routine. |
| 93 | */ |
| 94 | typedef enum ASCConnectRes { |
| 95 | /* Connection has succeeded in the connector routine. */ |
| 96 | ASC_CONNECT_SUCCEEDED, |
| 97 | /* Connection has failed in the connector routine. */ |
| 98 | ASC_CONNECT_FAILED, |
| 99 | /* Connection is in progress, and will be completed asynchronously. */ |
| 100 | ASC_CONNECT_IN_PROGRESS, |
| 101 | } ASCConnectRes; |
| 102 | |
| 103 | /* Declares callback that connector's client uses to monitor connection |
| 104 | * status / progress. |
| 105 | * Param: |
| 106 | * opaque - An opaque pointer associated with the client. |
| 107 | * connector - Connector instance for thecallback. |
| 108 | * event - Event that has occured. If event is set to ASC_CONNECTION_FAILED, |
| 109 | * errno contains connection error. |
| 110 | * Return: |
| 111 | * One of ASCCbRes values. |
| 112 | */ |
| 113 | typedef ASCCbRes (*asc_event_cb)(void* opaque, |
| 114 | AsyncSocketConnector* connector, |
| 115 | ASCEvent event); |
| 116 | |
| 117 | /* Creates and initializes AsyncSocketConnector instance. |
| 118 | * Param: |
| 119 | * address - Initialized socket address to connect to. |
| 120 | * retry_to - Retry timeout in milliseconds. |
| 121 | * cb, cb_opaque - Callback to invoke on connection events. This callback is |
| 122 | * required, and must not be NULL. |
| 123 | * Return: |
| 124 | * Initialized AsyncSocketConnector instance. Note that AsyncSocketConnector |
| 125 | * instance returned from this routine will be destroyed by the connector itself, |
| 126 | * when its work on connecting to the socket is completed. Typically, the |
| 127 | * connector wil destroy the descriptor after client's callback routine returns |
| 128 | * with the status other than ASC_CB_RETRY. |
| 129 | */ |
| 130 | extern AsyncSocketConnector* async_socket_connector_new(const SockAddress* address, |
| 131 | int retry_to, |
| 132 | asc_event_cb cb, |
| 133 | void* cb_opaque); |
| 134 | |
| 135 | /* Initiates asynchronous connection. |
| 136 | * Param: |
| 137 | * connector - Initialized AsyncSocketConnector instance. |
| 138 | * Return: |
| 139 | * Status indicating state of the connection: completed, failed, or in progress. |
| 140 | * Note that the connector will always invoke a callback passed to the |
| 141 | * async_socket_connector_new routine prior to exiting from this routine with |
| 142 | * statuses other ASC_CONNECT_IN_PROGRESS. |
| 143 | */ |
| 144 | extern ASCConnectRes async_socket_connector_connect(AsyncSocketConnector* connector); |
| 145 | |
| 146 | /* Pulls socket's file descriptor from the connector. |
| 147 | * This routine should be called from the connection callback on successful |
| 148 | * connection status. This will provide the connector's client with operational |
| 149 | * socket FD, and at the same time this will tell the connector not to close |
| 150 | * the FD when connector descriptor gets destroyed. |
| 151 | * Param: |
| 152 | * connector - Initialized AsyncSocketConnector instance. |
| 153 | * Return: |
| 154 | * File descriptor for the connected socket. |
| 155 | */ |
| 156 | extern int async_socket_connector_pull_fd(AsyncSocketConnector* connector); |
| 157 | |
| 158 | #endif /* ANDROID_ASYNC_SOCKET_CONNECTOR_H_ */ |