Add support for keep-alive thread.
This commit is contained in:
parent
4ebafa0482
commit
eac99e0ce1
@ -42,6 +42,8 @@
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "timestamp.h"
|
||||
|
||||
/**
|
||||
* Defines the guac_socket object and functionss for using and manipulating it.
|
||||
*
|
||||
@ -53,6 +55,12 @@
|
||||
*/
|
||||
#define GUAC_SOCKET_OUTPUT_BUFFER_SIZE 8192
|
||||
|
||||
/**
|
||||
* The number of milliseconds to wait between keep-alive pings on a socket
|
||||
* with keep-alive enabled.
|
||||
*/
|
||||
#define GUAC_SOCKET_KEEP_ALIVE_INTERVAL 5000
|
||||
|
||||
typedef struct guac_socket guac_socket;
|
||||
|
||||
/**
|
||||
@ -104,6 +112,23 @@ typedef int guac_socket_select_handler(guac_socket* socket, int usec_timeout);
|
||||
*/
|
||||
typedef int guac_socket_free_handler(guac_socket* socket);
|
||||
|
||||
/**
|
||||
* Possible current states of a guac_socket.
|
||||
*/
|
||||
typedef enum guac_socket_state {
|
||||
|
||||
/**
|
||||
* The socket is open and can be written to / read from.
|
||||
*/
|
||||
GUAC_SOCKET_OPEN,
|
||||
|
||||
/**
|
||||
* The socket is closed. Reads and writes will fail.
|
||||
*/
|
||||
GUAC_SOCKET_CLOSED
|
||||
|
||||
} guac_socket_state;
|
||||
|
||||
/**
|
||||
* The core I/O object of Guacamole. guac_socket provides buffered input and
|
||||
* output as well as convenience methods for efficiently writing base64 data.
|
||||
@ -138,6 +163,17 @@ struct guac_socket {
|
||||
*/
|
||||
guac_socket_free_handler* free_handler;
|
||||
|
||||
/**
|
||||
* The current state of this guac_socket.
|
||||
*/
|
||||
guac_socket_state state;
|
||||
|
||||
/**
|
||||
* The timestamp associated with the time the last block of data was
|
||||
* written to this guac_socket.
|
||||
*/
|
||||
guac_timestamp last_write_timestamp;
|
||||
|
||||
/**
|
||||
* The number of bytes present in the base64 "ready" buffer.
|
||||
*/
|
||||
@ -195,6 +231,16 @@ struct guac_socket {
|
||||
*/
|
||||
pthread_mutex_t __buffer_lock;
|
||||
|
||||
/**
|
||||
* Whether automatic keep-alive is enabled.
|
||||
*/
|
||||
int __keep_alive_enabled;
|
||||
|
||||
/**
|
||||
* The keep-alive thread.
|
||||
*/
|
||||
pthread_t __keep_alive_thread;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
@ -224,6 +270,17 @@ void guac_socket_free(guac_socket* socket);
|
||||
*/
|
||||
void guac_socket_require_threadsafe(guac_socket* socket);
|
||||
|
||||
/**
|
||||
* Declares that the given socket must automatically send a keep-alive ping
|
||||
* to ensure neither side of the socket times out while the socket is open.
|
||||
* This ping will take the form of a "nop" instruction. Enabling keep-alive
|
||||
* automatically enables threadsafety.
|
||||
*
|
||||
* @param socket The guac_socket to declare as requiring an automatic
|
||||
* keep-alive ping.
|
||||
*/
|
||||
void guac_socket_require_keep_alive(guac_socket* socket);
|
||||
|
||||
/**
|
||||
* Marks the beginning of a Guacamole protocol instruction. If threadsafety
|
||||
* is enabled on the socket, other instructions will be blocked from sending
|
||||
|
@ -55,6 +55,8 @@
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "socket.h"
|
||||
#include "protocol.h"
|
||||
#include "timestamp.h"
|
||||
#include "error.h"
|
||||
|
||||
char __guac_socket_BASE64_CHARACTERS[64] = {
|
||||
@ -65,9 +67,44 @@ char __guac_socket_BASE64_CHARACTERS[64] = {
|
||||
'8', '9', '+', '/'
|
||||
};
|
||||
|
||||
static void* __guac_socket_keep_alive_thread(void* data) {
|
||||
|
||||
/* Calculate sleep interval */
|
||||
struct timespec interval;
|
||||
interval.tv_sec = GUAC_SOCKET_KEEP_ALIVE_INTERVAL / 1000;
|
||||
interval.tv_nsec = (GUAC_SOCKET_KEEP_ALIVE_INTERVAL % 1000) * 1000000L;
|
||||
|
||||
/* Socket keep-alive loop */
|
||||
guac_socket* socket = (guac_socket*) data;
|
||||
while (socket->state == GUAC_SOCKET_OPEN) {
|
||||
|
||||
/* Send NOP keep-alive if it's been a while since the last output */
|
||||
guac_timestamp timestamp = guac_timestamp_current();
|
||||
if (timestamp - socket->last_write_timestamp >
|
||||
GUAC_SOCKET_KEEP_ALIVE_INTERVAL) {
|
||||
|
||||
/* Send NOP */
|
||||
if (guac_protocol_send_nop(socket)
|
||||
|| guac_socket_flush(socket))
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
/* Sleep until next keep-alive check */
|
||||
nanosleep(&interval, NULL);
|
||||
|
||||
}
|
||||
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
static ssize_t __guac_socket_write(guac_socket* socket,
|
||||
const void* buf, size_t count) {
|
||||
|
||||
/* Update timestamp of last write */
|
||||
socket->last_write_timestamp = guac_timestamp_current();
|
||||
|
||||
/* If handler defined, call it. */
|
||||
if (socket->write_handler)
|
||||
return socket->write_handler(socket, buf, count);
|
||||
@ -138,6 +175,7 @@ guac_socket* guac_socket_alloc() {
|
||||
socket->__ready = 0;
|
||||
socket->__written = 0;
|
||||
socket->data = NULL;
|
||||
socket->state = GUAC_SOCKET_OPEN;
|
||||
|
||||
/* Init members */
|
||||
socket->__instructionbuf_unparsed_start = socket->__instructionbuf;
|
||||
@ -166,6 +204,18 @@ void guac_socket_require_threadsafe(guac_socket* socket) {
|
||||
socket->__threadsafe_instructions = 1;
|
||||
}
|
||||
|
||||
void guac_socket_require_keep_alive(guac_socket* socket) {
|
||||
|
||||
/* Keep-alive thread requires a threadsafe socket */
|
||||
guac_socket_require_threadsafe(socket);
|
||||
|
||||
/* Start keep-alive thread */
|
||||
socket->__keep_alive_enabled = 1;
|
||||
pthread_create(&(socket->__keep_alive_thread), NULL,
|
||||
__guac_socket_keep_alive_thread, (void*) socket);
|
||||
|
||||
}
|
||||
|
||||
void guac_socket_instruction_begin(guac_socket* socket) {
|
||||
|
||||
/* Lock writes if threadsafety enabled */
|
||||
@ -205,6 +255,14 @@ void guac_socket_free(guac_socket* socket) {
|
||||
socket->free_handler(socket);
|
||||
|
||||
guac_socket_flush(socket);
|
||||
|
||||
/* Mark as closed */
|
||||
socket->state = GUAC_SOCKET_CLOSED;
|
||||
|
||||
/* Wait for keep-alive, if enabled */
|
||||
if (socket->__keep_alive_enabled)
|
||||
pthread_join(socket->__keep_alive_thread, NULL);
|
||||
|
||||
pthread_mutex_destroy(&(socket->__instruction_write_lock));
|
||||
free(socket);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user