AsyncStatus asyncWriter_write(AsyncWriter* aw) { int ret; if (aw->pos >= aw->buffsize) { return ASYNC_COMPLETE; } do { ret = socket_send(aw->io->fd, aw->buffer + aw->pos, aw->buffsize - aw->pos); if (ret == 0) { /* disconnection ! */ errno = ECONNRESET; return ASYNC_ERROR; } if (ret < 0) { if (errno == EINTR) /* loop on EINTR */ continue; if (errno == EWOULDBLOCK || errno == EAGAIN) { return ASYNC_NEED_MORE; } return ASYNC_ERROR; } aw->pos += ret; } while (aw->pos < aw->buffsize); loopIo_dontWantWrite(aw->io); return ASYNC_COMPLETE; }
AsyncStatus asyncConnector_run(AsyncConnector* ac) { switch (ac->state) { case CONNECT_ERROR: errno = ac->error; return ASYNC_ERROR; case CONNECT_CONNECTING: loopIo_dontWantWrite(ac->io); /* We need to read the socket error to determine if * the connection was really succesful or not. This * is optional, because in case of error a future * socket_recv() or socket_send() will fail anyway, but this * allows us to get a better error value as soon as * possible. */ ac->error = socket_get_error(ac->io->fd); if (ac->error == 0) { ac->state = CONNECT_COMPLETED; return ASYNC_COMPLETE; } ac->state = CONNECT_ERROR; errno = ac->error; return ASYNC_ERROR; default: return ASYNC_COMPLETE; } }
static void _adb_host_append_message(AdbHost* adb_host, const void* msg, int msglen) { printf("Append %d bytes to ADB host buffer.\n", msglen); /* Make sure that buffer can contain the appending data. */ if (adb_host->pending_send_buffer == NULL) { adb_host->pending_send_buffer = (uint8_t*)malloc(msglen); adb_host->pending_send_buffer_size = msglen; } else if ((adb_host->pending_send_data_size + msglen) > adb_host->pending_send_buffer_size) { adb_host->pending_send_buffer = (uint8_t*)realloc(adb_host->pending_send_buffer, adb_host->pending_send_data_size + msglen); adb_host->pending_send_buffer_size = adb_host->pending_send_data_size + msglen; } if (adb_host->pending_send_buffer == NULL) { D("Unable to allocate %d bytes for pending ADB host data.", adb_host->pending_send_data_size + msglen); adb_host->pending_send_buffer_size = adb_host->pending_send_data_size = 0; loopIo_dontWantWrite(adb_host->io); return; } memcpy(adb_host->pending_send_buffer + adb_host->pending_send_data_size, msg, msglen); loopIo_wantWrite(adb_host->io); }
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; } }
static void netPipe_resetState( NetPipe* pipe ) { if ((pipe->wakeWanted & PIPE_WAKE_WRITE) != 0) { loopIo_wantWrite(pipe->io); } else { loopIo_dontWantWrite(pipe->io); } if (pipe->state == STATE_CONNECTED && (pipe->wakeWanted & PIPE_WAKE_READ) != 0) { loopIo_wantRead(pipe->io); } else { loopIo_dontWantRead(pipe->io); } }
/* Write I/O callback on ADB host socket. */ static void _on_adb_host_write(AdbHost* adb_host) { while (adb_host->pending_send_data_size && adb_host->pending_send_buffer != NULL) { const int sent = socket_send(adb_host->host_so, adb_host->pending_send_buffer, adb_host->pending_send_data_size); if (sent < 0) { if (errno == EWOULDBLOCK) { /* Try again later. */ return; } else { D("Unable to send pending data to the ADB host: %s", strerror(errno)); free(adb_host->pending_send_buffer); adb_host->pending_send_buffer = NULL; adb_host->pending_send_buffer_size = 0; adb_host->pending_send_data_size = 0; break; } } else if (sent == 0) { /* Disconnect condition. */ free(adb_host->pending_send_buffer); adb_host->pending_send_buffer = NULL; adb_host->pending_send_buffer_size = 0; adb_host->pending_send_data_size = 0; _on_adb_host_disconnected(adb_host); break; } else if (sent == adb_host->pending_send_data_size) { free(adb_host->pending_send_buffer); adb_host->pending_send_buffer = NULL; adb_host->pending_send_buffer_size = 0; adb_host->pending_send_data_size = 0; } else { adb_host->pending_send_data_size -= sent; memmove(adb_host->pending_send_buffer, adb_host->pending_send_buffer + sent, adb_host->pending_send_data_size); return; } } loopIo_dontWantWrite(adb_host->io); }