From 96cc46313ac6f99e97530170481e8c9c9be774ae Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 10 Jun 2013 17:55:06 -0700 Subject: [PATCH] Implement guac_socket_write(), fix constness. --- src/libguac/guacamole/socket.h | 6 +++--- src/libguac/socket-fd.c | 2 +- src/libguac/socket-nest.c | 2 +- src/libguac/socket.c | 27 ++++++++++++++++++++++++--- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/src/libguac/guacamole/socket.h b/src/libguac/guacamole/socket.h index a9085bfd..5d3e9b69 100644 --- a/src/libguac/guacamole/socket.h +++ b/src/libguac/guacamole/socket.h @@ -73,7 +73,7 @@ typedef ssize_t guac_socket_read_handler(guac_socket* socket, * @return The number of bytes written, or -1 if an error occurs. */ typedef ssize_t guac_socket_write_handler(guac_socket* socket, - void* buf, size_t count); + const void* buf, size_t count); /** * Generic handler for socket select operations, similar to the POSIX select() @@ -284,8 +284,8 @@ ssize_t guac_socket_write_string(guac_socket* socket, const char* str); ssize_t guac_socket_write_base64(guac_socket* socket, const void* buf, size_t count); /** - * Writes the given data to the specified socket. The data written may be - * buffered until the buffer is flushed automatically or manually. + * Writes the given data to the specified socket. The data written is not + * buffered, and will be sent immediately. * * If an error occurs while writing, a non-zero value is returned, and * guac_error is set appropriately. diff --git a/src/libguac/socket-fd.c b/src/libguac/socket-fd.c index 9139a906..3bb2ddff 100644 --- a/src/libguac/socket-fd.c +++ b/src/libguac/socket-fd.c @@ -81,7 +81,7 @@ ssize_t __guac_socket_fd_read_handler(guac_socket* socket, } ssize_t __guac_socket_fd_write_handler(guac_socket* socket, - void* buf, size_t count) { + const void* buf, size_t count) { __guac_socket_fd_data* data = (__guac_socket_fd_data*) socket->data; int retval; diff --git a/src/libguac/socket-nest.c b/src/libguac/socket-nest.c index 92e431f6..c6aa4270 100644 --- a/src/libguac/socket-nest.c +++ b/src/libguac/socket-nest.c @@ -69,7 +69,7 @@ typedef struct __guac_socket_nest_data { } __guac_socket_nest_data; ssize_t __guac_socket_nest_write_handler(guac_socket* socket, - void* buf, size_t count) { + const void* buf, size_t count) { __guac_socket_nest_data* data = (__guac_socket_nest_data*) socket->data; unsigned char* source = (unsigned char*) buf; diff --git a/src/libguac/socket.c b/src/libguac/socket.c index 050386a5..7ee29503 100644 --- a/src/libguac/socket.c +++ b/src/libguac/socket.c @@ -64,8 +64,8 @@ char __guac_socket_BASE64_CHARACTERS[64] = { '8', '9', '+', '/' }; -ssize_t __guac_socket_write(guac_socket* socket, - void* buf, size_t count) { +static ssize_t __guac_socket_write(guac_socket* socket, + const void* buf, size_t count) { /* If handler defined, call it. */ if (socket->write_handler) @@ -76,7 +76,28 @@ ssize_t __guac_socket_write(guac_socket* socket, } -/* TODO: Implement guac_socket_write (buffered write) */ +ssize_t guac_socket_write(guac_socket* socket, + const void* buf, size_t count) { + + const char* buffer = buf; + + /* Write until completely written */ + while (count > 0) { + + /* Attempt to write, return on error */ + int written = __guac_socket_write(socket, buffer, count); + if (written == -1) + return 1; + + /* Advance buffer as data written */ + buffer += written; + count -= written; + + } + + return 0; + +} ssize_t guac_socket_read(guac_socket* socket, void* buf, size_t count) {