Removed transfer limit, improved cross-platform logging, added check for png_structp.

This commit is contained in:
Michael Jumper 2011-02-08 18:23:10 -08:00
parent fd1485e220
commit 9c7c09cd7f
4 changed files with 19 additions and 53 deletions

View File

@ -79,13 +79,6 @@ typedef struct GUACIO {
*/ */
char* instructionbuf; char* instructionbuf;
/**
* The transfer limit, in kilobytes per second. If 0, there is no
* transfer limit. If non-zero, sleep calls are used at the end of
* a write to ensure output never exceeds the specified limit.
*/
unsigned int transfer_limit; /* KB/sec */
} GUACIO; } GUACIO;
/** /**

View File

@ -20,16 +20,16 @@
#ifndef _GUACLOG_H #ifndef _GUACLOG_H
#define _GUACLOG_H #define _GUACLOG_H
#ifndef __MINGW32__ #ifdef HAVE_SYSLOG_H
// Logging for UNIX /* Logging for UNIX */
#include <syslog.h> #include <syslog.h>
#define GUAC_LOG_ERROR(...) syslog(LOG_ERR, __VA_ARGS__) #define GUAC_LOG_ERROR(...) syslog(LOG_ERR, __VA_ARGS__)
#define GUAC_LOG_INFO(...) syslog(LOG_INFO, __VA_ARGS__) #define GUAC_LOG_INFO(...) syslog(LOG_INFO, __VA_ARGS__)
#else #else
// Logging for W32 /* Logging for W32 */
#define GUAC_LOG_ERROR(...) fprintf(stderr, __VA_ARGS__) #define GUAC_LOG_ERROR(...) fprintf(stderr, __VA_ARGS__)
#define GUAC_LOG_INFO(...) fprintf(stderr, __VA_ARGS__) #define GUAC_LOG_INFO(...) fprintf(stderr, __VA_ARGS__)

View File

@ -23,8 +23,13 @@
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <time.h> #ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/select.h> #include <sys/select.h>
#endif
#include <time.h>
#include <sys/time.h> #include <sys/time.h>
#include "guacio.h" #include "guacio.h"
@ -48,9 +53,6 @@ GUACIO* guac_open(int fd) {
io->instructionbuf = malloc(io->instructionbuf_size); io->instructionbuf = malloc(io->instructionbuf_size);
io->instructionbuf_used_length = 0; io->instructionbuf_used_length = 0;
/* Set limit */
io->transfer_limit = 0;
return io; return io;
} }
@ -64,41 +66,10 @@ void guac_close(GUACIO* io) {
/* Write bytes, limit rate */ /* Write bytes, limit rate */
ssize_t __guac_write(GUACIO* io, const char* buf, int count) { ssize_t __guac_write(GUACIO* io, const char* buf, int count) {
struct timeval start, end;
int retval; int retval;
/* Write and time how long the write takes (microseconds) */ /* Write and time how long the write takes (microseconds) */
gettimeofday(&start, NULL);
retval = write(io->fd, buf, count); retval = write(io->fd, buf, count);
gettimeofday(&end, NULL);
if (retval < 0)
return retval;
if (io->transfer_limit > 0) {
suseconds_t elapsed;
suseconds_t required_usecs;
/* Get elapsed time */
elapsed = (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec;
/* Calculate how much time we must sleep */
required_usecs = retval * 1000 / io->transfer_limit - elapsed; /* useconds at transfer_limit KB/s*/
/* Sleep as necessary */
if (required_usecs > 0) {
struct timespec required_sleep;
required_sleep.tv_sec = required_usecs / 1000000;
required_sleep.tv_nsec = (required_usecs % 1000000) * 1000;
nanosleep(&required_sleep, NULL);
}
}
return retval; return retval;
} }
@ -136,19 +107,11 @@ ssize_t guac_write_string(GUACIO* io, const char* str) {
/* Flush when necessary, return on error */ /* Flush when necessary, return on error */
if (io->written > 8188 /* sizeof(out_buf) - 4 */) { if (io->written > 8188 /* sizeof(out_buf) - 4 */) {
struct timeval start, end;
suseconds_t elapsed;
gettimeofday(&start, NULL);
retval = __guac_write(io, out_buf, io->written); retval = __guac_write(io, out_buf, io->written);
gettimeofday(&end, NULL);
if (retval < 0) if (retval < 0)
return retval; return retval;
/* Get elapsed time */
elapsed = (end.tv_sec - start.tv_sec) * 1000000 + end.tv_usec - start.tv_usec;
io->written = 0; io->written = 0;
} }

View File

@ -23,8 +23,18 @@
#include <errno.h> #include <errno.h>
#include <png.h> #include <png.h>
/* If png_structp not defined in png.h, try to include pngstruct.h */
#ifndef png_structp
#include <pngstruct.h>
#endif
#include <sys/types.h> #include <sys/types.h>
#ifdef __MINGW32__
#include <winsock2.h>
#else
#include <sys/socket.h> #include <sys/socket.h>
#endif
#include "guacio.h" #include "guacio.h"
#include "protocol.h" #include "protocol.h"