mirror of
				https://github.com/gyurix1968/guacamole-client.git
				synced 2025-10-31 17:13:21 +00:00 
			
		
		
		
	guac_message structure, initial parsing support
This commit is contained in:
		| @@ -4,7 +4,6 @@ | ||||
| #include <unistd.h> | ||||
| #include <fcntl.h> | ||||
| #include <stdio.h> | ||||
| #include <errno.h> | ||||
|  | ||||
| #include <sys/select.h> | ||||
|  | ||||
| @@ -226,75 +225,3 @@ int guac_select(GUACIO* io, int usec_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; | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user