summary refs log tree commit diff
path: root/libevent/WIN32-Code/misc.c
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-11-07 09:40:25 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-11-07 09:40:25 +0000
commit5ccf414cbff49636a365a6bce2d2641be90ea412 (patch)
treea4305df50e7893cecb60822d6e1b50403e45e8be /libevent/WIN32-Code/misc.c
parent568d1163350f81be1f71b6c8b2c7b59fc4a4db86 (diff)
downloadbtpd-5ccf414cbff49636a365a6bce2d2641be90ea412.tar.gz
btpd-5ccf414cbff49636a365a6bce2d2641be90ea412.zip
Include libevent 1.2 in btpd.
Diffstat (limited to 'libevent/WIN32-Code/misc.c')
-rw-r--r--libevent/WIN32-Code/misc.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/libevent/WIN32-Code/misc.c b/libevent/WIN32-Code/misc.c
new file mode 100644
index 0000000..6f63ddf
--- /dev/null
+++ b/libevent/WIN32-Code/misc.c
@@ -0,0 +1,91 @@
+#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 = tb.time;
+	tv->tv_usec = ((int) tb.millitm) * 1000;
+	return 0;
+}
+#endif
+
+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);
+}
\ No newline at end of file