diff --git a/libguac/include/client-handlers.h b/libguac/include/client-handlers.h index de7603ae..8ad48435 100644 --- a/libguac/include/client-handlers.h +++ b/libguac/include/client-handlers.h @@ -40,6 +40,7 @@ #include "client.h" #include "protocol.h" +#include "error.h" /** * Provides initial handler functions and a lookup structure for automatically @@ -52,7 +53,7 @@ /** * Internal handler for Guacamole instructions. */ -typedef int __guac_instruction_handler(guac_client* client, guac_instruction* copied); +typedef guac_status __guac_instruction_handler(guac_client* client, guac_instruction* copied); /** * Structure mapping an instruction opcode to an instruction handler. @@ -76,35 +77,35 @@ typedef struct __guac_instruction_handler_mapping { * is received, this handler will be called. Sync instructions are automatically * handled, thus there is no client handler for sync instruction. */ -int __guac_handle_sync(guac_client* client, guac_instruction* instruction); +guac_status __guac_handle_sync(guac_client* client, guac_instruction* instruction); /** * Internal initial handler for the mouse instruction. When a mouse instruction * is received, this handler will be called. The client's mouse handler will * be invoked if defined. */ -int __guac_handle_mouse(guac_client* client, guac_instruction* instruction); +guac_status __guac_handle_mouse(guac_client* client, guac_instruction* instruction); /** * Internal initial handler for the key instruction. When a key instruction * is received, this handler will be called. The client's key handler will * be invoked if defined. */ -int __guac_handle_key(guac_client* client, guac_instruction* instruction); +guac_status __guac_handle_key(guac_client* client, guac_instruction* instruction); /** * Internal initial handler for the clipboard instruction. When a clipboard instruction * is received, this handler will be called. The client's clipboard handler will * be invoked if defined. */ -int __guac_handle_clipboard(guac_client* client, guac_instruction* instruction); +guac_status __guac_handle_clipboard(guac_client* client, guac_instruction* instruction); /** * Internal initial handler for the disconnect instruction. When a disconnect instruction * is received, this handler will be called. Disconnect instructions are automatically * handled, thus there is no client handler for disconnect instruction. */ -int __guac_handle_disconnect(guac_client* client, guac_instruction* instruction); +guac_status __guac_handle_disconnect(guac_client* client, guac_instruction* instruction); /** * Instruction handler mapping table. This is a NULL-terminated array of diff --git a/libguac/include/client.h b/libguac/include/client.h index 3483ce01..3c3e5387 100644 --- a/libguac/include/client.h +++ b/libguac/include/client.h @@ -312,9 +312,10 @@ guac_client* guac_get_client(int client_fd); * Enter the main network message handling loop for the given client. * * @param client The proxy client to start handling messages of/for. - * @return Zero if the client successfully started, non-zero if an error - * occurs during startup. Note that this function will still return - * zero if an error occurs while the client is running. + * @return GUAC_STATUS_SUCCESS if the client successfully started, or any + * other status code if an error occurs during startup. Note that + * this function will still return GUAC_STATUS_SUCCESS + * if an error occurs while the client is running. */ guac_status guac_start_client(guac_client* client); @@ -323,7 +324,7 @@ guac_status guac_start_client(guac_client* client); * * @param client The proxy client to free all reasources of. */ -guac_status guac_free_client(guac_client* client); +void guac_free_client(guac_client* client); /** * Call the appropriate handler defined by the given client for the given @@ -334,6 +335,9 @@ guac_status guac_free_client(guac_client* client); * @param client The proxy client whose handlers should be called. * @param instruction The instruction to pass to the proxy client via the * appropriate handler. + * @return GUAC_STATUS_SUCCESS if no handler was defined or if the handler + * was called successfully, or any other status code if an error + * occurs while calling the defined handler. */ guac_status guac_client_handle_instruction(guac_client* client, guac_instruction* instruction); @@ -344,7 +348,7 @@ guac_status guac_client_handle_instruction(guac_client* client, guac_instruction * * @param client The proxy client to stop. */ -guac_status guac_client_stop(guac_client* client); +void guac_client_stop(guac_client* client); /** * Allocates a new buffer (invisible layer). An arbitrary index is @@ -372,7 +376,7 @@ guac_layer* guac_client_alloc_layer(guac_client* client, int index); * @param client The proxy client to return the buffer to. * @param layer The buffer to return to the pool of available buffers. */ -guac_status guac_client_free_buffer(guac_client* client, guac_layer* layer); +void guac_client_free_buffer(guac_client* client, guac_layer* layer); /** * The default Guacamole client layer, layer 0. diff --git a/libguac/include/error.h b/libguac/include/error.h index 06da4a8f..2b897339 100644 --- a/libguac/include/error.h +++ b/libguac/include/error.h @@ -67,6 +67,12 @@ typedef enum guac_status { */ GUAC_STATUS_NO_INPUT, + /** + * No data could be read from an input stream, but data may be available + * if the operation is retried. + */ + GUAC_STATUS_INPUT_UNAVAILABLE, + /** * A timeout occurred while reading from the input stream associated * with the operation. diff --git a/libguac/include/guacio.h b/libguac/include/guacio.h index f0ee209d..c5ba5afb 100644 --- a/libguac/include/guacio.h +++ b/libguac/include/guacio.h @@ -151,7 +151,7 @@ int64_t guac_parse_int(const char* str); * @param i The unsigned int to write. * @return Zero on success, or non-zero if an error occurs while writing. */ -ssize_t guac_write_int(GUACIO* io, int64_t i); +guac_status guac_write_int(GUACIO* io, int64_t i); /** * Writes the given string to the given GUACIO object. The data @@ -164,7 +164,7 @@ ssize_t guac_write_int(GUACIO* io, int64_t i); * @param str The string to write. * @return Zero on success, or non-zero if an error occurs while writing. */ -ssize_t guac_write_string(GUACIO* io, const char* str); +guac_status guac_write_string(GUACIO* io, const char* str); /** * Writes the given binary data to the given GUACIO object as base64-encoded @@ -179,7 +179,7 @@ ssize_t guac_write_string(GUACIO* io, const char* str); * @param count The number of bytes to write. * @return Zero on success, or non-zero if an error occurs while writing. */ -ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count); +guac_status guac_write_base64(GUACIO* io, const void* buf, size_t count); /** * Flushes the base64 buffer, writing padding characters as necessary. @@ -187,7 +187,7 @@ ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count); * @param io The GUACIO object to flush * @return Zero on success, or non-zero if an error occurs during flush. */ -ssize_t guac_flush_base64(GUACIO* io); +guac_status guac_flush_base64(GUACIO* io); /** * Flushes the write buffer. @@ -195,7 +195,7 @@ ssize_t guac_flush_base64(GUACIO* io); * @param io The GUACIO object to flush * @return Zero on success, or non-zero if an error occurs during flush. */ -ssize_t guac_flush(GUACIO* io); +guac_status guac_flush(GUACIO* io); /** @@ -208,7 +208,7 @@ ssize_t guac_flush(GUACIO* io); * @return Positive on success, zero if the timeout elapsed and no data is * available, negative on error. */ -int guac_select(GUACIO* io, int usec_timeout); +guac_status guac_select(GUACIO* io, int usec_timeout); /** * Frees resources allocated to the given GUACIO object. Note that this diff --git a/libguac/include/protocol.h b/libguac/include/protocol.h index 5f73191f..8ae52846 100644 --- a/libguac/include/protocol.h +++ b/libguac/include/protocol.h @@ -176,18 +176,18 @@ void guac_free_instruction(guac_instruction* instruction); * * @param io The GUACIO connection to use. * @param args The NULL-terminated array of argument names (strings). - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_args(GUACIO* io, const char** name); +guac_status guac_send_args(GUACIO* io, const char** name); /** * Sends a name instruction over the given GUACIO connection. * * @param io The GUACIO connection to use. * @param name The name to send within the name instruction. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_name(GUACIO* io, const char* name); +guac_status guac_send_name(GUACIO* io, const char* name); /** * Sends a sync instruction over the given GUACIO connection. The @@ -195,27 +195,27 @@ int guac_send_name(GUACIO* io, const char* name); * * @param io The GUACIO connection to use. * @param timestamp The current timestamp (in milliseconds). - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_sync(GUACIO* io, guac_timestamp timestamp); +guac_status guac_send_sync(GUACIO* io, guac_timestamp timestamp); /** * Sends an error instruction over the given GUACIO connection. * * @param io The GUACIO connection to use. * @param error The description associated with the error. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_error(GUACIO* io, const char* error); +guac_status guac_send_error(GUACIO* io, const char* error); /** * Sends a clipboard instruction over the given GUACIO connection. * * @param io The GUACIO connection to use. * @param data The clipboard data to send. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_clipboard(GUACIO* io, const char* data); +guac_status guac_send_clipboard(GUACIO* io, const char* data); /** * Sends a size instruction over the given GUACIO connection. @@ -223,9 +223,9 @@ int guac_send_clipboard(GUACIO* io, const char* data); * @param io The GUACIO connection to use. * @param w The width of the display. * @param h The height of the display. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_size(GUACIO* io, int w, int h); +guac_status guac_send_size(GUACIO* io, int w, int h); /** * Sends a copy instruction over the given GUACIO connection. @@ -242,9 +242,9 @@ int guac_send_size(GUACIO* io, int w, int h); * should be copied. * @param dsty The Y coordinate of the destination, where the source rectangle * should be copied. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_copy(GUACIO* io, +guac_status guac_send_copy(GUACIO* io, const guac_layer* srcl, int srcx, int srcy, int w, int h, guac_composite_mode mode, const guac_layer* dstl, int dstx, int dsty); @@ -262,9 +262,9 @@ int guac_send_copy(GUACIO* io, * @param g The green component of the color of the rectangle. * @param b The blue component of the color of the rectangle. * @param a The alpha (transparency) component of the color of the rectangle. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_rect(GUACIO* io, +guac_status guac_send_rect(GUACIO* io, guac_composite_mode mode, const guac_layer* layer, int x, int y, int width, int height, int r, int g, int b, int a); @@ -278,9 +278,9 @@ int guac_send_rect(GUACIO* io, * @param y The Y coordinate of the clipping rectangle. * @param width The width of the clipping rectangle. * @param height The height of the clipping rectangle. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_clip(GUACIO* io, const guac_layer* layer, +guac_status guac_send_clip(GUACIO* io, const guac_layer* layer, int x, int y, int width, int height); /** @@ -293,9 +293,9 @@ int guac_send_clip(GUACIO* io, const guac_layer* layer, * @param x The destination X coordinate. * @param y The destination Y coordinate. * @param surface A cairo surface containing the image data to send. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_png(GUACIO* io, guac_composite_mode mode, +guac_status guac_send_png(GUACIO* io, guac_composite_mode mode, const guac_layer* layer, int x, int y, cairo_surface_t* surface); /** @@ -306,19 +306,20 @@ int guac_send_png(GUACIO* io, guac_composite_mode mode, * @param x The X coordinate of the cursor hotspot. * @param y The Y coordinate of the cursor hotspot. * @param surface A cairo surface containing the image data to send. - * @return Zero on success, non-zero on error. + * @return GUAC_STATUS_SUCCESS on success, any other status code on error. */ -int guac_send_cursor(GUACIO* io, int x, int y, cairo_surface_t* surface); +guac_status guac_send_cursor(GUACIO* io, int x, int y, cairo_surface_t* surface); /** * Returns whether new instruction data is available on the given GUACIO * connection for parsing. * * @param io The GUACIO connection to use. - * @return A positive value if data is available, negative on error, or - * zero if no data is currently available. + * @return GUAC_STATUS_SUCCESS if data is available, + * GUAC_STATUS_NO_INPUT if no data is currently available, + * or any other status code on error. */ -int guac_instructions_waiting(GUACIO* io); +guac_status guac_instructions_waiting(GUACIO* io); /** * Reads a single instruction from the given GUACIO connection. @@ -327,13 +328,13 @@ int guac_instructions_waiting(GUACIO* io); * @param parsed_instruction A pointer to a guac_instruction structure which * will be populated with data read from the given * GUACIO connection. - * @return A positive value if data was successfully read, negative on - * error, or zero if the instruction could not be read completely - * because GUAC_TIMEOUT elapsed, in which case subsequent calls to - * guac_read_instruction() will return the parsed instruction once - * enough data is available. + * @return GUAC_STATUS_SUCCESS if data was successfully read, + * GUAC_STATUS_INPUT_UNAVAILABLE if the instruction could not be read + * completely because GUAC_TIMEOUT elapsed, in which case subsequent + * calls to guac_read_instruction() will return the parsed instruction + * once enough data is available, or any other status code on error. */ -int guac_read_instruction(GUACIO* io, guac_instruction* parsed_instruction); +guac_status guac_read_instruction(GUACIO* io, guac_instruction* parsed_instruction); guac_timestamp guac_current_timestamp(); void guac_sleep(int millis); diff --git a/libguac/src/client.c b/libguac/src/client.c index a1cafa18..ba5a2d4d 100644 --- a/libguac/src/client.c +++ b/libguac/src/client.c @@ -161,26 +161,22 @@ guac_client* guac_get_client(int client_fd) { /* Wait for data until timeout */ result = guac_instructions_waiting(io); - if (result == 0) { + if (result == GUAC_STATUS_TIMEOUT) { guac_send_error(io, "Select timeout."); guac_close(io); return NULL; } /* If error occurs while waiting, exit with failure */ - if (result < 0) { + if (result != GUAC_STATUS_SUCCESS) { guac_close(io); return NULL; } result = guac_read_instruction(io, &instruction); - if (result < 0) { - guac_close(io); - return NULL; - } /* Select instruction read */ - if (result > 0) { + if (result == GUAC_STATUS_SUCCESS) { if (strcmp(instruction.opcode, "select") == 0) { @@ -247,6 +243,12 @@ guac_client* guac_get_client(int client_fd) { guac_free_instruction_data(&instruction); } + /* Give up on any legitimate error */ + else if (result != GUAC_STATUS_INPUT_UNAVAILABLE) { + guac_close(io); + return NULL; + } + } /* Wait for connect instruction */ diff --git a/libguac/src/error.c b/libguac/src/error.c index 6b66b592..310a7377 100644 --- a/libguac/src/error.c +++ b/libguac/src/error.c @@ -40,14 +40,15 @@ /* Error strings */ -const char* __GUAC_STATUS_SUCCESS_STR = "Success"; -const char* __GUAC_STATUS_NO_MEMORY_STR = "Insufficient memory"; -const char* __GUAC_STATUS_NO_INPUT_STR = "End of input stream"; -const char* __GUAC_STATUS_INPUT_TIMEOUT_STR = "Read timeout"; -const char* __GUAC_STATUS_OUTPUT_ERROR_STR = "Output error"; -const char* __GUAC_STATUS_BAD_ARGUMENT_STR = "Invalid argument"; -const char* __GUAC_STATUS_BAD_STATE_STR = "Illegal state"; -const char* __GUAC_STATUS_INVALID_STATUS_STR = "UNKNOWN STATUS CODE"; +const char* __GUAC_STATUS_SUCCESS_STR = "Success"; +const char* __GUAC_STATUS_NO_MEMORY_STR = "Insufficient memory"; +const char* __GUAC_STATUS_NO_INPUT_STR = "End of input stream"; +const char* __GUAC_STATUS_INPUT_UNAVAILABLE_STR = "Input unavailable"; +const char* __GUAC_STATUS_INPUT_TIMEOUT_STR = "Read timeout"; +const char* __GUAC_STATUS_OUTPUT_ERROR_STR = "Output error"; +const char* __GUAC_STATUS_BAD_ARGUMENT_STR = "Invalid argument"; +const char* __GUAC_STATUS_BAD_STATE_STR = "Illegal state"; +const char* __GUAC_STATUS_INVALID_STATUS_STR = "UNKNOWN STATUS CODE"; const char* guac_status_string(guac_status_t status) { @@ -66,6 +67,10 @@ const char* guac_status_string(guac_status_t status) { case GUAC_STATUS_NO_INPUT: return __GUAC_STATUS_NO_INPUT_STR; + /* Input unavailable */ + case GUAC_STATUS_INPUT_UNAVAILABLE: + return __GUAC_STATUS_INPUT_UNAVAILABLE_STR; + /* Input timeout */ case GUAC_STATUS_INPUT_TIMEOUT: return __GUAC_STATUS_INPUT_TIMEOUT_STR;