Implement guac_socket_write(), fix constness.

This commit is contained in:
Michael Jumper 2013-06-10 17:55:06 -07:00
parent e7c70111ef
commit 96cc46313a
4 changed files with 29 additions and 8 deletions

View File

@ -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.

View File

@ -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;

View File

@ -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;

View File

@ -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) {