async-socket.h revision 7136b053b7fc7840ec64e01d1d19ab822e1f949a
1a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* 2a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Copyright (C) 2012 The Android Open Source Project 3a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * 4a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Licensed under the Apache License, Version 2.0 (the "License"); 5a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * you may not use this file except in compliance with the License. 6a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * You may obtain a copy of the License at 7a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * 8a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * http://www.apache.org/licenses/LICENSE-2.0 9a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * 10a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Unless required by applicable law or agreed to in writing, software 11a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * distributed under the License is distributed on an "AS IS" BASIS, 12a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * See the License for the specific language governing permissions and 14a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * limitations under the License. 15a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 16a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 17a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine#ifndef ANDROID_ASYNC_SOCKET_H_ 18a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine#define ANDROID_ASYNC_SOCKET_H_ 19a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 207136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkine#include "qemu-common.h" 216dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine#include "android/async-io-common.h" 227136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkine#include "android/async-utils.h" 236dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 24a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* 256dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * Contains declaration of an API that encapsulates communication via an 26a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * asynchronous socket. 27a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * 286dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * This is pretty basic API that allows asynchronous connection to a socket, 296dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * and asynchronous read from / write to the connected socket. 30a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * 31a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Since all the operations (including connection) are asynchronous, all the 32a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * operation results are reported back to the client of this API via set of 33a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * callbacks that client supplied to this API. 34ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * 35ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Since it's hard to control lifespan of an object in asynchronous environment, 36ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * we make AsyncSocketConnector a referenced object, that will self-destruct when 37ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * its reference count drops to zero, indicating that the last client has 38ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * abandoned that object. 39a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 40a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 41a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Declares asynchronous socket descriptor. */ 42a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkinetypedef struct AsyncSocket AsyncSocket; 43a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 446dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Asynchronous socket I/O (reader, or writer) descriptor. */ 456dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkinetypedef struct AsyncSocketIO AsyncSocketIO; 46a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 47a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Defines client's callback set to monitor socket connection. 48a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 49a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * client_opaque - An opaque pointer associated with the client. 50a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized AsyncSocket instance. 51a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * status - Socket connection status. 52a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Return: 536dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * One of the AsyncIOAction values. 54a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 556dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkinetypedef AsyncIOAction (*on_as_connection_cb)(void* client_opaque, 566dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine AsyncSocket* as, 576dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine AsyncIOState status); 58a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 596dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Defines client's callback set to monitor I/O progress. 60a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 616dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * io_opaque - An opaque pointer associated with the I/O. 626dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * asio - Async I/O in progress. 636dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * status - Status of the I/O. 646dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * Return: 656dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * One of the AsyncIOAction values. 66a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 676dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkinetypedef AsyncIOAction (*on_as_io_cb)(void* io_opaque, 686dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine AsyncSocketIO* asio, 696dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine AsyncIOState status); 70a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 716dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/******************************************************************************** 726dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * AsyncSocketIO API 736dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine *******************************************************************************/ 74a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 75ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine/* References AsyncSocketIO object. 76ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Param: 77ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * asio - Initialized AsyncSocketIO instance. 78ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Return: 79ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Number of outstanding references to the object. 80ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine */ 81ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkineextern int async_socket_io_reference(AsyncSocketIO* asio); 82ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine 83ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine/* Releases AsyncSocketIO object. 84ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Note that upon exit from this routine the object might be destroyed, even if 85ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * the routine returns value other than zero. 86ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Param: 87ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * asio - Initialized AsyncSocketIO instance. 88ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Return: 89ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Number of outstanding references to the object. 90ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine */ 91ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkineextern int async_socket_io_release(AsyncSocketIO* asio); 92ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine 93ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine/* Gets AsyncSocket instance for an I/O. Note that this routine will reference 94ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * AsyncSocket instance before returning it to the caller. */ 956dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern AsyncSocket* async_socket_io_get_socket(const AsyncSocketIO* asio); 966dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 976dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Cancels time out set for an I/O */ 986dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void async_socket_io_cancel_time_out(AsyncSocketIO* asio); 996dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 1006dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets an opaque pointer associated with an I/O */ 1016dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void* async_socket_io_get_io_opaque(const AsyncSocketIO* asio); 102a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 1036dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets an opaque pointer associated with the client that has requested I/O */ 1046dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void* async_socket_io_get_client_opaque(const AsyncSocketIO* asio); 1056dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 1066dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets I/O buffer information. 107a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 1086dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * asio - I/O descriptor. 1096dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * transferred - Optional pointer to receive number of bytes transferred with 1106dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * this I/O. Can be NULL. 1116dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * to_transfer - Optional pointer to receive number of bytes requested to 1126dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * transfer with this I/O. Can be NULL. 1136dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * Return: 1146dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * I/O buffer. 115a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 1166dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void* async_socket_io_get_buffer_info(const AsyncSocketIO* asio, 1176dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine uint32_t* transferred, 1186dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine uint32_t* to_transfer); 119a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 1206dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets I/O buffer. */ 1216dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void* async_socket_io_get_buffer(const AsyncSocketIO* asio); 122a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 1236dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets number of bytes transferred with this I/O. */ 1246dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern uint32_t async_socket_io_get_transferred(const AsyncSocketIO* asio); 125a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 1266dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets number of bytes requested to transfer with this I/O. */ 1276dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern uint32_t async_socket_io_get_to_transfer(const AsyncSocketIO* asio); 1286dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 1296dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets I/O type: read (returns 1), or write (returns 0). */ 1306dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern int async_socket_io_is_read(const AsyncSocketIO* asio); 1316dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 1326dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/******************************************************************************** 1336dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * AsyncSocket API 1346dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine *******************************************************************************/ 135a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 136a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Creates an asynchronous socket descriptor. 137a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 138a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * port - TCP port to connect the socket to. 1396dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * reconnect_to - Timeout before trying to reconnect after disconnection. 1406dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * connect_cb - Client callback to monitor connection state (must not be NULL). 141a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * client_opaque - An opaque pointer to associate with the socket client. 142c8aa2c570d30098da59f1967d5158024ed28570dVladimir Chtchetkine * looper - An optional (can be NULL) I/O looper to use for socket I/O. If 143c8aa2c570d30098da59f1967d5158024ed28570dVladimir Chtchetkine * this parameter is NULL, the socket will create its own looper. 144a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Return: 145a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Initialized AsyncSocket instance on success, or NULL on failure. 146a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 147a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkineextern AsyncSocket* async_socket_new(int port, 148a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine int reconnect_to, 1496dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine on_as_connection_cb connect_cb, 150c8aa2c570d30098da59f1967d5158024ed28570dVladimir Chtchetkine void* client_opaque, 151c8aa2c570d30098da59f1967d5158024ed28570dVladimir Chtchetkine Looper* looper); 152a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 153ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine/* References AsyncSocket object. 154ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Param: 155ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * as - Initialized AsyncSocket instance. 156ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Return: 157ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Number of outstanding references to the object. 158ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine */ 159ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkineextern int async_socket_reference(AsyncSocket* as); 160ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine 161ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine/* Releases AsyncSocket object. 162ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Note that upon exit from this routine the object might be destroyed, even if 163ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * the routine returns value other than zero. 164ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Param: 165ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * as - Initialized AsyncSocket instance. 166ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Return: 167ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine * Number of outstanding references to the object. 168ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine */ 169ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkineextern int async_socket_release(AsyncSocket* as); 170ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine 171a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Asynchronously connects to an asynchronous socket. 1726dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * Note that connection result will be reported via callback passed to the 1736dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * async_socket_new routine. 174a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 175a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized AsyncSocket instance. 176a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * retry_to - Number of milliseconds to wait before retrying a failed 1776dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * connection attempt. 178a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 1796dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void async_socket_connect(AsyncSocket* as, int retry_to); 180a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 181a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Disconnects from an asynchronous socket. 1826dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * NOTE: AsyncSocket instance referenced in this call will be destroyed in this 1836dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * routine. 184a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 185a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized and connected AsyncSocket instance. 186a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 187a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkineextern void async_socket_disconnect(AsyncSocket* as); 188a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 189a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Asynchronously reconnects to an asynchronous socket. 1906dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * Note that connection result will be reported via callback passed to the 1916dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * async_socket_new routine. 192a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 193a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized AsyncSocket instance. 1946dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * retry_to - Number of milliseconds to wait before trying to reconnect. 195a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 1966dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void async_socket_reconnect(AsyncSocket* as, int retry_to); 197a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 198a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Asynchronously reads data from an asynchronous socket with a deadline. 199a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 200a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized and connected AsyncSocket instance. 201a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * buffer, len - Buffer where to read data. 2026dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * reader_cb - Callback to monitor I/O progress (must not be NULL). 203a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * reader_opaque - An opaque pointer associated with the reader. 204a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * deadline - Deadline to complete the read. 205a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 2066dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void async_socket_read_abs(AsyncSocket* as, 2076dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine void* buffer, uint32_t len, 2086dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine on_as_io_cb reader_cb, 2096dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine void* reader_opaque, 2106dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine Duration deadline); 211a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 212a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Asynchronously reads data from an asynchronous socket with a relative timeout. 213a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 214a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized and connected AsyncSocket instance. 215a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * buffer, len - Buffer where to read data. 2166dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * reader_cb - Callback to monitor I/O progress (must not be NULL). 217a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * reader_opaque - An opaque pointer associated with the reader. 218a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * to - Milliseconds to complete the read. to < 0 indicates "no timeout" 219a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 2206dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void async_socket_read_rel(AsyncSocket* as, 2216dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine void* buffer, uint32_t len, 2226dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine on_as_io_cb reader_cb, 2236dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine void* reader_opaque, 2246dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine int to); 225a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 226a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Asynchronously writes data to an asynchronous socket with a deadline. 227a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 228a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized and connected AsyncSocket instance. 229a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * buffer, len - Buffer with writing data. 2306dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * writer_cb - Callback to monitor I/O progress (must not be NULL). 231a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * writer_opaque - An opaque pointer associated with the writer. 232a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * deadline - Deadline to complete the write. 233a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 2346dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void async_socket_write_abs(AsyncSocket* as, 2356dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine const void* buffer, uint32_t len, 2366dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine on_as_io_cb writer_cb, 2376dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine void* writer_opaque, 2386dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine Duration deadline); 239a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 240a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine/* Asynchronously writes data to an asynchronous socket with a relative timeout. 241a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Param: 242a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * as - Initialized and connected AsyncSocket instance. 243a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * buffer, len - Buffer with writing data. 2446dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * writer_cb - Callback to monitor I/O progress (must not be NULL) 245a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * writer_opaque - An opaque pointer associated with the writer. 246a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * to - Milliseconds to complete the write. to < 0 indicates "no timeout" 2476dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine */ 2486dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void async_socket_write_rel(AsyncSocket* as, 2496dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine const void* buffer, uint32_t len, 2506dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine on_as_io_cb writer_cb, 2516dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine void* writer_opaque, 2526dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine int to); 2536dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 2546dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Get a deadline for the given time interval relative to "now". 2556dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * Param: 2566dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * as - Initialized AsyncSocket instance. 2576dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * rel - Time interval. If < 0 an infinite duration will be returned. 258a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine * Return: 2596dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine * A deadline for the given time interval relative to "now". 260a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine */ 2616dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern Duration async_socket_deadline(AsyncSocket* as, int rel); 2626dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine 2636dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkine/* Gets an opaque pointer associated with the socket's client */ 2646dc5c2cef91004488f04fc6e9c0946f6d3a29705Vladimir Chtchetkineextern void* async_socket_get_client_opaque(const AsyncSocket* as); 265a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine 266ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine/* Gets TCP port for the socket. */ 267ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkineextern int async_socket_get_port(const AsyncSocket* as); 268ef4ccd385650612a830a098f4b1eac48482b65b3Vladimir Chtchetkine 2697136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkine/* Checks if socket is connected. 2707136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkine * Return: 2717136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkine * Boolean: 1 - socket is connected, 0 - socket is not connected. 2727136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkine */ 2737136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkineextern int async_socket_is_connected(const AsyncSocket* as); 2747136b053b7fc7840ec64e01d1d19ab822e1f949aVladimir Chtchetkine 275a7383ef4eb8d3863c8d582ea0d6b2ddb42125cbaVladimir Chtchetkine#endif /* ANDROID_ASYNC_SOCKET_H_ */ 276