diff options
author | David 'Digit' Turner <digit@android.com> | 2011-03-17 14:57:51 +0100 |
---|---|---|
committer | David 'Digit' Turner <digit@android.com> | 2011-04-11 18:08:11 +0200 |
commit | f9e333ade2529f257ced6bcff8e5824cb07eacf9 (patch) | |
tree | c3ea65717a284ae744764b4ecf8c5b745ea3415d | |
parent | 2086c639648ccecc9081705a279249ce939d818b (diff) | |
download | external_qemu-f9e333ade2529f257ced6bcff8e5824cb07eacf9.zip external_qemu-f9e333ade2529f257ced6bcff8e5824cb07eacf9.tar.gz external_qemu-f9e333ade2529f257ced6bcff8e5824cb07eacf9.tar.bz2 |
Simplify async utils by removing extra LoopIo parameter.
This patch removes the LoopIo parameter from asyncReader_run() by
storing the initial pointer passed to asyncReader_init() inside the
object itself.
Same treatment is performed for:
- AsyncReader
- AsyncWriter
- AsyncLineReader
- AsyncConnector
- AsyncConsoleConnect
Change-Id: Ic74b817e4c326230ca1d38b3a5d8c4790c4f90c1
-rw-r--r-- | android/async-console.c | 24 | ||||
-rw-r--r-- | android/async-console.h | 3 | ||||
-rw-r--r-- | android/async-utils.c | 35 | ||||
-rw-r--r-- | android/async-utils.h | 47 | ||||
-rw-r--r-- | android/main-common.c | 2 | ||||
-rw-r--r-- | android/protocol/attach-ui-proxy.c | 2 | ||||
-rw-r--r-- | android/protocol/core-commands-impl.c | 2 | ||||
-rw-r--r-- | android/protocol/fb-updates-proxy.c | 6 | ||||
-rw-r--r-- | android/protocol/ui-commands-proxy.c | 2 | ||||
-rw-r--r-- | android/protocol/user-events-impl.c | 2 |
10 files changed, 76 insertions, 49 deletions
diff --git a/android/async-console.c b/android/async-console.c index f486df0..523c2b2 100644 --- a/android/async-console.c +++ b/android/async-console.c @@ -45,10 +45,10 @@ enum { /* A helper function to prepare the line reader and switch to a new state */ static AsyncStatus -_acc_prepareLineReader(AsyncConsoleConnector* acc, LoopIo* io, int newState) +_acc_prepareLineReader(AsyncConsoleConnector* acc, int newState) { acc->state = newState; - asyncLineReader_init(acc->lreader, acc->lbuff, sizeof(acc->lbuff), io); + asyncLineReader_init(acc->lreader, acc->lbuff, sizeof(acc->lbuff), acc->io); return ASYNC_NEED_MORE; } @@ -59,13 +59,13 @@ asyncConsoleConnector_connect(AsyncConsoleConnector* acc, { acc->state = STATE_INITIAL; acc->address = address[0]; - return asyncConsoleConnector_run(acc, io); + acc->io = io; + return asyncConsoleConnector_run(acc); } AsyncStatus -asyncConsoleConnector_run(AsyncConsoleConnector* acc, - LoopIo* io) +asyncConsoleConnector_run(AsyncConsoleConnector* acc) { AsyncStatus status = ASYNC_NEED_MORE; @@ -78,29 +78,29 @@ asyncConsoleConnector_run(AsyncConsoleConnector* acc, case STATE_INITIAL: /* initial connection attempt */ acc->state = STATE_CONNECTING; - status = asyncConnector_init(acc->connector, &acc->address, io); + status = asyncConnector_init(acc->connector, &acc->address, acc->io); if (status == ASYNC_ERROR) goto SET_ERROR; if (status == ASYNC_COMPLETE) { /* immediate connection */ - _acc_prepareLineReader(acc, io, STATE_READ_BANNER_1); + _acc_prepareLineReader(acc, STATE_READ_BANNER_1); continue; } break; case STATE_CONNECTING: /* still trying to connect */ - status = asyncConnector_run(acc->connector, io); + status = asyncConnector_run(acc->connector); if (status == ASYNC_ERROR) goto SET_ERROR; if (status == ASYNC_COMPLETE) { - _acc_prepareLineReader(acc, io, STATE_READ_BANNER_1); + _acc_prepareLineReader(acc, STATE_READ_BANNER_1); continue; } break; case STATE_READ_BANNER_1: /* reading the first banner line */ - status = asyncLineReader_read(acc->lreader, io); + status = asyncLineReader_read(acc->lreader); if (status == ASYNC_ERROR) goto SET_ERROR; @@ -112,13 +112,13 @@ asyncConsoleConnector_run(AsyncConsoleConnector* acc, goto BAD_BANNER; } /* ok, fine, prepare for the next banner line then */ - _acc_prepareLineReader(acc, io, STATE_READ_BANNER_2); + _acc_prepareLineReader(acc, STATE_READ_BANNER_2); continue; } break; case STATE_READ_BANNER_2: /* reading the second banner line */ - status = asyncLineReader_read(acc->lreader, io); + status = asyncLineReader_read(acc->lreader); if (status == ASYNC_ERROR) goto SET_ERROR; diff --git a/android/async-console.h b/android/async-console.h index 6779ae2..2d8519a 100644 --- a/android/async-console.h +++ b/android/async-console.h @@ -56,8 +56,7 @@ asyncConsoleConnector_connect(AsyncConsoleConnector* acc, * Not enough data was exchanged, call this function later. */ AsyncStatus -asyncConsoleConnector_run(AsyncConsoleConnector* acc, - LoopIo* io); +asyncConsoleConnector_run(AsyncConsoleConnector* acc); #endif /* ANDROID_ASYNC_CONSOLE_H */ diff --git a/android/async-utils.c b/android/async-utils.c index a66e32f..35a144a 100644 --- a/android/async-utils.c +++ b/android/async-utils.c @@ -30,8 +30,7 @@ asyncReader_init(AsyncReader* ar, } AsyncStatus -asyncReader_read(AsyncReader* ar, - LoopIo* io) +asyncReader_read(AsyncReader* ar) { int ret; @@ -40,7 +39,7 @@ asyncReader_read(AsyncReader* ar, } do { - ret = socket_recv(io->fd, ar->buffer + ar->pos, ar->buffsize - ar->pos); + ret = socket_recv(ar->io->fd, ar->buffer + ar->pos, ar->buffsize - ar->pos); if (ret == 0) { /* disconnection ! */ errno = ECONNRESET; @@ -50,7 +49,7 @@ asyncReader_read(AsyncReader* ar, if (errno == EINTR) /* loop on EINTR */ continue; if (errno == EWOULDBLOCK || errno == EAGAIN) { - loopIo_wantRead(io); + loopIo_wantRead(ar->io); return ASYNC_NEED_MORE; } return ASYNC_ERROR; @@ -59,7 +58,7 @@ asyncReader_read(AsyncReader* ar, } while (ar->pos < ar->buffsize); - loopIo_dontWantRead(io); + loopIo_dontWantRead(ar->io); return ASYNC_COMPLETE; } @@ -77,8 +76,7 @@ asyncWriter_init(AsyncWriter* aw, } AsyncStatus -asyncWriter_write(AsyncWriter* aw, - LoopIo* io) +asyncWriter_write(AsyncWriter* aw) { int ret; @@ -87,7 +85,7 @@ asyncWriter_write(AsyncWriter* aw, } do { - ret = socket_send(io->fd, aw->buffer + aw->pos, aw->buffsize - aw->pos); + ret = socket_send(aw->io->fd, aw->buffer + aw->pos, aw->buffsize - aw->pos); if (ret == 0) { /* disconnection ! */ errno = ECONNRESET; @@ -105,7 +103,7 @@ asyncWriter_write(AsyncWriter* aw, } while (aw->pos < aw->buffsize); - loopIo_dontWantWrite(io); + loopIo_dontWantWrite(aw->io); return ASYNC_COMPLETE; } @@ -119,13 +117,13 @@ asyncLineReader_init(AsyncLineReader* alr, alr->buffer = buffer; alr->buffsize = buffsize; alr->pos = 0; + alr->io = io; if (buffsize > 0) loopIo_wantRead(io); } AsyncStatus -asyncLineReader_read(AsyncLineReader* alr, - LoopIo* io) +asyncLineReader_read(AsyncLineReader* alr) { int ret; @@ -136,7 +134,7 @@ asyncLineReader_read(AsyncLineReader* alr, do { char ch; - ret = socket_recv(io->fd, &ch, 1); + ret = socket_recv(alr->io->fd, &ch, 1); if (ret == 0) { /* disconnection ! */ errno = ECONNRESET; @@ -146,20 +144,20 @@ asyncLineReader_read(AsyncLineReader* alr, if (errno == EINTR) /* loop on EINTR */ continue; if (errno == EWOULDBLOCK || errno == EAGAIN) { - loopIo_wantRead(io); + loopIo_wantRead(alr->io); return ASYNC_NEED_MORE; } return ASYNC_ERROR; } alr->buffer[alr->pos++] = (uint8_t)ch; if (ch == '\n') { - loopIo_dontWantRead(io); + loopIo_dontWantRead(alr->io); return ASYNC_COMPLETE; } } while (alr->pos < alr->buffsize); /* Not enough room in the input buffer!*/ - loopIo_dontWantRead(io); + loopIo_dontWantRead(alr->io); errno = ENOMEM; return ASYNC_ERROR; } @@ -216,6 +214,7 @@ asyncConnector_init(AsyncConnector* ac, { int ret; ac->error = 0; + ac->io = io; ret = socket_connect(io->fd, address); if (ret == 0) { ac->state = CONNECT_COMPLETED; @@ -235,7 +234,7 @@ asyncConnector_init(AsyncConnector* ac, } AsyncStatus -asyncConnector_run(AsyncConnector* ac, LoopIo* io) +asyncConnector_run(AsyncConnector* ac) { switch (ac->state) { case CONNECT_ERROR: @@ -243,7 +242,7 @@ asyncConnector_run(AsyncConnector* ac, LoopIo* io) return ASYNC_ERROR; case CONNECT_CONNECTING: - loopIo_dontWantWrite(io); + 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 @@ -251,7 +250,7 @@ asyncConnector_run(AsyncConnector* ac, LoopIo* io) * allows us to get a better error value as soon as * possible. */ - ac->error = socket_get_error(io->fd); + ac->error = socket_get_error(ac->io->fd); if (ac->error == 0) { ac->state = CONNECT_COMPLETED; return ASYNC_COMPLETE; diff --git a/android/async-utils.h b/android/async-utils.h index 0b52f37..93148c1 100644 --- a/android/async-utils.h +++ b/android/async-utils.h @@ -32,6 +32,13 @@ typedef enum { ASYNC_NEED_MORE /* more data is needed, try again later */ } AsyncStatus; +/************************************************************************** + ************************************************************************** + ***** + ***** A S Y N C R E A D E R + ***** + *****/ + /* An AsyncReader makes it easier to read a given number of bytes into * a target buffer asynchronously. Usage is the following: * @@ -44,6 +51,7 @@ typedef struct { uint8_t* buffer; size_t buffsize; size_t pos; + LoopIo* io; } AsyncReader; /* Setup an ASyncReader, by giving the address of the read buffer, @@ -68,8 +76,14 @@ void asyncReader_init(AsyncReader* ar, * ASYNC_NEED_MORE: If there was not enough incoming data to complete * the read (or if 'events' doesn't contain LOOP_IO_READ). */ -AsyncStatus asyncReader_read(AsyncReader* ar, - LoopIo* io); +AsyncStatus asyncReader_read(AsyncReader* ar); + +/************************************************************************** + ************************************************************************** + ***** + ***** A S Y N C W R I T E R + ***** + *****/ /* An AsyncWriter is the counterpart of an AsyncReader, but for writing * data to a file descriptor asynchronously. @@ -78,6 +92,7 @@ typedef struct { const uint8_t* buffer; size_t buffsize; size_t pos; + LoopIo* io; } AsyncWriter; /* Setup an ASyncWriter, by giving the address of the write buffer, @@ -102,10 +117,16 @@ void asyncWriter_init(AsyncWriter* aw, * ASYNC_NEED_MORE: If not all bytes could be sent yet (or if 'events' * doesn't contain LOOP_IO_WRITE). */ -AsyncStatus asyncWriter_write(AsyncWriter* aw, - LoopIo* io); +AsyncStatus asyncWriter_write(AsyncWriter* aw); +/************************************************************************** + ************************************************************************** + ***** + ***** A S Y N C L I N E R E A D E R + ***** + *****/ + /* An AsyncLineReader allows you to read one line of text asynchronously. * The biggest difference with AsyncReader is that you don't know the line * size in advance, so the object will read data byte-by-byte until it @@ -115,6 +136,7 @@ typedef struct { uint8_t* buffer; size_t buffsize; size_t pos; + LoopIo* io; } AsyncLineReader; /* Setup an AsyncLineReader to read at most 'buffsize' characters (bytes) @@ -148,8 +170,7 @@ void asyncLineReader_init(AsyncLineReader* alr, * ASYNC_NEED_MORE: If there was not enough incoming data (or events * does not contain LOOP_IO_READ). */ -AsyncStatus asyncLineReader_read(AsyncLineReader* alr, - LoopIo* io); +AsyncStatus asyncLineReader_read(AsyncLineReader* alr); /* Return a pointer to the NON-ZERO-TERMINATED line characters, if any. * If 'pLength" is not NULL, the function sets '*pLength' to the length @@ -169,11 +190,19 @@ const char* asyncLineReader_getLineRaw(AsyncLineReader* alr, int *pLength); */ const char* asyncLineReader_getLine(AsyncLineReader* alr); +/************************************************************************** + ************************************************************************** + ***** + ***** A S Y N C C O N N E C T O R + ***** + *****/ + /* Asynchronous connection to a socket */ typedef struct { - int error; - int state; + int error; + int state; + LoopIo* io; } AsyncConnector; AsyncStatus @@ -182,6 +211,6 @@ asyncConnector_init(AsyncConnector* ac, LoopIo* io); AsyncStatus -asyncConnector_run(AsyncConnector* ac, LoopIo* io); +asyncConnector_run(AsyncConnector* ac); #endif /* ANDROID_ASYNC_UTILS_H */ diff --git a/android/main-common.c b/android/main-common.c index b237116..05a2cb0 100644 --- a/android/main-common.c +++ b/android/main-common.c @@ -972,7 +972,7 @@ coreconsole_io_func(void* opaque, int fd, unsigned events) { CoreConsole* cc = opaque; AsyncStatus status; - status = asyncConsoleConnector_run(cc->connector, cc->io); + status = asyncConsoleConnector_run(cc->connector); if (status == ASYNC_COMPLETE) { cc->ok = 1; } diff --git a/android/protocol/attach-ui-proxy.c b/android/protocol/attach-ui-proxy.c index 191b36d..a0bc7fc 100644 --- a/android/protocol/attach-ui-proxy.c +++ b/android/protocol/attach-ui-proxy.c @@ -67,7 +67,7 @@ _attachUiProxy_io_func(void* opaque, int fd, unsigned events) // Try to read asyncReader_init(&reader, read_buf, sizeof(read_buf), &uicmd->io); - status = asyncReader_read(&reader, &uicmd->io); + status = asyncReader_read(&reader); // We expect only error status here. if (status != ASYNC_ERROR) { derror("Unexpected read status %d in _attachUiProxy_io_func\n", status); diff --git a/android/protocol/core-commands-impl.c b/android/protocol/core-commands-impl.c index 7fa2a0b..4366529 100644 --- a/android/protocol/core-commands-impl.c +++ b/android/protocol/core-commands-impl.c @@ -345,7 +345,7 @@ _coreCmdImpl_io_func(void* opaque, int fd, unsigned events) corecmd = (CoreCmdImpl*)opaque; // Read whatever is expected from the socket. - status = asyncReader_read(&corecmd->async_reader, &corecmd->io); + status = asyncReader_read(&corecmd->async_reader); switch (status) { case ASYNC_COMPLETE: switch (corecmd->cmd_state) { diff --git a/android/protocol/fb-updates-proxy.c b/android/protocol/fb-updates-proxy.c index ec7414d..c1b431e 100644 --- a/android/protocol/fb-updates-proxy.c +++ b/android/protocol/fb-updates-proxy.c @@ -156,7 +156,7 @@ _proxyFb_io_write(ProxyFramebuffer* proxy_fb) FBUpdateNotify* current_update = proxy_fb->fb_update_head; // Lets continue writing of the current notification. const AsyncStatus status = - asyncWriter_write(&proxy_fb->fb_update_writer, &proxy_fb->io); + asyncWriter_write(&proxy_fb->fb_update_writer); switch (status) { case ASYNC_COMPLETE: // Done with the current update. Move on to the next one. @@ -202,7 +202,7 @@ _proxyFb_io_read(ProxyFramebuffer* proxy_fb) // Read the request header. DisplaySurface* dsu; const AsyncStatus status = - asyncReader_read(&proxy_fb->fb_req_reader, &proxy_fb->io); + asyncReader_read(&proxy_fb->fb_req_reader); switch (status) { case ASYNC_COMPLETE: // Request header is received @@ -320,7 +320,7 @@ proxyFb_update(void* opaque, int x, int y, int w, int h) asyncWriter_init(&proxy_fb->fb_update_writer, &proxy_fb->fb_update_head->message, proxy_fb->fb_update_head->message_size, &proxy_fb->io); - status = asyncWriter_write(&proxy_fb->fb_update_writer, &proxy_fb->io); + status = asyncWriter_write(&proxy_fb->fb_update_writer); switch (status) { case ASYNC_COMPLETE: fbupdatenotify_delete(descr); diff --git a/android/protocol/ui-commands-proxy.c b/android/protocol/ui-commands-proxy.c index 76bf883..1aedc62 100644 --- a/android/protocol/ui-commands-proxy.c +++ b/android/protocol/ui-commands-proxy.c @@ -116,7 +116,7 @@ _uiCmdProxy_io_func(void* opaque, int fd, unsigned events) // Try to read asyncReader_init(&reader, read_buf, sizeof(read_buf), &uicmd->io); - status = asyncReader_read(&reader, &uicmd->io); + status = asyncReader_read(&reader); // We expect only error status here. if (status != ASYNC_ERROR) { derror("Unexpected read status %d in _uiCmdProxy_io_func\n", status); diff --git a/android/protocol/user-events-impl.c b/android/protocol/user-events-impl.c index 5c9525e..7df820e 100644 --- a/android/protocol/user-events-impl.c +++ b/android/protocol/user-events-impl.c @@ -90,7 +90,7 @@ _userEventsImpl_io_func(void* opaque, int fd, unsigned events) ueimpl = (UserEventsImpl*)opaque; // Read whatever is expected from the socket. - status = asyncReader_read(&ueimpl->user_events_reader, &ueimpl->io); + status = asyncReader_read(&ueimpl->user_events_reader); switch (status) { |