void async_socket_connector_connect(AsyncSocketConnector* connector) { AsyncStatus status; T("ASC %s: Handling connect request. Connector FD = %d", _asc_socket_string(connector), connector->fd); if (_async_socket_connector_open_socket(connector) == 0) { const AsyncIOAction action = connector->on_connected_cb(connector->on_connected_cb_opaque, connector, ASIO_STATE_STARTED); if (action == ASIO_ACTION_ABORT) { D("ASC %s: Client has aborted connection. Connector FD = %d", _asc_socket_string(connector), connector->fd); return; } else { loopIo_init(connector->connector_io, connector->looper, connector->fd, _on_async_socket_connector_io, connector); status = asyncConnector_init(connector->connector, &connector->address, connector->connector_io); } } else { status = ASYNC_ERROR; } _on_async_socket_connector_connecting(connector, status); }
DrainerObject::DrainerObject(int socket_fd, Looper* looper, SocketDrainerImpl* parent) : mSocket(socket_fd), mLooper(looper), mParent(parent), mIo(), mSocketIsDrained(false) { shutdownWrite(); if (drainSocket() && mLooper && mParent) { loopIo_init(mIo, mLooper, mSocket, _on_read_socket_fd, this); loopIo_wantRead(mIo); loopIo_dontWantWrite(mIo); } else { // there is nothing to read, the drainer object is done mLooper = 0; } }
int attachUiProxy_create(int fd) { // Initialize the only AttachUIProxy instance. _attachUiProxy.sock = fd; _attachUiProxy.looper = looper_newCore(); loopIo_init(&_attachUiProxy.io, _attachUiProxy.looper, _attachUiProxy.sock, _attachUiProxy_io_func, &_attachUiProxy); loopIo_wantRead(&_attachUiProxy.io); return 0; }
int userEventsImpl_create(int fd) { _UserEventsImpl.sock = fd; _UserEventsImpl.event_header.event_type = -1; _UserEventsImpl.state = EXPECTS_HEADER; _UserEventsImpl.looper = looper_newCore(); loopIo_init(&_UserEventsImpl.io, _UserEventsImpl.looper, _UserEventsImpl.sock, _userEventsImpl_io_func, &_UserEventsImpl); asyncReader_init(&_UserEventsImpl.user_events_reader, &_UserEventsImpl.event_header, sizeof(_UserEventsImpl.event_header), &_UserEventsImpl.io); return 0; }
static void coreconsole_init(CoreConsole* cc, const SockAddress* address, Looper* looper) { int fd = socket_create_inet(SOCKET_STREAM); AsyncStatus status; cc->port = sock_address_get_port(address); cc->ok = 0; loopIo_init(cc->io, looper, fd, coreconsole_io_func, cc); if (fd >= 0) { status = asyncConsoleConnector_connect(cc->connector, address, cc->io); if (status == ASYNC_ERROR) { cc->ok = 0; } } }
/* I/O callback on ADB server socket. */ static void _on_server_socket_io(void* opaque, int fd, unsigned events) { AdbHost* adb_host; AdbGuest* adb_guest; AdbServer* adb_srv = (AdbServer*)opaque; assert(adb_srv->so == fd); /* Since this is a server socket, we only expect a connection I/O here. */ if ((events & LOOP_IO_READ) == 0) { D("Unexpected write I/O on ADB server socket"); return; } /* Create AdbHost instance for the new host connection. */ adb_host = _adb_host_new(adb_srv); /* Accept the connection. */ adb_host->host_so = socket_accept(fd, &adb_srv->socket_address); if (adb_host->host_so < 0) { D("Unable to accept ADB connection: %s", strerror(errno)); _adb_host_free(adb_host); return; } /* Prepare for I/O on the host connection socket. */ loopIo_init(adb_host->io, adb_srv->looper, adb_host->host_so, _on_adb_host_io, adb_host); /* Lets see if there is an ADB guest waiting for a host connection. */ adb_guest = (AdbGuest*)alist_remove_head(&adb_srv->pending_guests); if (adb_guest != NULL) { /* Tie up ADB host with the ADB guest. */ alist_insert_tail(&adb_srv->adb_guests, &adb_guest->list_entry); alist_insert_tail(&adb_srv->adb_hosts, &adb_host->list_entry); _adb_connect(adb_host, adb_guest); } else { /* Pend this connection. */ D("Pend ADB host %p(so=%d)", adb_host, adb_host->host_so); alist_insert_tail(&adb_srv->pending_hosts, &adb_host->list_entry); } /* Enable I/O on the host socket. */ loopIo_wantRead(adb_host->io); }
/******************************************************************************** * ADB server API *******************************************************************************/ int adb_server_init(int port) { if (!_adb_server_initialized) { /* Initialize the descriptor. */ memset(&_adb_server, 0, sizeof(_adb_server)); alist_init(&_adb_server.adb_hosts); alist_init(&_adb_server.adb_guests); alist_init(&_adb_server.pending_hosts); alist_init(&_adb_server.pending_guests); _adb_server.port = port; /* Create looper for an async I/O on the server. */ _adb_server.looper = looper_newCore(); if (_adb_server.looper == NULL) { E("Unable to create I/O looper for ADB server"); return -1; } /* Create loopback server socket for the ADB port. */ sock_address_init_inet(&_adb_server.socket_address, SOCK_ADDRESS_INET_LOOPBACK, port); _adb_server.so = socket_loopback_server(port, SOCKET_STREAM); if (_adb_server.so < 0) { E("Unable to create ADB server socket: %s", strerror(errno)); return -1; } /* Prepare server socket for I/O */ socket_set_nonblock(_adb_server.so); loopIo_init(_adb_server.io, _adb_server.looper, _adb_server.so, _on_server_socket_io, &_adb_server); loopIo_wantRead(_adb_server.io); D("ADB server has been initialized for port %d. Socket: %d", port, _adb_server.so); _adb_server_initialized = 1; } return 0; }
int coreCmdImpl_create(int fd) { _coreCmdImpl.sock = fd; _coreCmdImpl.looper = looper_newCore(); loopIo_init(&_coreCmdImpl.io, _coreCmdImpl.looper, _coreCmdImpl.sock, _coreCmdImpl_io_func, &_coreCmdImpl); _coreCmdImpl.cmd_state = EXPECTS_HEADER; _coreCmdImpl.cmd_param_buf = &_coreCmdImpl.cmd_param[0]; asyncReader_init(&_coreCmdImpl.async_reader, &_coreCmdImpl.cmd_header, sizeof(_coreCmdImpl.cmd_header), &_coreCmdImpl.io); _coreCmdImpl.sync_writer = syncsocket_init(fd); if (_coreCmdImpl.sync_writer == NULL) { derror("Unable to create writer for CoreCmdImpl instance: %s\n", errno_str); coreCmdImpl_destroy(); return -1; } return 0; }
/* Retry connection timer callback. * Param: * opaque - AsyncSocketConnector instance. */ static void _on_async_socket_connector_retry(void* opaque) { AsyncStatus status; AsyncSocketConnector* const connector = (AsyncSocketConnector*)opaque; T("ASC %s: Reconnect timer expired. Connector FD = %d", _asc_socket_string(connector), connector->fd); /* Reference the connector while we're in callback. */ async_socket_connector_reference(connector); /* Invoke the callback to notify about a connection retry attempt. */ AsyncIOAction action = connector->on_connected_cb(connector->on_connected_cb_opaque, connector, ASIO_STATE_RETRYING); if (action != ASIO_ACTION_ABORT) { /* Close handle opened for the previous (failed) attempt. */ _async_socket_connector_close_socket(connector); /* Retry connection attempt. */ if (_async_socket_connector_open_socket(connector) == 0) { loopIo_init(connector->connector_io, connector->looper, connector->fd, _on_async_socket_connector_io, connector); status = asyncConnector_init(connector->connector, &connector->address, connector->connector_io); } else { status = ASYNC_ERROR; } _on_async_socket_connector_connecting(connector, status); } else { D("ASC %s: Client has aborted connection. Connector FD = %d", _asc_socket_string(connector), connector->fd); } /* Release the connector after we're done with the callback. */ async_socket_connector_release(connector); }
void* netPipe_initFromAddress( void* hwpipe, const SockAddress* address, Looper* looper ) { NetPipe* pipe; ANEW0(pipe); pipe->hwpipe = hwpipe; pipe->state = STATE_INIT; { AsyncStatus status; int fd = socket_create( sock_address_get_family(address), SOCKET_STREAM ); if (fd < 0) { D("%s: Could create socket from address family!", __FUNCTION__); netPipe_free(pipe); return NULL; } loopIo_init(pipe->io, looper, fd, netPipe_io_func, pipe); asyncConnector_init(pipe->connector, address, pipe->io); pipe->state = STATE_CONNECTING; status = asyncConnector_run(pipe->connector); if (status == ASYNC_ERROR) { D("%s: Could not connect to socket: %s", __FUNCTION__, errno_str); netPipe_free(pipe); return NULL; } if (status == ASYNC_COMPLETE) { pipe->state = STATE_CONNECTED; netPipe_resetState(pipe); } } return pipe; }
int uiCmdProxy_create(int fd) { _uiCmdProxy.sock = fd; _uiCmdProxy.looper = looper_newCore(); loopIo_init(&_uiCmdProxy.io, _uiCmdProxy.looper, _uiCmdProxy.sock, _uiCmdProxy_io_func, &_uiCmdProxy); loopIo_wantRead(&_uiCmdProxy.io); _uiCmdProxy.sync_writer = syncsocket_init(fd); if (_uiCmdProxy.sync_writer == NULL) { derror("Unable to initialize UICmdProxy writer: %s\n", errno_str); uiCmdProxy_destroy(); return -1; } { AndroidHwControlFuncs funcs; funcs.light_brightness = _uiCmdProxy_brightness_change_callback; android_hw_control_set(&_uiCmdProxy, &funcs); } return 0; }