mirror of
https://github.com/gyurix1968/guacamole-client.git
synced 2025-09-06 13:17:41 +00:00
Basic input message support.
This commit is contained in:
@@ -1,7 +1,12 @@
|
|||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <errno.h>
|
||||||
|
|
||||||
|
#include <sys/select.h>
|
||||||
|
|
||||||
#include "guacio.h"
|
#include "guacio.h"
|
||||||
|
|
||||||
@@ -14,11 +19,22 @@ char __GUACIO_BAS64_CHARACTERS[64] = {
|
|||||||
|
|
||||||
GUACIO* guac_open(int fd) {
|
GUACIO* guac_open(int fd) {
|
||||||
|
|
||||||
|
int flags;
|
||||||
|
|
||||||
GUACIO* io = malloc(sizeof(GUACIO));
|
GUACIO* io = malloc(sizeof(GUACIO));
|
||||||
io->ready = 0;
|
io->ready = 0;
|
||||||
io->written = 0;
|
io->written = 0;
|
||||||
io->fd = fd;
|
io->fd = fd;
|
||||||
|
|
||||||
|
/* Set O_NONBLOCK */
|
||||||
|
flags = fcntl(io->fd, F_GETFL, 0);
|
||||||
|
fcntl(io->fd, F_SETFL, flags | O_NONBLOCK);
|
||||||
|
|
||||||
|
/* Allocate message buffer */
|
||||||
|
io->messagebuf_size = 1024;
|
||||||
|
io->messagebuf = malloc(io->messagebuf_size);
|
||||||
|
io->messagebuf_used_length = 0;
|
||||||
|
|
||||||
return io;
|
return io;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -194,3 +210,91 @@ ssize_t guac_flush_base64(GUACIO* io) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int guac_select(GUACIO* io, int usec_timeout) {
|
||||||
|
|
||||||
|
fd_set fds;
|
||||||
|
struct timeval timeout;
|
||||||
|
|
||||||
|
timeout.tv_sec = usec_timeout/1000000;
|
||||||
|
timeout.tv_usec = usec_timeout%1000000;
|
||||||
|
|
||||||
|
FD_ZERO(&fds);
|
||||||
|
FD_SET(io->fd, &fds);
|
||||||
|
|
||||||
|
return select(io->fd + 1, &fds, NULL, NULL, &timeout);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int __guac_fill_messagebuf(GUACIO* io) {
|
||||||
|
|
||||||
|
int retval;
|
||||||
|
|
||||||
|
/* Attempt to fill buffer */
|
||||||
|
retval = read(
|
||||||
|
io->fd,
|
||||||
|
io->messagebuf + io->messagebuf_used_length,
|
||||||
|
io->messagebuf_size - io->messagebuf_used_length
|
||||||
|
);
|
||||||
|
|
||||||
|
if (retval < 0)
|
||||||
|
return retval;
|
||||||
|
|
||||||
|
io->messagebuf_used_length += retval;
|
||||||
|
|
||||||
|
/* Expand buffer if necessary */
|
||||||
|
if (io->messagebuf_used_length > io->messagebuf_size / 2) {
|
||||||
|
io->messagebuf_size *= 2;
|
||||||
|
io->messagebuf = realloc(io->messagebuf, io->messagebuf_size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return retval;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int guac_read_message(GUACIO* io) {
|
||||||
|
|
||||||
|
int retval;
|
||||||
|
int i = 0;
|
||||||
|
|
||||||
|
/* Loop until a message is read */
|
||||||
|
for (;;) {
|
||||||
|
|
||||||
|
/* Search for end of message */
|
||||||
|
for (; i < io->messagebuf_used_length; i++) {
|
||||||
|
|
||||||
|
if (io->messagebuf[i] == ';') {
|
||||||
|
|
||||||
|
char* message = malloc(i+1);
|
||||||
|
memcpy(message, io->messagebuf, i+1);
|
||||||
|
message[i] = '\0'; /* Replace semicolon with null terminator. */
|
||||||
|
|
||||||
|
fprintf(stderr, "RECEIVED MESSAGE: %s\n", message);
|
||||||
|
|
||||||
|
/* Found. Reset buffer */
|
||||||
|
memmove(io->messagebuf, io->messagebuf + i + 1, io->messagebuf_used_length - i - 1);
|
||||||
|
io->messagebuf_used_length -= i + 1;
|
||||||
|
|
||||||
|
/* Done */
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/* No message yet? Get more data ... */
|
||||||
|
retval = guac_select(io, 1000);
|
||||||
|
if (retval < 0)
|
||||||
|
return retval;
|
||||||
|
|
||||||
|
/* Break if descriptor doesn't have enough data */
|
||||||
|
if (retval == 0)
|
||||||
|
return 0; /* SOFT FAIL: No message ... yet, but is still in buffer */
|
||||||
|
|
||||||
|
retval = __guac_fill_messagebuf(io);
|
||||||
|
if (retval < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
|
||||||
|
return retval;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@@ -14,6 +14,10 @@ typedef struct GUACIO {
|
|||||||
int written;
|
int written;
|
||||||
char out_buf[8192];
|
char out_buf[8192];
|
||||||
|
|
||||||
|
int messagebuf_size;
|
||||||
|
int messagebuf_used_length;
|
||||||
|
char* messagebuf;
|
||||||
|
|
||||||
} GUACIO;
|
} GUACIO;
|
||||||
|
|
||||||
GUACIO* guac_open(int fd);
|
GUACIO* guac_open(int fd);
|
||||||
@@ -22,6 +26,8 @@ ssize_t guac_write_string(GUACIO* io, const char* str);
|
|||||||
ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count);
|
ssize_t guac_write_base64(GUACIO* io, const void* buf, size_t count);
|
||||||
ssize_t guac_flush_base64(GUACIO* io);
|
ssize_t guac_flush_base64(GUACIO* io);
|
||||||
ssize_t guac_flush(GUACIO* io);
|
ssize_t guac_flush(GUACIO* io);
|
||||||
|
int guac_select(GUACIO* io, int usec_timeout);
|
||||||
|
int guac_read_message(GUACIO* io);
|
||||||
void guac_close(GUACIO* io);
|
void guac_close(GUACIO* io);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@@ -162,6 +162,8 @@ void proxy(int client_fd) {
|
|||||||
/* VNC Client Loop */
|
/* VNC Client Loop */
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
||||||
|
/* TODO: Separate into THREADS ... no need to do this weird polling thing */
|
||||||
|
|
||||||
wait_result = WaitForMessage(rfb_client, 2000);
|
wait_result = WaitForMessage(rfb_client, 2000);
|
||||||
if (wait_result < 0) {
|
if (wait_result < 0) {
|
||||||
fprintf(stderr, "WAIT FAIL\n");
|
fprintf(stderr, "WAIT FAIL\n");
|
||||||
@@ -176,6 +178,12 @@ void proxy(int client_fd) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
wait_result = guac_select(io, 2000);
|
||||||
|
if (wait_result > 0) {
|
||||||
|
guac_read_message(io);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Free PNG data */
|
/* Free PNG data */
|
||||||
|
Reference in New Issue
Block a user