X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=blobdiff_plain;f=jbsockets.c;h=625d830e196cfb4d30746e34d514d7fb36ac4193;hp=1fe56e1ebaec39d292136cba032e3cac2460a4da;hb=6eb8be9b2ddbf734039d83c31f0ce3b5987d5d29;hpb=c047f21f14e305dacb22135b2add11b4eca78957 diff --git a/jbsockets.c b/jbsockets.c index 1fe56e1e..625d830e 100644 --- a/jbsockets.c +++ b/jbsockets.c @@ -673,6 +673,60 @@ int write_socket(jb_socket fd, const char *buf, size_t len) } +/********************************************************************* + * + * Function : write_socket_delayed + * + * Description : Write the contents of buf (for n bytes) to + * socket fd, optionally delaying the operation. + * + * Parameters : + * 1 : fd = File descriptor (aka. handle) of socket to write to. + * 2 : buf = Pointer to data to be written. + * 3 : len = Length of data to be written to the socket "fd". + * 4 : delay = Delay in milliseconds. + * + * Returns : 0 on success (entire buffer sent). + * nonzero on error. + * + *********************************************************************/ +int write_socket_delayed(jb_socket fd, const char *buf, size_t len, unsigned int delay) +{ + size_t i = 0; + + if (delay == 0) + { + return write_socket(fd, buf, len); + } + + while (i < len) + { + size_t write_length; + enum {MAX_WRITE_LENGTH = 10}; + + if ((i + MAX_WRITE_LENGTH) > len) + { + write_length = len - i; + } + else + { + write_length = MAX_WRITE_LENGTH; + } + + privoxy_millisleep(delay); + + if (write_socket(fd, buf + i, write_length) != 0) + { + return 1; + } + i += write_length; + } + + return 0; + +} + + /********************************************************************* * * Function : read_socket