r19649@catbus: nickm | 2008-05-08 10:00:14 -0400

Replace gettimeofday() usage with a new evutil_gettimeofday().  This removes all previous need for win32-code/misc.[ch]


svn:r792
This commit is contained in:
Nick Mathewson 2008-05-08 14:06:33 +00:00
parent 85ed713343
commit a26442c5ac
8 changed files with 36 additions and 111 deletions

View File

@ -95,6 +95,8 @@ Changes in current version:
o event_base_new_with_config() and corresponding config APIs.
o migrate the evhttp header to event2/ but accessors are still missing.
o deprecate timeout_* event functions by moving them to event_compat.h
o Move windows gettimeofday replacement into a new evutil_gettimeofday().
Changes in 1.4.0:
o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr.

View File

@ -39,8 +39,8 @@ EXTRA_DIST = autogen.sh event.h event-internal.h log.h evsignal.h evdns.3 \
test/test-eof.c test/test-weof.c test/test-time.c \
test/test-init.c test/test.sh \
compat/sys/queue.h compat/sys/_time.h \
WIN32-Code/config.h WIN32-Code/misc.c \
WIN32-Code/win32.c WIN32-Code/misc.h \
WIN32-Code/config.h \
WIN32-Code/win32.c \
WIN32-Code/tree.h \
WIN32-Prj/event_test/event_test.dsp \
WIN32-Prj/event_test/test.txt WIN32-Prj/libevent.dsp \
@ -54,7 +54,7 @@ SUBDIRS = . include sample test
if BUILD_WIN32
SYS_LIBS = -lws2_32
SYS_SRC = WIN32-Code/misc.c WIN32-Code/win32.c
SYS_SRC = WIN32-Code/win32.c
SYS_INCLUDES = -IWIN32-Code
else

View File

@ -1,93 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <sys/timeb.h>
#include <time.h>
#ifdef __GNUC__
/*our prototypes for timeval and timezone are in here, just in case the above
headers don't have them*/
#include "misc.h"
#endif
/****************************************************************************
*
* Function: gettimeofday(struct timeval *, struct timezone *)
*
* Purpose: Get current time of day.
*
* Arguments: tv => Place to store the curent time of day.
* tz => Ignored.
*
* Returns: 0 => Success.
*
****************************************************************************/
#ifndef HAVE_GETTIMEOFDAY
int gettimeofday(struct timeval *tv, struct timezone *tz) {
struct _timeb tb;
if(tv == NULL)
return -1;
_ftime(&tb);
tv->tv_sec = (long) tb.time;
tv->tv_usec = ((int) tb.millitm) * 1000;
return 0;
}
#endif
#if 0
int
win_read(int fd, void *buf, unsigned int length)
{
DWORD dwBytesRead;
int res = ReadFile((HANDLE) fd, buf, length, &dwBytesRead, NULL);
if (res == 0) {
DWORD error = GetLastError();
if (error == ERROR_NO_DATA)
return (0);
return (-1);
} else
return (dwBytesRead);
}
int
win_write(int fd, void *buf, unsigned int length)
{
DWORD dwBytesWritten;
int res = WriteFile((HANDLE) fd, buf, length, &dwBytesWritten, NULL);
if (res == 0) {
DWORD error = GetLastError();
if (error == ERROR_NO_DATA)
return (0);
return (-1);
} else
return (dwBytesWritten);
}
int
socketpair(int d, int type, int protocol, int *sv)
{
static int count;
char buf[64];
HANDLE fd;
DWORD dwMode;
sprintf(buf, "\\\\.\\pipe\\levent-%d", count++);
/* Create a duplex pipe which will behave like a socket pair */
fd = CreateNamedPipe(buf, PIPE_ACCESS_DUPLEX, PIPE_TYPE_BYTE | PIPE_NOWAIT,
PIPE_UNLIMITED_INSTANCES, 4096, 4096, 0, NULL);
if (fd == INVALID_HANDLE_VALUE)
return (-1);
sv[0] = (int)fd;
fd = CreateFile(buf, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (fd == INVALID_HANDLE_VALUE)
return (-1);
dwMode = PIPE_NOWAIT;
SetNamedPipeHandleState(fd, &dwMode, NULL, NULL);
sv[1] = (int)fd;
return (0);
}
#endif

View File

@ -1,11 +0,0 @@
#ifndef MISC_H
#define MISC_H
struct timezone;
struct timeval;
#ifndef HAVE_GETTIMEOFDAY
int gettimeofday(struct timeval *,struct timezone *);
#endif
#endif

View File

@ -1084,13 +1084,13 @@ default_transaction_id_fn(void)
#ifdef DNS_USE_GETTIMEOFDAY_FOR_ID
struct timeval tv;
gettimeofday(&tv, NULL);
evutil_gettimeofday(&tv, NULL);
trans_id = tv.tv_usec & 0xffff;
#endif
#ifdef DNS_USE_OPENSSL_FOR_ID
if (RAND_pseudo_bytes((u8 *) &trans_id, 2) == -1) {
/* in the case that the RAND call fails we back */
/* in the case that the RAND call fails we used to back */
/* down to using gettimeofday. */
/*
struct timeval tv;

View File

@ -171,7 +171,7 @@ gettime(struct event_base *base, struct timeval *tp)
}
#endif
return (gettimeofday(tp, NULL));
return (evutil_gettimeofday(tp, NULL));
}
struct event_base *
@ -895,7 +895,7 @@ event_pending(struct event *ev, short event, struct timeval *tv)
gettime(ev->ev_base, &now);
evutil_timersub(&ev->ev_timeout, &now, &res);
/* correctly remap to real time */
gettimeofday(&now, NULL);
evutil_gettimeofday(&now, NULL);
evutil_timeradd(&now, &res, tv);
}

View File

@ -51,6 +51,11 @@
#endif
#include <errno.h>
#ifndef _EVENT_HAVE_GETTIMEOFDAY
#include <sys/timeb.h>
#include <time.h>
#endif
#include "event2/util.h"
#include "log.h"
@ -196,3 +201,19 @@ evutil_strtoll(const char *s, char **endptr, int base)
#error "I don't know how to parse 64-bit integers."
#endif
}
#ifndef _EVENT_HAVE_GETTIMEOFDAY
int
evutil_gettimeofday(struct timeval *tv, struct timezone *tz)
{
struct _timeb tb;
if(tv == NULL)
return -1;
_ftime(&tb);
tv->tv_sec = (long) tb.time;
tv->tv_usec = ((int) tb.millitm) * 1000;
return 0;
}
#endif

View File

@ -179,6 +179,12 @@ int evutil_make_socket_nonblocking(evutil_socket_t sock);
/* big-int related functions */
ev_int64_t evutil_strtoll(const char *s, char **endptr, int base);
#ifdef _EVENT_HAVE_GETTIMEOFDAY
#define evutil_gettimeofday(tv, tz) gettimeofday((tv), (tz))
#else
int evutil_gettimeofday(struct timeval *tv, struct timezone *tz);
#endif
#ifdef __cplusplus
}
#endif