Fix compilation of rate-limiting code on win32.

This commit is contained in:
Nick Mathewson 2009-12-30 14:29:56 -05:00
parent 885b42734c
commit 165d30e31a
5 changed files with 38 additions and 3 deletions

View File

@ -325,7 +325,7 @@ read_complete(struct event_overlapped *eo, uintptr_t key,
if (ok && nbytes) {
BEV_RESET_GENERIC_READ_TIMEOUT(bev);
_bufferevent_derement_read_buckets(&bev_a->bev, nbytes);
_bufferevent_decrement_read_buckets(&bev_a->bev, nbytes);
if (bev->readcb != NULL &&
evbuffer_get_length(bev->input) >= bev->wm_read.low)
_bufferevent_run_readcb(bev);
@ -359,7 +359,7 @@ write_complete(struct event_overlapped *eo, uintptr_t key,
if (ok && nbytes) {
BEV_RESET_GENERIC_WRITE_TIMEOUT(bev);
_bufferevent_derement_write_buckets(&bev_a->bev, nbytes);
_bufferevent_decrement_write_buckets(&bev_a->bev, nbytes);
if (bev->writecb != NULL &&
evbuffer_get_length(bev->output) <= bev->wm_write.low)
_bufferevent_run_writecb(bev);

View File

@ -415,7 +415,7 @@ _bev_group_random_element(struct bufferevent_rate_limit_group *group)
EVUTIL_ASSERT(! TAILQ_EMPTY(&group->members));
which = random() % group->n_members;
which = _evutil_weakrand() % group->n_members;
bev = TAILQ_FIRST(&group->members);
while (which--)

View File

@ -1759,3 +1759,14 @@ evutil_getenv(const char *varname)
return getenv(varname);
}
long
_evutil_weakrand(void)
{
#ifdef WIN32
return rand();
#else
return random();
#endif
}

View File

@ -30,6 +30,11 @@
#include <assert.h>
#include <math.h>
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#endif
#include <event2/bufferevent.h>
#include <event2/buffer.h>
#include <event2/event.h>
@ -63,7 +68,11 @@ loud_writecb(struct bufferevent *bev, void *ctx)
struct client_state *cs = ctx;
struct evbuffer *output = bufferevent_get_output(bev);
char buf[1024];
#ifdef WIN32
int r = rand() % 256;
#else
int r = random() % 256;
#endif
memset(buf, r, sizeof(buf));
while (evbuffer_get_length(output) < 8192) {
evbuffer_add(output, buf, sizeof(buf));
@ -285,6 +294,15 @@ main(int argc, char **argv)
int i,j;
double ratio;
#ifdef WIN32
WORD wVersionRequested = MAKEWORD(2,2);
WSADATA wsaData;
int err;
err = WSAStartup(wVersionRequested, &wsaData);
#endif
for (i = 1; i < argc; ++i) {
for (j = 0; options[j].name; ++j) {
if (!strcmp(argv[i],options[j].name)) {
@ -315,7 +333,11 @@ main(int argc, char **argv)
{
struct timeval tv;
evutil_gettimeofday(&tv, NULL);
#ifdef WIN32
srand(tv.tv_usec);
#else
srandom(tv.tv_usec);
#endif
}
evthread_enable_lock_debuging();

View File

@ -146,6 +146,8 @@ int evutil_resolve(int family, const char *hostname, struct sockaddr *sa,
const char *evutil_getenv(const char *name);
long _evutil_weakrand(void);
/* Evaluates to the same boolean value as 'p', and hints to the compiler that
* we expect this value to be false. */
#ifdef __GNUC__X