Refactor of prototypes, partial continuation of refactor into client.

This commit is contained in:
Michael Jumper 2011-11-19 14:34:08 -08:00
parent 4d6218560f
commit 8ef7d724bb
7 changed files with 83 additions and 64 deletions

View File

@ -40,6 +40,7 @@
#include "client.h" #include "client.h"
#include "protocol.h" #include "protocol.h"
#include "error.h"
/** /**
* Provides initial handler functions and a lookup structure for automatically * Provides initial handler functions and a lookup structure for automatically
@ -52,7 +53,7 @@
/** /**
* Internal handler for Guacamole instructions. * 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. * 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 * is received, this handler will be called. Sync instructions are automatically
* handled, thus there is no client handler for sync instruction. * 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 * Internal initial handler for the mouse instruction. When a mouse instruction
* is received, this handler will be called. The client's mouse handler will * is received, this handler will be called. The client's mouse handler will
* be invoked if defined. * 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 * Internal initial handler for the key instruction. When a key instruction
* is received, this handler will be called. The client's key handler will * is received, this handler will be called. The client's key handler will
* be invoked if defined. * 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 * Internal initial handler for the clipboard instruction. When a clipboard instruction
* is received, this handler will be called. The client's clipboard handler will * is received, this handler will be called. The client's clipboard handler will
* be invoked if defined. * 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 * Internal initial handler for the disconnect instruction. When a disconnect instruction
* is received, this handler will be called. Disconnect instructions are automatically * is received, this handler will be called. Disconnect instructions are automatically
* handled, thus there is no client handler for disconnect instruction. * 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 * Instruction handler mapping table. This is a NULL-terminated array of

View File

@ -312,9 +312,10 @@ guac_client* guac_get_client(int client_fd);
* Enter the main network message handling loop for the given client. * Enter the main network message handling loop for the given client.
* *
* @param client The proxy client to start handling messages of/for. * @param client The proxy client to start handling messages of/for.
* @return Zero if the client successfully started, non-zero if an error * @return GUAC_STATUS_SUCCESS if the client successfully started, or any
* occurs during startup. Note that this function will still return * other status code if an error occurs during startup. Note that
* zero if an error occurs while the client is running. * 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); 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. * @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 * 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 client The proxy client whose handlers should be called.
* @param instruction The instruction to pass to the proxy client via the * @param instruction The instruction to pass to the proxy client via the
* appropriate handler. * 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); 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. * @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 * 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 client The proxy client to return the buffer to.
* @param layer The buffer to return to the pool of available buffers. * @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. * The default Guacamole client layer, layer 0.

View File

@ -67,6 +67,12 @@ typedef enum guac_status {
*/ */
GUAC_STATUS_NO_INPUT, 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 * A timeout occurred while reading from the input stream associated
* with the operation. * with the operation.

View File

@ -151,7 +151,7 @@ int64_t guac_parse_int(const char* str);
* @param i The unsigned int to write. * @param i The unsigned int to write.
* @return Zero on success, or non-zero if an error occurs while writing. * @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 * 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. * @param str The string to write.
* @return Zero on success, or non-zero if an error occurs while writing. * @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 * 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. * @param count The number of bytes to write.
* @return Zero on success, or non-zero if an error occurs while writing. * @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. * 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 * @param io The GUACIO object to flush
* @return Zero on success, or non-zero if an error occurs during 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. * Flushes the write buffer.
@ -195,7 +195,7 @@ ssize_t guac_flush_base64(GUACIO* io);
* @param io The GUACIO object to flush * @param io The GUACIO object to flush
* @return Zero on success, or non-zero if an error occurs during 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 * @return Positive on success, zero if the timeout elapsed and no data is
* available, negative on error. * 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 * Frees resources allocated to the given GUACIO object. Note that this

View File

