async.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /** @file async.c
  2. * @brief Asynchronous input and output.
  3. *
  4. * Class uses non-blocking input and output and has internal input and output
  5. * buffers. Class has associated file descriptor to work with it.
  6. *
  7. * For async writing user uses faux_async_write() function. It writes all
  8. * given data to internal buffer and then tries to really write it to file
  9. * descriptor. If not all data was written in non-blocking mode then function
  10. * executes special callback "stall" function to inform us about non-empty
  11. * output buffer. "Stall" callback function can make programm to inspect fd
  12. * for write possibility. Then programm must call faux_async_out() to really
  13. * write the rest of the data to fd. Function also can execute "stall" callback.
  14. *
  15. * For async reading user can call faux_sync_in(). For example this function
  16. * can be called after select() or poll() when data is available on interested
  17. * fd. Function reads data in non-blocking mode and stores to internal buffer.
  18. * User can specify read "limits" - min and max. When amount of reded data is
  19. * greater or equal to "min" limit then "read" callback will be executed.
  20. * The "read" callback will get allocated buffer with received data. The
  21. * length of the data is greater or equal to "min" limit and less or equal to
  22. * "max" limit.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif /* HAVE_CONFIG_H */
  27. #include <stdlib.h>
  28. #include <stdint.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include <unistd.h>
  33. #include <errno.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include <fcntl.h>
  37. #include "faux/faux.h"
  38. #include "faux/str.h"
  39. #include "faux/buf.h"
  40. #include "faux/net.h"
  41. #include "faux/async.h"
  42. #include "private.h"
  43. /** @brief Create new async I/O object.
  44. *
  45. * Constructor gets associated file descriptor to operate on it. File
  46. * descriptor must be nonblocked. If not so then constructor will set
  47. * nonblock flag itself.
  48. *
  49. * @param [in] fd File descriptor.
  50. * @return Allocated object or NULL on error.
  51. */
  52. faux_async_t *faux_async_new(int fd)
  53. {
  54. faux_async_t *async = NULL;
  55. int fflags = 0;
  56. // Prepare FD
  57. if (fd < 0) // Illegal fd
  58. return NULL;
  59. if ((fflags = fcntl(fd, F_GETFL)) == -1)
  60. return NULL;
  61. if (fcntl(fd, F_SETFL, fflags | O_NONBLOCK) == -1)
  62. return NULL;
  63. async = faux_zmalloc(sizeof(*async));
  64. assert(async);
  65. if (!async)
  66. return NULL;
  67. // Init
  68. async->fd = fd;
  69. // Read (Input)
  70. async->read_cb = NULL;
  71. async->read_udata = NULL;
  72. async->min = 1;
  73. async->max = FAUX_ASYNC_UNLIMITED;
  74. async->ibuf = faux_buf_new(DATA_CHUNK);
  75. faux_buf_set_limit(async->ibuf, FAUX_ASYNC_IN_OVERFLOW);
  76. // Write (Output)
  77. async->stall_cb = NULL;
  78. async->stall_udata = NULL;
  79. async->obuf = faux_buf_new(DATA_CHUNK);
  80. faux_buf_set_limit(async->obuf, FAUX_ASYNC_OUT_OVERFLOW);
  81. return async;
  82. }
  83. /** @brief Free async I/O object.
  84. *
  85. * @param [in] Async I/O object.
  86. */
  87. void faux_async_free(faux_async_t *async)
  88. {
  89. if (!async)
  90. return;
  91. faux_buf_free(async->ibuf);
  92. faux_buf_free(async->obuf);
  93. faux_free(async);
  94. }
  95. /** @brief Get file descriptor from async I/O object.
  96. *
  97. * @param [in] async Allocated and initialized async I/O object.
  98. * @return Serviced file descriptor.
  99. */
  100. int faux_async_fd(const faux_async_t *async)
  101. {
  102. assert(async);
  103. if (!async)
  104. return -1;
  105. return async->fd;
  106. }
  107. /** @brief Set read callback and associated user data.
  108. *
  109. * If callback function pointer is NULL then class will drop all readed data.
  110. *
  111. * @param [in] async Allocated and initialized async I/O object.
  112. * @param [in] read_cb Read callback.
  113. * @param [in] user_data Associated user data.
  114. */
  115. void faux_async_set_read_cb(faux_async_t *async,
  116. faux_async_read_cb_fn read_cb, void *user_data)
  117. {
  118. assert(async);
  119. if (!async)
  120. return;
  121. async->read_cb = read_cb;
  122. async->read_udata = user_data;
  123. }
  124. /** @brief Set read limits.
  125. *
  126. * Read limits define conditions when the read callback will be executed.
  127. * Buffer must contain data amount greater or equal to "min" value. Callback
  128. * will not get data amount greater than "max" value. If min == max then
  129. * callback will be executed with fixed data size. The "max" value can be "0".
  130. * It means indefinite i.e. data transferred to callback can be really large.
  131. *
  132. * @param [in] async Allocated and initialized async I/O object.
  133. * @param [in] min Minimal data amount.
  134. * @param [in] max Maximal data amount. The "0" means indefinite.
  135. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  136. */
  137. bool_t faux_async_set_read_limits(faux_async_t *async, size_t min, size_t max)
  138. {
  139. assert(async);
  140. if (!async)
  141. return BOOL_FALSE;
  142. if (min < 1)
  143. return BOOL_FALSE;
  144. if ((min > max) && (max != 0))
  145. return BOOL_FALSE;
  146. async->min = min;
  147. async->max = max;
  148. return BOOL_TRUE;
  149. }
  150. /** @brief Set stall callback and associated user data.
  151. *
  152. * @param [in] async Allocated and initialized async I/O object.
  153. * @param [in] stall_cb Stall callback.
  154. * @param [in] user_data Associated user data.
  155. */
  156. void faux_async_set_stall_cb(faux_async_t *async,
  157. faux_async_stall_cb_fn stall_cb, void *user_data)
  158. {
  159. assert(async);
  160. if (!async)
  161. return;
  162. async->stall_cb = stall_cb;
  163. async->stall_udata = user_data;
  164. }
  165. /** @brief Set write overflow value.
  166. *
  167. * "Overflow" is a value when engine consider data consumer as a stalled.
  168. * Data gets into the async I/O object buffer but object can't write it to
  169. * serviced fd for too long time. So it accumulates great amount of data.
  170. *
  171. * @param [in] async Allocated and initialized async I/O object.
  172. * @param [in] overflow Overflow value.
  173. */
  174. void faux_async_set_write_overflow(faux_async_t *async, size_t overflow)
  175. {
  176. assert(async);
  177. if (!async)
  178. return;
  179. faux_buf_set_limit(async->obuf, overflow);
  180. }
  181. /** @brief Set read overflow value.
  182. *
  183. * "Overflow" is a value when engine consider data consumer as a stalled.
  184. * Data gets into the async I/O object buffer but object can't write it to
  185. * serviced fd for too long time. So it accumulates great amount of data.
  186. *
  187. * @param [in] async Allocated and initialized async I/O object.
  188. * @param [in] overflow Overflow value.
  189. */
  190. void faux_async_set_read_overflow(faux_async_t *async, size_t overflow)
  191. {
  192. assert(async);
  193. if (!async)
  194. return;
  195. faux_buf_set_limit(async->ibuf, overflow);
  196. }
  197. /** @brief Async data write.
  198. *
  199. * All given data will be stored to internal buffer (list of data chunks).
  200. * Then function will try to write stored data to file descriptor in
  201. * non-blocking mode. Note some data can be left within buffer. In this case
  202. * the "stall" callback will be executed to inform about it. To try to write
  203. * the rest of the data user can be call faux_async_out() function. Both
  204. * functions will not block.
  205. *
  206. * @param [in] async Allocated and initialized async I/O object.
  207. * @param [in] data Data buffer to write.
  208. * @param [in] len Data length to write.
  209. * @return Length of stored/writed data or < 0 on error.
  210. */
  211. ssize_t faux_async_write(faux_async_t *async, void *data, size_t len)
  212. {
  213. ssize_t data_written = len;
  214. assert(async);
  215. if (!async)
  216. return -1;
  217. assert(data);
  218. if (!data)
  219. return -1;
  220. data_written = faux_buf_write(async->obuf, data, len);
  221. if (data_written < 0)
  222. return -1;
  223. // Try to real write data to fd in nonblocked mode
  224. faux_async_out(async);
  225. return len;
  226. }
  227. /** @brief Write output buffer to fd in non-blocking mode.
  228. *
  229. * Previously data must be written to internal buffer by faux_async_write()
  230. * function. But some data can be left within internal buffer because can't be
  231. * written to fd in non-blocking mode. This function tries to write the rest of
  232. * data to fd in non-blocking mode. So function doesn't block. It can be called
  233. * after select() or poll() if fd is ready to be written to. If function can't
  234. * to write all buffer to fd it executes "stall" callback to inform about it.
  235. *
  236. * @param [in] async Allocated and initialized async I/O object.
  237. * @return Length of data actually written or < 0 on error.
  238. */
  239. ssize_t faux_async_out(faux_async_t *async)
  240. {
  241. ssize_t total_written = 0;
  242. assert(async);
  243. if (!async)
  244. return -1;
  245. while (faux_buf_len(async->obuf) > 0) {
  246. ssize_t data_to_write = 0;
  247. ssize_t bytes_written = 0;
  248. bool_t postpone = BOOL_FALSE;
  249. void *data = NULL;
  250. data_to_write = faux_buf_dread_lock_easy(async->obuf, &data);
  251. if (data_to_write <= 0)
  252. return -1;
  253. bytes_written = write(async->fd, data, data_to_write);
  254. if (bytes_written > 0) {
  255. total_written += bytes_written;
  256. faux_buf_dread_unlock_easy(async->obuf, bytes_written);
  257. } else {
  258. faux_buf_dread_unlock_easy(async->obuf, 0);
  259. }
  260. if (bytes_written < 0) {
  261. if ( // Something went wrong
  262. (errno != EINTR) &&
  263. (errno != EAGAIN) &&
  264. (errno != EWOULDBLOCK)
  265. )
  266. return -1;
  267. // Postpone next read
  268. postpone = BOOL_TRUE;
  269. // Not whole data block was written
  270. } else if (bytes_written != data_to_write) {
  271. // Postpone next read
  272. postpone = BOOL_TRUE;
  273. }
  274. // Postponed
  275. if (postpone) {
  276. // Execute callback
  277. if (async->stall_cb)
  278. async->stall_cb(async,
  279. faux_buf_len(async->obuf),
  280. async->stall_udata);
  281. break;
  282. }
  283. }
  284. return total_written;
  285. }
  286. /** @brief Read data and store it to internal buffer in non-blocking mode.
  287. *
  288. * Reads fd and puts data to internal buffer. It can't be blocked. If length of
  289. * data stored within internal buffer is greater or equal than "min" limit then
  290. * function will execute "read" callback. It allocates linear buffer, copies
  291. * data to it and give it to callback. Note this function will never free
  292. * allocated buffer. So callback must do it or it must be done later. Function
  293. * will not allocate buffer larger than "max" read limit. If "max" limit is "0"
  294. * (it means indefinite) then function will pass all available data to callback.
  295. *
  296. * @param [in] async Allocated and initialized async I/O object.
  297. * @return Length of data actually readed or < 0 on error.
  298. */
  299. ssize_t faux_async_in(faux_async_t *async)
  300. {
  301. ssize_t total_readed = 0;
  302. ssize_t bytes_readed = 0;
  303. ssize_t locked_len = 0;
  304. assert(async);
  305. if (!async)
  306. return -1;
  307. do {
  308. void *data = NULL;
  309. size_t bytes_stored = 0;
  310. locked_len = faux_buf_dwrite_lock_easy(async->ibuf, &data);
  311. if (locked_len <= 0)
  312. return -1;
  313. // Read data
  314. bytes_readed = read(async->fd, data, locked_len);
  315. if (bytes_readed < 0) {
  316. if ( // Something went wrong
  317. (errno != EINTR) &&
  318. (errno != EAGAIN) &&
  319. (errno != EWOULDBLOCK)
  320. )
  321. return -1;
  322. }
  323. faux_buf_dwrite_unlock_easy(async->ibuf, bytes_readed);
  324. total_readed += bytes_readed;
  325. // Check for amount of stored data
  326. while ((bytes_stored = faux_buf_len(async->ibuf)) >= async->min) {
  327. size_t copy_len = 0;
  328. char *buf = NULL;
  329. if (FAUX_ASYNC_UNLIMITED == async->max) { // Indefinite
  330. copy_len = bytes_stored; // Take all data
  331. } else {
  332. copy_len = (bytes_stored < async->max) ?
  333. bytes_stored : async->max;
  334. }
  335. buf = faux_malloc(copy_len);
  336. assert(buf);
  337. faux_buf_read(async->ibuf, buf, copy_len);
  338. // Execute callback
  339. if (async->read_cb)
  340. async->read_cb(async, buf,
  341. copy_len, async->read_udata);
  342. }
  343. } while (bytes_readed == locked_len);
  344. return total_readed;
  345. }