Detect closed connections.

This commit is contained in:
Michael Jumper
2010-09-07 23:32:38 -07:00
parent 8f4d722a9c
commit 5bcea85d81
5 changed files with 41 additions and 35 deletions

View File

@@ -1,5 +1,5 @@
CFLAGS=-O2 -ansi -pedantic -Wall -Werror CFLAGS=-g -ansi -pedantic -Wall -Werror
LDFLAGS=-lpng -lvncclient LDFLAGS=-lpng -lvncclient
.PHONY: clean .PHONY: clean

View File

@@ -60,6 +60,7 @@ guac_client* guac_get_client(int client_fd, void (*client_init)(guac_client* cli
client->io = guac_open(client_fd); client->io = guac_open(client_fd);
client_init(client, hostname, port); client_init(client, hostname, port);
guac_flush(client->io);
return client; return client;
@@ -84,38 +85,48 @@ void guac_start_client(guac_client* client) {
for (;;) { for (;;) {
/* Handle server messages */ /* Handle server messages */
if (client->handle_messages) if (client->handle_messages) {
client->handle_messages(client); client->handle_messages(client);
guac_flush(client->io);
}
wait_result = guac_instructions_waiting(io); wait_result = guac_instructions_waiting(io);
if (wait_result > 0) { if (wait_result > 0) {
guac_instruction* instruction; guac_instruction* instruction;
while ((instruction = guac_read_instruction(io))) { if ((instruction = guac_read_instruction(io))) {
if (strcmp(instruction->opcode, "mouse") == 0) { do {
if (client->mouse_handler) if (strcmp(instruction->opcode, "mouse") == 0) {
client->mouse_handler( if (client->mouse_handler)
client, client->mouse_handler(
atoi(instruction->argv[0]), /* x */ client,
atoi(instruction->argv[1]), /* y */ atoi(instruction->argv[0]), /* x */
atoi(instruction->argv[2]) /* mask */ atoi(instruction->argv[1]), /* y */
); atoi(instruction->argv[2]) /* mask */
} );
}
else if (strcmp(instruction->opcode, "key") == 0) { else if (strcmp(instruction->opcode, "key") == 0) {
if (client->key_handler) if (client->key_handler)
client->key_handler( client->key_handler(
client, client,
atoi(instruction->argv[0]), /* keysym */ atoi(instruction->argv[0]), /* keysym */
atoi(instruction->argv[1]) /* pressed */ atoi(instruction->argv[1]) /* pressed */
); );
} }
guac_free_instruction(instruction);
} while ((instruction = guac_read_instruction(io)));
guac_free_instruction(instruction);
} }
else
return;
} }
else if (wait_result < 0)
return;
} }

View File

@@ -227,6 +227,9 @@ int guac_select(GUACIO* io, int usec_timeout) {
fd_set fds; fd_set fds;
struct timeval timeout; struct timeval timeout;
if (usec_timeout < 0)
return select(io->fd + 1, &fds, NULL, NULL, NULL);
timeout.tv_sec = usec_timeout/1000000; timeout.tv_sec = usec_timeout/1000000;
timeout.tv_usec = usec_timeout%1000000; timeout.tv_usec = usec_timeout%1000000;

View File

@@ -289,6 +289,7 @@ int __guac_fill_instructionbuf(GUACIO* io) {
} }
/* Returns new instruction if one exists, or NULL if no more instructions. */
guac_instruction* guac_read_instruction(GUACIO* io) { guac_instruction* guac_read_instruction(GUACIO* io) {
guac_instruction* parsed_instruction; guac_instruction* parsed_instruction;
@@ -355,18 +356,13 @@ guac_instruction* guac_read_instruction(GUACIO* io) {
} }
/* No instruction yet? Get more data ... */ /* No instruction yet? Get more data ... */
retval = guac_select(io, 1000); while ((retval = guac_select(io, 2500000)) == 0);
if (retval < 0) if (retval < 0)
return NULL; return NULL; /* If error, no more instructions */
/* Break if descriptor doesn't have enough data */
if (retval == 0)
return NULL; /* SOFT FAIL: No instruction ... yet, but is still in buffer */
retval = __guac_fill_instructionbuf(io); retval = __guac_fill_instructionbuf(io);
if (retval < 0 && errno != EAGAIN && errno != EWOULDBLOCK) if (retval <= 0)
return NULL; return NULL; /* If error, or no more data, no instructions remain */
} }
} }

View File

@@ -98,7 +98,6 @@ void guac_vnc_cursor(rfbClient* client, int x, int y, int w, int h, int bpp) {
/* SEND CURSOR */ /* SEND CURSOR */
guac_send_cursor(io, x, y, png_buffer, w, h); guac_send_cursor(io, x, y, png_buffer, w, h);
guac_flush(io);
} }
void guac_vnc_update(rfbClient* client, int x, int y, int w, int h) { void guac_vnc_update(rfbClient* client, int x, int y, int w, int h) {
@@ -150,7 +149,6 @@ void guac_vnc_update(rfbClient* client, int x, int y, int w, int h) {
} }
guac_send_png(io, x, y, png_buffer, w, h); guac_send_png(io, x, y, png_buffer, w, h);
guac_flush(io);
} }
@@ -159,7 +157,6 @@ void guac_vnc_copyrect(rfbClient* client, int src_x, int src_y, int w, int h, in
GUACIO* io = rfbClientGetClientData(client, __GUAC_VNC_TAG_IO); GUACIO* io = rfbClientGetClientData(client, __GUAC_VNC_TAG_IO);
guac_send_copy(io, src_x, src_y, w, h, dest_x, dest_y); guac_send_copy(io, src_x, src_y, w, h, dest_x, dest_y);
guac_flush(io);
} }
@@ -244,7 +241,7 @@ void vnc_guac_client_init(guac_client* client, const char* hostname, int port) {
/* Framebuffer update handler */ /* Framebuffer update handler */
rfb_client->GotFrameBufferUpdate = guac_vnc_update; rfb_client->GotFrameBufferUpdate = guac_vnc_update;
/*rfb_client->GotCopyRect = guac_vnc_copyrect;*/ rfb_client->GotCopyRect = guac_vnc_copyrect;
/* Enable client-side cursor */ /* Enable client-side cursor */
rfb_client->GotCursorShape = guac_vnc_cursor; rfb_client->GotCursorShape = guac_vnc_cursor;
@@ -288,7 +285,6 @@ void vnc_guac_client_init(guac_client* client, const char* hostname, int port) {
/* Send size */ /* Send size */
guac_send_size(client->io, rfb_client->width, rfb_client->height); guac_send_size(client->io, rfb_client->width, rfb_client->height);
guac_flush(client->io);
} }