Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 

199 Zeilen
6.2 KiB

  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef IO_H
  3. #define IO_H
  4. /* http://cr.yp.to/lib/io.html */
  5. #include "uint64.h"
  6. #include "taia.h"
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /* like open(s,O_RDONLY) */
  11. /* return 1 if ok, 0 on error */
  12. int io_readfile(int64* d,const char* s);
  13. /* like open(s,O_WRONLY|O_CREAT|O_TRUNC,0600) */
  14. /* return 1 if ok, 0 on error */
  15. int io_createfile(int64* d,const char* s);
  16. /* like open(s,O_RDWR) */
  17. /* return 1 if ok, 0 on error */
  18. int io_readwritefile(int64* d,const char* s);
  19. /* like open(s,O_WRONLY|O_APPEND|O_CREAT,0600) */
  20. /* return 1 if ok, 0 on error */
  21. int io_appendfile(int64* d,const char* s);
  22. /* like pipe(d) */
  23. /* return 1 if ok, 0 on error */
  24. int io_pipe(int64* d);
  25. /* like socketpair() */
  26. /* return 1 if ok, 0 on error */
  27. int io_socketpair(int64* d);
  28. /* non-blocking read(), -1 for EAGAIN and -3+errno for other errors */
  29. int64 io_tryread(int64 d,char* buf,int64 len);
  30. /* blocking read(), with -3 instead of -1 for errors */
  31. int64 io_waitread(int64 d,char* buf,int64 len);
  32. /* non-blocking write(), -1 for EAGAIN and -3+errno for other errors */
  33. int64 io_trywrite(int64 d,const char* buf,int64 len);
  34. /* blocking write(), with -3 instead of -1 for errors */
  35. int64 io_waitwrite(int64 d,const char* buf,int64 len);
  36. /* modify timeout attribute of file descriptor */
  37. void io_timeout(int64 d,tai6464 t);
  38. /* like io_tryread but will return -2,errno=ETIMEDOUT if d has a timeout
  39. * associated and it is passed without input being there */
  40. int64 io_tryreadtimeout(int64 d,char* buf,int64 len);
  41. /* like io_trywrite but will return -2,errno=ETIMEDOUT if d has a timeout
  42. * associated and it is passed without being able to write */
  43. int64 io_trywritetimeout(int64 d,const char* buf,int64 len);
  44. void io_wantread(int64 d);
  45. void io_wantwrite(int64 d);
  46. void io_dontwantread(int64 d);
  47. void io_dontwantwrite(int64 d);
  48. void io_wait();
  49. void io_waituntil(tai6464 t);
  50. int64 io_waituntil2(int64 milliseconds);
  51. void io_check();
  52. /* signal that read/accept/whatever returned EAGAIN */
  53. /* needed for SIGIO and epoll */
  54. void io_eagain(int64 d); /* do not use, API was a bad idea */
  55. #define HAVE_EAGAIN_READWRITE
  56. void io_eagain_read(int64 d); /* use these ones */
  57. void io_eagain_write(int64 d);
  58. /* return next descriptor from io_wait that can be read from */
  59. int64 io_canread();
  60. /* return next descriptor from io_wait that can be written to */
  61. int64 io_canwrite();
  62. /* return next descriptor with expired timeout */
  63. int64 io_timeouted();
  64. /* is this fd over its timeout? */
  65. int io_timedout(int64 d);
  66. /* 1 means: have IO_FD_CANWRITE, IO_FD_BLOCK and IO_FD_NONBLOCK,
  67. * will be incremented if API is extended in the future */
  68. #define HAVE_IO_FD_FLAGS 1
  69. enum io_fd_flags {
  70. IO_FD_CANWRITE=1, /* new TCP connection, we know it's writable */
  71. IO_FD_BLOCK=2, /* skip the fcntl, assume fd is set to blocking */
  72. IO_FD_NONBLOCK=4, /* skip the fcntl, assume fd is set to non-blocking */
  73. };
  74. /* put d on internal data structure, return 1 on success, 0 on error */
  75. int io_fd(int64 d); /* use this for sockets before you called connect() or accept() */
  76. int io_fd_canwrite(int64 d); /* use this for connected sockets (assumes socket is writable) */
  77. int io_fd_flags(int64 d,int flags); /* can be used to tell io_fd to skip one syscall */
  78. void io_setcookie(int64 d,void* cookie);
  79. void* io_getcookie(int64 d);
  80. /* put descriptor in non-blocking mode */
  81. void io_nonblock(int64 d);
  82. /* put descriptor in blocking mode */
  83. void io_block(int64 d);
  84. /* put descriptor in close-on-exec mode */
  85. void io_closeonexec(int64 d);
  86. void io_close(int64 d);
  87. /* Free the internal data structures from libio.
  88. * This only makes sense if you run your program in a malloc checker and
  89. * these produce false alarms. Your OS will free these automatically on
  90. * process termination. */
  91. void io_finishandshutdown(void);
  92. /* send n bytes from file fd starting at offset off to socket s */
  93. /* return number of bytes written */
  94. int64 io_sendfile(int64 s,int64 fd,uint64 off,uint64 n);
  95. /* Pass fd over sock (must be a unix domain socket) to other process.
  96. * Return 0 if ok, -1 on error, setting errno. */
  97. int io_passfd(int64 sock,int64 fd);
  98. /* Receive fd over sock (must be a unix domain socket) from other
  99. * process. Return sock if ok, -1 on error, setting errno. */
  100. int64 io_receivefd(int64 sock);
  101. int io_starteventloopthread(unsigned int threads);
  102. #define HAVE_IO_QUEUEFORREAD
  103. /* Artificially queue a file descriptor as readable.
  104. * The next call to io_canread will return this descriptor. */
  105. int io_queueforread(int64 d);
  106. /* Artificially queue a file descriptor as writable.
  107. * The next call to io_canread will return this descriptor. */
  108. int io_queueforwrite(int64 d);
  109. typedef int64 (*io_write_callback)(int64 s,const void* buf,uint64 n);
  110. /* used internally, but hey, who knows */
  111. int64 io_mmapwritefile(int64 out,int64 in,uint64 off,uint64 bytes,io_write_callback writecb);
  112. /* only needed for debugging, will print some stats into the buffer to
  113. * aid in debugging the state machine if a descriptor loops or so */
  114. unsigned int io_debugstring(int64 s,char* buf,unsigned int bufsize);
  115. #ifdef __MINGW32__
  116. #include <mcfgthread/c11thread.h>
  117. #elif defined(__dietlibc__)
  118. #include <threads.h>
  119. #else
  120. #include <pthread.h>
  121. #include <semaphore.h>
  122. #endif
  123. enum { SLOTS=128 };
  124. typedef struct iomux {
  125. int ctx;
  126. int working; /* used to synchronize who is filling the queue */
  127. unsigned int h,l; /* high, low */
  128. struct {
  129. int fd, events;
  130. } q[SLOTS];
  131. #if defined(__MINGW32__) || defined(__dietlibc__)
  132. mtx_t mtx;
  133. cnd_t sem;
  134. #else
  135. sem_t sem;
  136. #endif
  137. } iomux_t;
  138. /* Init master context */
  139. int iom_init(iomux_t* c);
  140. /* Add socket to iomux */
  141. enum {
  142. IOM_READ=1,
  143. IOM_WRITE=2,
  144. IOM_ERROR=4
  145. };
  146. /* return -1 if error, events can be IOM_READ or IOM_WRITE */
  147. int iom_add(iomux_t* c,int64 s,unsigned int events);
  148. /* Blocking wait for single event, timeout in milliseconds */
  149. /* return -1 if error, 0 if ok; s set to fd, revents set to known events on that fd */
  150. /* when done with the fd, call iom_add on it again! */
  151. /* This can be called by multiple threads in parallel */
  152. int iom_wait(iomux_t* c,int64* s,unsigned int* revents,unsigned long timeout);
  153. /* Call this to terminate all threads waiting in iom_wait */
  154. int iom_abort(iomux_t* c);
  155. #ifdef __cplusplus
  156. }
  157. #endif
  158. #endif