A clone of btpd with my configuration changes.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

136 lignes
2.6 KiB

  1. /*
  2. * Compile with:
  3. * cc -I/usr/local/include -o event-test event-test.c -L/usr/local/lib -levent
  4. */
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #ifndef WIN32
  8. #include <sys/queue.h>
  9. #include <unistd.h>
  10. #include <sys/time.h>
  11. #else
  12. #include <windows.h>
  13. #endif
  14. #include <fcntl.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <errno.h>
  19. #include <event.h>
  20. void
  21. fifo_read(int fd, short event, void *arg)
  22. {
  23. char buf[255];
  24. int len;
  25. struct event *ev = arg;
  26. #ifdef WIN32
  27. DWORD dwBytesRead;
  28. #endif
  29. /* Reschedule this event */
  30. event_add(ev, NULL);
  31. fprintf(stderr, "fifo_read called with fd: %d, event: %d, arg: %p\n",
  32. fd, event, arg);
  33. #ifdef WIN32
  34. len = ReadFile((HANDLE)fd, buf, sizeof(buf) - 1, &dwBytesRead, NULL);
  35. // Check for end of file.
  36. if(len && dwBytesRead == 0) {
  37. fprintf(stderr, "End Of File");
  38. event_del(ev);
  39. return;
  40. }
  41. buf[dwBytesRead] = '\0';
  42. #else
  43. len = read(fd, buf, sizeof(buf) - 1);
  44. if (len == -1) {
  45. perror("read");
  46. return;
  47. } else if (len == 0) {
  48. fprintf(stderr, "Connection closed\n");
  49. return;
  50. }
  51. buf[len] = '\0';
  52. #endif
  53. fprintf(stdout, "Read: %s\n", buf);
  54. }
  55. int
  56. main (int argc, char **argv)
  57. {
  58. struct event evfifo;
  59. #ifdef WIN32
  60. HANDLE socket;
  61. // Open a file.
  62. socket = CreateFile("test.txt", // open File
  63. GENERIC_READ, // open for reading
  64. 0, // do not share
  65. NULL, // no security
  66. OPEN_EXISTING, // existing file only
  67. FILE_ATTRIBUTE_NORMAL, // normal file
  68. NULL); // no attr. template
  69. if(socket == INVALID_HANDLE_VALUE)
  70. return 1;
  71. #else
  72. struct stat st;
  73. char *fifo = "event.fifo";
  74. int socket;
  75. if (lstat (fifo, &st) == 0) {
  76. if ((st.st_mode & S_IFMT) == S_IFREG) {
  77. errno = EEXIST;
  78. perror("lstat");
  79. exit (1);
  80. }
  81. }
  82. unlink (fifo);
  83. if (mkfifo (fifo, 0600) == -1) {
  84. perror("mkfifo");
  85. exit (1);
  86. }
  87. /* Linux pipes are broken, we need O_RDWR instead of O_RDONLY */
  88. #ifdef __linux
  89. socket = open (fifo, O_RDWR | O_NONBLOCK, 0);
  90. #else
  91. socket = open (fifo, O_RDONLY | O_NONBLOCK, 0);
  92. #endif
  93. if (socket == -1) {
  94. perror("open");
  95. exit (1);
  96. }
  97. fprintf(stderr, "Write data to %s\n", fifo);
  98. #endif
  99. /* Initalize the event library */
  100. event_init();
  101. /* Initalize one event */
  102. #ifdef WIN32
  103. event_set(&evfifo, (int)socket, EV_READ, fifo_read, &evfifo);
  104. #else
  105. event_set(&evfifo, socket, EV_READ, fifo_read, &evfifo);
  106. #endif
  107. /* Add it to the active events, without a timeout */
  108. event_add(&evfifo, NULL);
  109. event_dispatch();
  110. #ifdef WIN32
  111. CloseHandle(socket);
  112. #endif
  113. return (0);
  114. }