async.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 Async "struct iovec" write.
  228. *
  229. * This function is like a faux_async_write() function but uses scatter/gather.
  230. *
  231. * @see faux_async_write().
  232. * @param [in] async Allocated and initialized async I/O object.
  233. * @param [in] iov Array of "struct iovec" structures.
  234. * @param [in] iovcnt Number of iov array members.
  235. * @return Length of stored/writed data or < 0 on error.
  236. */
  237. ssize_t faux_async_writev(faux_async_t *async,
  238. const struct iovec *iov, int iovcnt)
  239. {
  240. size_t total_written = 0;
  241. int i = 0;
  242. assert(async);
  243. if (!async)
  244. return -1;
  245. if (!iov)
  246. return -1;
  247. if (iovcnt == 0)
  248. return 0;
  249. for (i = 0; i < iovcnt; i++) {
  250. ssize_t bytes_written = 0;
  251. if (iov[i].iov_len == 0)
  252. continue;
  253. bytes_written = faux_buf_write(async->obuf,
  254. iov[i].iov_base, iov[i].iov_len);
  255. if (bytes_written < 0) { // Error
  256. if (total_written != 0)
  257. break;
  258. return -1;
  259. }
  260. if (0 == bytes_written) // Insufficient space
  261. break;
  262. total_written += bytes_written;
  263. }
  264. // Try to real write data to fd in nonblocked mode
  265. if (total_written > 0)
  266. faux_async_out(async);
  267. return total_written;
  268. }
  269. /** @brief Write output buffer to fd in non-blocking mode.
  270. *
  271. * Previously data must be written to internal buffer by faux_async_write()
  272. * function. But some data can be left within internal buffer because can't be
  273. * written to fd in non-blocking mode. This function tries to write the rest of
  274. * data to fd in non-blocking mode. So function doesn't block. It can be called
  275. * after select() or poll() if fd is ready to be written to. If function can't
  276. * to write all buffer to fd it executes "stall" callback to inform about it.
  277. *
  278. * @param [in] async Allocated and initialized async I/O object.
  279. * @return Length of data actually written or < 0 on error.
  280. */
  281. ssize_t faux_async_out(faux_async_t *async)
  282. {
  283. ssize_t total_written = 0;
  284. assert(async);
  285. if (!async)
  286. return -1;
  287. while (faux_buf_len(async->obuf) > 0) {
  288. ssize_t data_to_write = 0;
  289. ssize_t bytes_written = 0;
  290. bool_t postpone = BOOL_FALSE;
  291. void *data = NULL;
  292. data_to_write = faux_buf_dread_lock_easy(async->obuf, &data);
  293. if (data_to_write <= 0)
  294. return -1;
  295. bytes_written = write(async->fd, data, data_to_write);
  296. if (bytes_written > 0) {
  297. total_written += bytes_written;
  298. faux_buf_dread_unlock_easy(async->obuf, bytes_written);
  299. } else {
  300. faux_buf_dread_unlock_easy(async->obuf, 0);
  301. }
  302. if (bytes_written < 0) {
  303. if ( // Something went wrong
  304. (errno != EINTR) &&
  305. (errno != EAGAIN) &&
  306. (errno != EWOULDBLOCK)
  307. )
  308. return -1;
  309. // Postpone next read
  310. postpone = BOOL_TRUE;
  311. // Not whole data block was written
  312. } else if (bytes_written != data_to_write) {
  313. // Postpone next read
  314. postpone = BOOL_TRUE;
  315. }
  316. // Postponed
  317. if (postpone) {
  318. // Execute callback
  319. if (async->stall_cb)
  320. async->stall_cb(async,
  321. faux_buf_len(async->obuf),
  322. async->stall_udata);
  323. break;
  324. }
  325. }
  326. return total_written;
  327. }
  328. /** @brief Read data and store it to internal buffer in non-blocking mode.
  329. *
  330. * Reads fd and puts data to internal buffer. It can't be blocked. If length of
  331. * data stored within internal buffer is greater or equal than "min" limit then
  332. * function will execute "read" callback. It gives faux_buf_t object to callback.
  333. * If "max" limit is "0"
  334. * (it means indefinite) then function will pass all available data to callback.
  335. *
  336. * @param [in] async Allocated and initialized async I/O object.
  337. * @return Length of data actually readed or < 0 on error.
  338. */
  339. ssize_t faux_async_in(faux_async_t *async)
  340. {
  341. ssize_t total_readed = 0;
  342. ssize_t bytes_readed = 0;
  343. ssize_t locked_len = 0;
  344. assert(async);
  345. if (!async)
  346. return -1;
  347. do {
  348. void *data = NULL;
  349. size_t bytes_stored = 0;
  350. locked_len = faux_buf_dwrite_lock_easy(async->ibuf, &data);
  351. if (locked_len <= 0)
  352. return -1;
  353. // Read data
  354. bytes_readed = read(async->fd, data, locked_len);
  355. if (bytes_readed < 0) {
  356. if ( // Something went wrong
  357. (errno != EINTR) &&
  358. (errno != EAGAIN) &&
  359. (errno != EWOULDBLOCK)
  360. ) {
  361. faux_buf_dwrite_unlock_easy(async->ibuf, 0);
  362. return -1;
  363. }
  364. }
  365. faux_buf_dwrite_unlock_easy(async->ibuf, bytes_readed);
  366. total_readed += bytes_readed;
  367. if (!async->read_cb) // No read callback
  368. continue;
  369. // Check for amount of stored data
  370. while ((bytes_stored = faux_buf_len(async->ibuf)) >= async->min) {
  371. size_t copy_len = 0;
  372. // Calculate length of user-requested block
  373. if (FAUX_ASYNC_UNLIMITED == async->max) { // Indefinite
  374. copy_len = bytes_stored; // Take all data
  375. } else {
  376. copy_len = (bytes_stored < async->max) ?
  377. bytes_stored : async->max;
  378. }
  379. // Execute callback
  380. async->read_cb(async, async->ibuf,
  381. copy_len, async->read_udata);
  382. }
  383. } while (bytes_readed == locked_len);
  384. return total_readed;
  385. }