#include <sys/types.h>
#include <sys/stat.h>
+#ifndef WIN32
#include <sys/queue.h>
+#include <unistd.h>
#include <sys/time.h>
+#else
+#include <windows.h>
+#endif
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <unistd.h>
#include <errno.h>
#include <event.h>
char buf[255];
int len;
struct event *ev = arg;
+#ifdef WIN32
+ DWORD dwBytesRead;
+#endif
/* Reschedule this event */
event_add(ev, NULL);
fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
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);
+
if (len == -1) {
perror("read");
return;
}
buf[len] = '\0';
+#endif
fprintf(stdout, "Read: %s\n", buf);
}
int
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;
char *fifo = "event.fifo";
int socket;
- struct event evfifo;
if (lstat (fifo, &st) == 0) {
if ((st.st_mode & S_IFMT) == S_IFREG) {
}
fprintf(stderr, "Write data to %s\n", fifo);
-
+#endif
/* Initalize the event library */
event_init();
/* 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);
+#endif
/* Add it to the active events, without a timeout */
event_add(&evfifo, NULL);
event_dispatch();
-
+#ifdef WIN32
+ CloseHandle(socket);
+#endif
return (0);
}
#include <sys/types.h>
#include <sys/stat.h>
+#ifndef WIN32
#include <sys/queue.h>
+#include <unistd.h>
+#else
+#include <time.h>
+#endif
#include <sys/time.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
-#include <unistd.h>
#include <errno.h>
#include <event.h>
struct event *timeout = arg;
int newtime = time(NULL);
- printf("%s: called at %d: %d\n", __FUNCTION__, newtime,
+ printf("%s: called at %d: %d\n", __func__, newtime,
newtime - lasttime);
lasttime = newtime;