windows support

svn:r79
This commit is contained in:
Niels Provos 2003-09-25 03:29:37 +00:00
parent e0216ed709
commit 9d26a46c39
3 changed files with 58 additions and 9 deletions

View File

@ -5,13 +5,17 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h> #include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#else
#include <windows.h>
#endif
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <event.h> #include <event.h>
@ -22,14 +26,29 @@ fifo_read(int fd, short event, void *arg)
char buf[255]; char buf[255];
int len; int len;
struct event *ev = arg; struct event *ev = arg;
#ifdef WIN32
DWORD dwBytesRead;
#endif
/* Reschedule this event */ /* Reschedule this event */
event_add(ev, NULL); event_add(ev, NULL);
fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n", fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
fd, event, arg); fd, event, arg);
#ifdef WIN32
len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
// Check for end of file.
if(len && dwBytesRead == 0) {
fprintf(stderr, "End Of File");
event_del(ev);
return;
}
buf[dwBytesRead + 1] = '\0';
#else
len = read(fd, buf, sizeof(buf) - 1); len = read(fd, buf, sizeof(buf) - 1);
if (len == -1) { if (len == -1) {
perror("read"); perror("read");
return; return;
@ -39,16 +58,32 @@ fifo_read(int fd, short event, void *arg)
} }
buf[len] = '\0'; buf[len] = '\0';
#endif
fprintf(stdout, "Read: %s\n", buf); fprintf(stdout, "Read: %s\n", buf);
} }
int int
main (int argc, char **argv) main (int argc, char **argv)
{ {
struct event evfifo;
#ifdef WIN32
HANDLE socket;
// Open a file.
socket = CreateFile("test.txt", // open File
GENERIC_READ, // open for reading
0, // do not share
NULL, // no security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attr. template
if(socket == INVALID_HANDLE_VALUE)
return 1;
#else
struct stat st; struct stat st;
char *fifo = "event.fifo"; char *fifo = "event.fifo";
int socket; int socket;
struct event evfifo;
if (lstat (fifo, &st) == 0) { if (lstat (fifo, &st) == 0) {
if ((st.st_mode & S_IFMT) == S_IFREG) { if ((st.st_mode & S_IFMT) == S_IFREG) {
@ -77,18 +112,24 @@ main (int argc, char **argv)
} }
fprintf(stderr, "Write data to %s\n", fifo); fprintf(stderr, "Write data to %s\n", fifo);
#endif
/* Initalize the event library */ /* Initalize the event library */
event_init(); event_init();
/* Initalize one event */ /* Initalize one event */
#ifdef WIN32
event_set(&evfifo, (int)socket, EV_READ, fifo_read, &evfifo);
#else
event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo); event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
#endif
/* Add it to the active events, without a timeout */ /* Add it to the active events, without a timeout */
event_add(&evfifo, NULL); event_add(&evfifo, NULL);
event_dispatch(); event_dispatch();
#ifdef WIN32
CloseHandle(socket);
#endif
return (0); return (0);
} }

View File

@ -5,14 +5,18 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h> #include <sys/queue.h>
#include <unistd.h>
#include <sys/time.h> #include <sys/time.h>
#else
#include <windows.h>
#endif
#include <signal.h> #include <signal.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <event.h> #include <event.h>
@ -24,7 +28,7 @@ signal_cb(int fd, short event, void *arg)
{ {
struct event *signal = arg; struct event *signal = arg;
printf("%s: got signal %d\n", __FUNCTION__, EVENT_SIGNAL(signal)); printf("%s: got signal %d\n", __func__, EVENT_SIGNAL(signal));
if (called >= 2) if (called >= 2)
event_del(signal); event_del(signal);

View File

@ -5,13 +5,17 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#ifndef WIN32
#include <sys/queue.h> #include <sys/queue.h>
#include <unistd.h>
#else
#include <time.h>
#endif
#include <sys/time.h> #include <sys/time.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h>
#include <errno.h> #include <errno.h>
#include <event.h> #include <event.h>
@ -25,7 +29,7 @@ timeout_cb(int fd, short event, void *arg)
struct event *timeout = arg; struct event *timeout = arg;
int newtime = time(NULL); int newtime = time(NULL);
printf("%s: called at %d: %d\n", __FUNCTION__, newtime, printf("%s: called at %d: %d\n", __func__, newtime,
newtime - lasttime); newtime - lasttime);
lasttime = newtime; lasttime = newtime;