From 014fc579ca9a18a64a6124047942baa8398788f1 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Fri, 19 Oct 2012 14:06:39 -0700 Subject: [PATCH] Add nested protocol socket. --- libguac/src/Makefile.am | 1 + libguac/src/socket-nest.c | 152 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 libguac/src/socket-nest.c diff --git a/libguac/src/Makefile.am b/libguac/src/Makefile.am index ff97dedb..340ab4c7 100644 --- a/libguac/src/Makefile.am +++ b/libguac/src/Makefile.am @@ -69,6 +69,7 @@ libguac_la_SOURCES = \ protocol.c \ socket.c \ socket-fd.c \ + socket-nest.c \ timestamp.c \ unicode.c diff --git a/libguac/src/socket-nest.c b/libguac/src/socket-nest.c new file mode 100644 index 00000000..2f06200d --- /dev/null +++ b/libguac/src/socket-nest.c @@ -0,0 +1,152 @@ + +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is libguac. + * + * The Initial Developer of the Original Code is + * Michael Jumper. + * Portions created by the Initial Developer are Copyright (C) 2010 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * David PHAM-VAN Ulteo SAS - http://www.ulteo.com + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __MINGW32__ +#include +#else +#include +#endif + +#include +#include + +#include "socket.h" +#include "protocol.h" +#include "error.h" +#include "unicode.h" + +#define GUAC_SOCKET_NEST_BUFFER_SIZE 8192 + +typedef struct guac_socket_nest_data { + + guac_socket* parent; + char buffer[GUAC_SOCKET_NEST_BUFFER_SIZE]; + int index; + +} guac_socket_nest_data; + +ssize_t __guac_socket_nest_write_handler(guac_socket* socket, + void* buf, size_t count) { + + guac_socket_nest_data* data = (guac_socket_nest_data*) socket->data; + unsigned char* source = (unsigned char*) buf; + + /* Current location in destination buffer during copy */ + char* current = data->buffer; + + /* Number of bytes remaining in source buffer */ + int remaining = count; + + /* If we can't actually store that many bytes, reduce number of bytes + * expected to be written */ + if (remaining > GUAC_SOCKET_NEST_BUFFER_SIZE) + remaining = GUAC_SOCKET_NEST_BUFFER_SIZE; + + /* Current offset within destination buffer */ + int offset; + + /* Number of characters before start of next character */ + int skip = 0; + + /* Copy UTF-8 characters into buffer */ + for (offset = 0; offset < GUAC_SOCKET_NEST_BUFFER_SIZE; offset++) { + + /* Get next byte */ + unsigned char c = *source; + remaining--; + + /* If skipping, then skip */ + if (skip > 0) skip--; + + /* Otherwise, determine next skip value, and increment length */ + else { + + /* Determine skip value (size in bytes of rest of character) */ + skip = guac_utf8_charsize(c) - 1; + + /* If not enough bytes to complete character, break */ + if (skip > remaining) + break; + + } + + /* Store byte */ + *current = c; + + /* Advance to next character */ + source++; + current++; + + } + + /* Append null-terminator */ + *current = 0; + + /* Send nest instruction containing read UTF-8 segment */ + guac_protocol_send_nest(data->parent, data->index, data->buffer); + + /* Return number of bytes actually written */ + return offset; + +} + +guac_socket* guac_socket_nest(guac_socket* parent, int index) { + + /* Allocate socket and associated data */ + guac_socket* socket = guac_socket_alloc(); + guac_socket_nest_data* data = malloc(sizeof(guac_socket_nest_data)); + + /* Store file descriptor as socket data */ + data->parent = parent; + socket->data = data; + + /* Set write handler */ + socket->write_handler = __guac_socket_nest_write_handler; + + return socket; + +} +