Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

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