Ensure complete writes, define buffer size.

This commit is contained in:
Michael Jumper 2013-06-29 10:27:52 -07:00
parent a08180acfe
commit 59b058be3d
2 changed files with 19 additions and 21 deletions

View File

@ -48,6 +48,11 @@
* @file socket.h * @file socket.h
*/ */
/**
* The number of bytes to buffer within each socket before flushing.
*/
#define GUAC_SOCKET_OUTPUT_BUFFER_SIZE 8192
typedef struct guac_socket guac_socket; typedef struct guac_socket guac_socket;
/** /**
@ -153,7 +158,7 @@ struct guac_socket {
* The main write buffer. Bytes written go here before being flushed * The main write buffer. Bytes written go here before being flushed
* to the open file descriptor. * to the open file descriptor.
*/ */
char __out_buf[8192]; char __out_buf[GUAC_SOCKET_OUTPUT_BUFFER_SIZE];
/** /**
* The current location of parsing within the instruction buffer. * The current location of parsing within the instruction buffer.

View File

@ -214,19 +214,17 @@ ssize_t guac_socket_write_string(guac_socket* socket, const char* str) {
char* __out_buf = socket->__out_buf; char* __out_buf = socket->__out_buf;
int retval;
for (; *str != '\0'; str++) { for (; *str != '\0'; str++) {
__out_buf[socket->__written++] = *str; __out_buf[socket->__written++] = *str;
/* Flush when necessary, return on error */ /* Flush when necessary, return on error. Note that we must flush within 4 bytes of boundary because
if (socket->__written > 8188 /* sizeof(__out_buf) - 4 */) { * __guac_socket_write_base64_triplet ALWAYS writes four bytes, and would otherwise potentially overflow
* the buffer. */
if (socket->__written >= GUAC_SOCKET_OUTPUT_BUFFER_SIZE - 4) {
retval = __guac_socket_write(socket, __out_buf, socket->__written); if (guac_socket_write(socket, __out_buf, socket->__written))
return 1;
if (retval < 0)
return retval;
socket->__written = 0; socket->__written = 0;
@ -242,8 +240,6 @@ ssize_t __guac_socket_write_base64_triplet(guac_socket* socket, int a, int b, in
char* __out_buf = socket->__out_buf; char* __out_buf = socket->__out_buf;
int retval;
/* Byte 1 */ /* Byte 1 */
__out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[(a & 0xFC) >> 2]; /* [AAAAAA]AABBBB BBBBCC CCCCCC */ __out_buf[socket->__written++] = __guac_socket_BASE64_CHARACTERS[(a & 0xFC) >> 2]; /* [AAAAAA]AABBBB BBBBCC CCCCCC */
@ -268,10 +264,10 @@ ssize_t __guac_socket_write_base64_triplet(guac_socket* socket, int a, int b, in
/* At this point, 4 bytes have been socket->__written */ /* At this point, 4 bytes have been socket->__written */
/* Flush when necessary, return on error */ /* Flush when necessary, return on error */
if (socket->__written > 8188 /* sizeof(__out_buf) - 4 */) { if (socket->__written >= GUAC_SOCKET_OUTPUT_BUFFER_SIZE - 4) {
retval = __guac_socket_write(socket, __out_buf, socket->__written);
if (retval < 0) if (guac_socket_write(socket, __out_buf, socket->__written))
return retval; return -1;
socket->__written = 0; socket->__written = 0;
} }
@ -327,14 +323,11 @@ ssize_t guac_socket_write_base64(guac_socket* socket, const void* buf, size_t co
ssize_t guac_socket_flush(guac_socket* socket) { ssize_t guac_socket_flush(guac_socket* socket) {
int retval;
/* Flush remaining bytes in buffer */ /* Flush remaining bytes in buffer */
if (socket->__written > 0) { if (socket->__written > 0) {
retval = __guac_socket_write(socket,
socket->__out_buf, socket->__written); if (guac_socket_write(socket, socket->__out_buf, socket->__written))
if (retval < 0) return 1;
return retval;
socket->__written = 0; socket->__written = 0;
} }