Add read handler, call free handler when needed, add guac_socket_read.

This commit is contained in:
Michael Jumper 2012-10-19 12:07:49 -07:00
parent 208649306a
commit d44ec4b2c1
2 changed files with 49 additions and 14 deletions

View File

@ -300,7 +300,7 @@ ssize_t guac_socket_write(guac_socket* socket, const void* buf, size_t count);
* @return The number of bytes read, or non-zero if an error occurs while * @return The number of bytes read, or non-zero if an error occurs while
* reading. * reading.
*/ */
ssize_t guac_socket_read(guac_socket* socket, const void* buf, size_t count); ssize_t guac_socket_read(guac_socket* socket, void* buf, size_t count);
/** /**
* Flushes the base64 buffer, writing padding characters as necessary. * Flushes the base64 buffer, writing padding characters as necessary.

View File

@ -76,7 +76,48 @@ ssize_t __guac_socket_write(guac_socket* socket,
} }
/* Write bytes, limit rate */ /* TODO: Implement guac_socket_write (buffered write) */
ssize_t guac_socket_read(guac_socket* socket, void* buf, size_t count) {
/* If handler defined, call it. */
if (socket->read_handler)
return socket->read_handler(socket, buf, count);
/* Otherwise, pretend nothing was read. */
return 0;
}
int guac_socket_select(guac_socket* socket, int usec_timeout) {
/* Call select handler if defined */
if (socket->select_handler)
return socket->select_handler(socket, usec_timeout);
/* Otherwise, assume ready. */
return 1;
}
ssize_t __guac_socket_fd_read_handler(guac_socket* socket,
void* buf, size_t count) {
guac_socket_fd_data* data = (guac_socket_fd_data*) socket->data;
/* Read from socket */
int retval = read(data->fd, buf, count);
/* Record errors in guac_error */
if (retval < 0) {
guac_error = GUAC_STATUS_SEE_ERRNO;
guac_error_message = "Error reading data from socket";
}
return retval;
}
ssize_t __guac_socket_fd_write_handler(guac_socket* socket, ssize_t __guac_socket_fd_write_handler(guac_socket* socket,
void* buf, size_t count) { void* buf, size_t count) {
@ -193,6 +234,7 @@ guac_socket* guac_socket_open(int fd) {
socket->data = data; socket->data = data;
/* FIXME: Set read/write/free handlers */ /* FIXME: Set read/write/free handlers */
socket->read_handler = __guac_socket_fd_read_handler;
socket->write_handler = __guac_socket_fd_write_handler; socket->write_handler = __guac_socket_fd_write_handler;
socket->select_handler = __guac_socket_fd_select_handler; socket->select_handler = __guac_socket_fd_select_handler;
@ -201,6 +243,11 @@ guac_socket* guac_socket_open(int fd) {
} }
void guac_socket_free(guac_socket* socket) { void guac_socket_free(guac_socket* socket) {
/* Call free handler if defined */
if (socket->free_handler)
socket->free_handler(socket);
guac_socket_flush(socket); guac_socket_flush(socket);
free(socket->__instructionbuf); free(socket->__instructionbuf);
free(socket); free(socket);
@ -362,15 +409,3 @@ ssize_t guac_socket_flush_base64(guac_socket* socket) {
} }
int guac_socket_select(guac_socket* socket, int usec_timeout) {
/* Call select handler if defined */
if (socket->select_handler)
return socket->select_handler(socket, usec_timeout);
/* Otherwise, assume ready. */
return 1;
}