@ -176,18 +176,18 @@ void guac_free_instruction(guac_instruction* instruction);
* *
* @param io The GUACIO connection to use. * @param io The GUACIO connection to use.
* @param args The NULL-terminated array of argument names (strings). * @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. * Sends a name instruction over the given GUACIO connection.
* *
* @param io The GUACIO connection to use. * @param io The GUACIO connection to use.
* @param name The name to send within the name instruction. * @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 * 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 io The GUACIO connection to use.
* @param timestamp The current timestamp (in milliseconds). * @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. * Sends an error instruction over the given GUACIO connection.
* *
* @param io The GUACIO connection to use. * @param io The GUACIO connection to use.
* @param error The description associated with the error. * @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. * Sends a clipboard instruction over the given GUACIO connection.
* *
* @param io The GUACIO connection to use. * @param io The GUACIO connection to use.
* @param data The clipboard data to send. * @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. * 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 io The GUACIO connection to use.
* @param w The width of the display. * @param w The width of the display.
* @param h The height 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. * 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. * should be copied.
* @param dsty The Y coordinate of the destination, where the source rectangle * @param dsty The Y coordinate of the destination, where the source rectangle
* should be copied. * 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, const guac_layer* srcl, int srcx, int srcy, int w, int h,
guac_composite_mode mode, const guac_layer* dstl, int dstx, int dsty); 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 g The green component of the color of the rectangle.
* @param b The blue 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. * @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, guac_composite_mode mode, const guac_layer* layer,
int x, int y, int width, int height, int x, int y, int width, int height,
int r, int g, int b, int a); 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 y The Y coordinate of the clipping rectangle.
* @param width The width of the clipping rectangle. * @param width The width of the clipping rectangle.
* @param height The height 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); 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 x The destination X coordinate.
* @param y The destination Y coordinate. * @param y The destination Y coordinate.
* @param surface A cairo surface containing the image data to send. * @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); 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 x The X coordinate of the cursor hotspot.
* @param y The Y 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. * @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 * Returns whether new instruction data is available on the given GUACIO
* connection for parsing. * connection for parsing.
* *
* @param io The GUACIO connection to use. * @param io The GUACIO connection to use.
* @return A positive value if data is available, negative on error, or * @return GUAC_STATUS_SUCCESS if data is available,
* zero if no data is currently 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. * 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 * @param parsed_instruction A pointer to a guac_instruction structure which
* will be populated with data read from the given * will be populated with data read from the given
* GUACIO connection. * GUACIO connection.
* @return A positive value if data was successfully read, negative on * @return GUAC_STATUS_SUCCESS if data was successfully read,
* error, or zero if the instruction could not be read completely * GUAC_STATUS_INPUT_UNAVAILABLE if the instruction could not be read
* because GUAC_TIMEOUT elapsed, in which case subsequent calls to * completely because GUAC_TIMEOUT elapsed, in which case subsequent
* guac_read_instruction() will return the parsed instruction once * calls to guac_read_instruction() will return the parsed instruction
* enough data is available. * 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(); guac_timestamp guac_current_timestamp();
void guac_sleep(int millis); void guac_sleep(int millis);

View File

@ -161,26 +161,22 @@ guac_client* guac_get_client(int client_fd) {
/* Wait for data until timeout */ /* Wait for data until timeout */
result = guac_instructions_waiting(io); result = guac_instructions_waiting(io);
if (result == 0) { if (result == GUAC_STATUS_TIMEOUT) {
guac_send_error(io, "Select timeout."); guac_send_error(io, "Select timeout.");
guac_close(io); guac_close(io);
return NULL; return NULL;
} }
/* If error occurs while waiting, exit with failure */ /* If error occurs while waiting, exit with failure */
if (result < 0) { if (result != GUAC_STATUS_SUCCESS) {
guac_close(io); guac_close(io);
return NULL; return NULL;
} }
result = guac_read_instruction(io, &instruction); result = guac_read_instruction(io, &instruction);
if (result < 0) {
guac_close(io);
return NULL;
}
/* Select instruction read */ /* Select instruction read */
if (result > 0) { if (result == GUAC_STATUS_SUCCESS) {
if (strcmp(instruction.opcode, "select") == 0) { if (strcmp(instruction.opcode, "select") == 0) {
@ -247,6 +243,12 @@ guac_client* guac_get_client(int client_fd) {
guac_free_instruction_data(&instruction); 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 */ /* Wait for connect instruction */

View File

@ -40,14 +40,15 @@
/* Error strings */ /* Error strings */
const char* __GUAC_STATUS_SUCCESS_STR = "Success"; const char* __GUAC_STATUS_SUCCESS_STR = "Success";
const char* __GUAC_STATUS_NO_MEMORY_STR = "Insufficient memory"; const char* __GUAC_STATUS_NO_MEMORY_STR = "Insufficient memory";
const char* __GUAC_STATUS_NO_INPUT_STR = "End of input stream"; const char* __GUAC_STATUS_NO_INPUT_STR = "End of input stream";
const char* __GUAC_STATUS_INPUT_TIMEOUT_STR = "Read timeout"; const char* __GUAC_STATUS_INPUT_UNAVAILABLE_STR = "Input unavailable";
const char* __GUAC_STATUS_OUTPUT_ERROR_STR = "Output error"; const char* __GUAC_STATUS_INPUT_TIMEOUT_STR = "Read timeout";
const char* __GUAC_STATUS_BAD_ARGUMENT_STR = "Invalid argument"; const char* __GUAC_STATUS_OUTPUT_ERROR_STR = "Output error";
const char* __GUAC_STATUS_BAD_STATE_STR = "Illegal state"; const char* __GUAC_STATUS_BAD_ARGUMENT_STR = "Invalid argument";
const char* __GUAC_STATUS_INVALID_STATUS_STR = "UNKNOWN STATUS CODE"; 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) { 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: case GUAC_STATUS_NO_INPUT:
return __GUAC_STATUS_NO_INPUT_STR; return __GUAC_STATUS_NO_INPUT_STR;
/* Input unavailable */
case GUAC_STATUS_INPUT_UNAVAILABLE:
return __GUAC_STATUS_INPUT_UNAVAILABLE_STR;
/* Input timeout */ /* Input timeout */
case GUAC_STATUS_INPUT_TIMEOUT: case GUAC_STATUS_INPUT_TIMEOUT:
return __GUAC_STATUS_INPUT_TIMEOUT_STR; return __GUAC_STATUS_INPUT_TIMEOUT_STR;