]> granicus.if.org Git - libevent/commitdiff
windows support
authorNiels Provos <provos@gmail.com>
Thu, 25 Sep 2003 03:29:37 +0000 (03:29 +0000)
committerNiels Provos <provos@gmail.com>
Thu, 25 Sep 2003 03:29:37 +0000 (03:29 +0000)
svn:r79

sample/event-test.c
sample/signal-test.c
sample/time-test.c

index f4cfa11b0272c6b8813b4ec2e7786360dde0e501..0eac37e7001f7274a44b51215c5395f8c6d41227 100644 (file)
@@ -5,13 +5,17 @@
 
 #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>
@@ -22,14 +26,29 @@ fifo_read(int fd, short event, void *arg)
        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;
@@ -39,16 +58,32 @@ fifo_read(int fd, short event, void *arg)
        }
 
        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) {
@@ -77,18 +112,24 @@ main (int argc, char **argv)
        }
 
        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);
 }
 
index 562bcf1e33c574e7e5acf16bf994178258aeae95..7a1fefec8050d367a089123e4c78d5d81966f68d 100644 (file)
@@ -5,14 +5,18 @@
 
 #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 <signal.h>
 #include <fcntl.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <unistd.h>
 #include <errno.h>
 
 #include <event.h>
@@ -24,11 +28,11 @@ signal_cb(int fd, short event, void *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)
                event_del(signal);
-
+       
        called++;
 }
 
index 86c14724e64644bd30ec410558c1ede54ac49564..da508ff88ed0f31ba8020d79eb7a380b11996bba 100644 (file)
@@ -5,13 +5,17 @@
 
 #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>
@@ -25,7 +29,7 @@ timeout_cb(int fd, short event, void *arg)
        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;