mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-15 07:15:03 -04:00
windows support
svn:r79
This commit is contained in:
parent
e0216ed709
commit
9d26a46c39
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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++;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user