summaryrefslogtreecommitdiff
path: root/libevent/WIN32-Code/misc.c
blob: 371e192beaedd3980537f4103e7da65dead4f82b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#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