async.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 <syslog.h>
  38. #include "faux/faux.h"
  39. #include "faux/str.h"
  40. #include "faux/buf.h"
  41. #include "faux/net.h"
  42. #include "faux/async.h"
  43. #include "private.h"
  44. /** @brief Create new async I/O object.
  45. *
  46. * Constructor gets associated file descriptor to operate on it. File
  47. * descriptor must be nonblocked. If not so then constructor will set
  48. * nonblock flag itself.
  49. *
  50. * @param [in] fd File descriptor.
  51. * @return Allocated object or NULL on error.
  52. */
  53. faux_async_t *faux_async_new(int fd)
  54. {
  55. faux_async_t *async = NULL;
  56. int fflags = 0;
  57. // Prepare FD
  58. if (fd < 0) // Illegal fd
  59. return NULL;
  60. if ((fflags = fcntl(fd, F_GETFL)) == -1)
  61. return NULL;
  62. if (fcntl(fd, F_SETFL, fflags | O_NONBLOCK) == -1)
  63. return NULL;
  64. async = faux_zmalloc(sizeof(*async));
  65. assert(async);
  66. if (!async)
  67. return NULL;
  68. // Init
  69. async->fd = fd;
  70. // Read (Input)
  71. async->read_cb = NULL;
  72. async->read_udata = NULL;
  73. async->min = 1;
  74. async->max = FAUX_ASYNC_UNLIMITED;
  75. async->ibuf = faux_buf_new(DATA_CHUNK);
  76. faux_buf_set_limit(async->ibuf, FAUX_ASYNC_IN_OVERFLOW);
  77. // Write (Output)
  78. async->stall_cb = NULL;
  79. async->stall_udata = NULL;
  80. async->obuf = faux_buf_new(DATA_CHUNK);
  81. faux_buf_set_limit(async->obuf, FAUX_ASYNC_OUT_OVERFLOW);
  82. return async;
  83. }
  84. /** @brief Free async I/O object.
  85. *
  86. * @param [in] Async I/O object.
  87. */
  88. void faux_async_free(faux_async_t *async)
  89. {
  90. if (!async)
  91. return;
  92. faux_buf_free(async->ibuf);
  93. faux_buf_free(async->obuf);
  94. faux_free(async);
  95. }
  96. /** @brief Get file descriptor from async I/O object.
  97. *
  98. * @param [in] async Allocated and initialized async I/O object.
  99. * @return Serviced file descriptor.
  100. */
  101. int faux_async_fd(const faux_async_t *async)
  102. {
  103. assert(async);
  104. if (!async)
  105. return -1;
  106. return async->fd;
  107. }
  108. /** @brief Set read callback and associated user data.
  109. *
  110. * If callback function pointer is NULL then class will drop all readed data.
  111. *
  112. * @param [in] async Allocated and initialized async I/O object.
  113. * @param [in] read_cb Read callback.
  114. * @param [in] user_data Associated user data.
  115. */
  116. void faux_async_set_read_cb(faux_async_t *async,
  117. faux_async_read_cb_fn read_cb, void *user_data)
  118. {
  119. assert(async);
  120. if (!async)
  121. return;
  122. async->read_cb = read_cb;
  123. async->read_udata = user_data;
  124. }
  125. /** @brief Set read limits.
  126. *
  127. * Read limits define conditions when the read callback will be executed.
  128. * Buffer must contain data amount greater or equal to "min" value. Callback
  129. * will not get data amount greater than "max" value. If min == max then
  130. * callback will be executed with fixed data size. The "max" value can be "0".
  131. * It means indefinite i.e. data transferred to callback can be really large.
  132. *
  133. * @param [in] async Allocated and initialized async I/O object.
  134. * @param [in] min Minimal data amount.
  135. * @param [in] max Maximal data amount. The "0" means indefinite.
  136. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  137. */
  138. bool_t faux_async_set_read_limits(faux_async_t *async, size_t min, size_t max)
  139. {
  140. assert(async);
  141. if (!async)
  142. return BOOL_FALSE;
  143. if (min < 1)
  144. return BOOL_FALSE;
  145. if ((min > max) && (max != 0))
  146. return BOOL_FALSE;
  147. async->min = min;
  148. async->max = max;
  149. return BOOL_TRUE;
  150. }
  151. /** @brief Set stall callback and associated user data.
  152. *
  153. * @param [in] async Allocated and initialized async I/O object.
  154. * @param [in] stall_cb Stall callback.
  155. * @param [in] user_data Associated user data.
  156. */
  157. void faux_async_set_stall_cb(faux_async_t *async,
  158. faux_async_stall_cb_fn stall_cb, void *user_data)
  159. {
  160. assert(async);
  161. if (!async)
  162. return;
  163. async->stall_cb = stall_cb;
  164. async->stall_udata = user_data;
  165. }
  166. /** @brief Set write overflow value.
  167. *
  168. * "Overflow" is a value when engine consider data consumer as a stalled.
  169. * Data gets into the async I/O object buffer but object can't write it to
  170. * serviced fd for too long time. So it accumulates great amount of data.
  171. *
  172. * @param [in] async Allocated and initialized async I/O object.
  173. * @param [in] overflow Overflow value.
  174. */
  175. void faux_async_set_write_overflow(faux_async_t *async, size_t overflow)
  176. {
  177. assert(async);
  178. if (!async)
  179. return;
  180. faux_buf_set_limit(async->obuf, overflow);
  181. }
  182. /** @brief Set read overflow value.
  183. *
  184. * "Overflow" is a value when engine consider data consumer as a stalled.
  185. * Data gets into the async I/O object buffer but object can't write it to
  186. * serviced fd for too long time. So it accumulates great amount of data.
  187. *
  188. * @param [in] async Allocated and initialized async I/O object.
  189. * @param [in] overflow Overflow value.
  190. */
  191. void faux_async_set_read_overflow(faux_async_t *async, size_t overflow)
  192. {
  193. assert(async);
  194. if (!async)
  195. return;
  196. faux_buf_set_limit(async->ibuf, overflow);
  197. }
  198. /** @brief Async data write.
  199. *
  200. * All given data will be stored to internal buffer (list of data chunks).
  201. * Then function will try to write stored data to file descriptor in
  202. * non-blocking mode. Note some data can be left within buffer. In this case
  203. * the "stall" callback will be executed to inform about it. To try to write
  204. * the rest of the data user can be call faux_async_out() function. Both
  205. * functions will not block.
  206. *
  207. * @param [in] async Allocated and initialized async I/O object.
  208. * @param [in] data Data buffer to write.
  209. * @param [in] len Data length to write.
  210. * @return Length of stored/writed data or < 0 on error.
  211. */
  212. ssize_t faux_async_write(faux_async_t *async, void *data, size_t len)
  213. {
  214. ssize_t data_written = len;
  215. assert(async);
  216. if (!async)
  217. return -1;
  218. assert(data);
  219. if (!data)
  220. return -1;
  221. data_written = faux_buf_write(async->obuf, data, len);
  222. if (data_written < 0)
  223. return -1;
  224. // Try to real write data to fd in nonblocked mode
  225. faux_async_out(async);
  226. return len;
  227. }
  228. /** @brief Async "struct iovec" write.
  229. *
  230. * This function is like a faux_async_write() function but uses scatter/gather.
  231. *
  232. * @see faux_async_write().
  233. * @param [in] async Allocated and initialized async I/O object.
  234. * @param [in] iov Array of "struct iovec" structures.
  235. * @param [in] iovcnt Number of iov array members.
  236. * @return Length of stored/writed data or < 0 on error.
  237. */
  238. ssize_t faux_async_writev(faux_async_t *async,
  239. const struct iovec *iov, int iovcnt)
  240. {
  241. size_t total_written = 0;
  242. int i = 0;
  243. assert(async);
  244. if (!async)
  245. return -1;
  246. if (!iov)
  247. return -1;
  248. if (iovcnt == 0)
  249. return 0;
  250. for (i = 0; i < iovcnt; i++) {
  251. ssize_t bytes_written = 0;
  252. if (iov[i].iov_len == 0)
  253. continue;
  254. bytes_written = faux_buf_write(async->obuf,
  255. iov[i].iov_base, iov[i].iov_len);
  256. if (bytes_written < 0) { // Error
  257. if (total_written != 0)
  258. break;
  259. return -1;
  260. }
  261. if (0 == bytes_written) // Insufficient space
  262. break;
  263. total_written += bytes_written;
  264. }
  265. // Try to real write data to fd in nonblocked mode
  266. if (total_written > 0)
  267. faux_async_out(async);
  268. return total_written;
  269. }
  270. /** @brief Write output buffer to fd in non-blocking mode.
  271. *
  272. * Previously data must be written to internal buffer by faux_async_write()
  273. * function. But some data can be left within internal buffer because can't be
  274. * written to fd in non-blocking mode. This function tries to write the rest of
  275. * data to fd in non-blocking mode. So function doesn't block. It can be called
  276. * after select() or poll() if fd is ready to be written to. If function can't
  277. * to write all buffer to fd it executes "stall" callback to inform about it.
  278. *
  279. * @param [in] async Allocated and initialized async I/O object.
  280. * @return Length of data actually written or < 0 on error.
  281. */
  282. ssize_t faux_async_out(faux_async_t *async)
  283. {
  284. ssize_t total_written = 0;
  285. assert(async);
  286. if (!async)
  287. return -1;
  288. while (faux_buf_len(async->obuf) > 0) {
  289. ssize_t data_to_write = 0;
  290. ssize_t bytes_written = 0;
  291. bool_t postpone = BOOL_FALSE;
  292. void *data = NULL;
  293. data_to_write = faux_buf_dread_lock_easy(async->obuf, &data);
  294. if (data_to_write <= 0)
  295. return -1;
  296. bytes_written = write(async->fd, data, data_to_write);
  297. if (bytes_written > 0) {
  298. total_written += bytes_written;
  299. faux_buf_dread_unlock_easy(async->obuf, bytes_written);
  300. } else {
  301. faux_buf_dread_unlock_easy(async->obuf, 0);
  302. }
  303. if (bytes_written < 0) {
  304. if ( // Something went wrong
  305. (errno != EINTR) &&
  306. (errno != EAGAIN) &&
  307. (errno != EWOULDBLOCK)
  308. )
  309. return -1;
  310. // Postpone next read
  311. postpone = BOOL_TRUE;
  312. // Not whole data block was written
  313. } else if (bytes_written != data_to_write) {
  314. // Postpone next read
  315. postpone = BOOL_TRUE;
  316. }
  317. // Postponed
  318. if (postpone) {
  319. // Execute callback
  320. if (async->stall_cb)
  321. async->stall_cb(async,
  322. faux_buf_len(async->obuf),
  323. async->stall_udata);
  324. break;
  325. }
  326. }
  327. return total_written;
  328. }
  329. /** @brief Read data and store it to internal buffer in non-blocking mode.
  330. *
  331. * Reads fd and puts data to internal buffer. It can't be blocked. If length of
  332. * data stored within internal buffer is greater or equal than "min" limit then
  333. * function will execute "read" callback. It gives faux_buf_t object to callback.
  334. * If "max" limit is "0"
  335. * (it means indefinite) then function will pass all available data to callback.
  336. *
  337. * @param [in] async Allocated and initialized async I/O object.
  338. * @return Length of data actually readed or < 0 on error.
  339. */
  340. ssize_t faux_async_in(faux_async_t *async)
  341. {
  342. ssize_t total_readed = 0;
  343. ssize_t bytes_readed = 0;
  344. ssize_t locked_len = 0;
  345. assert(async);
  346. if (!async)
  347. return -1;
  348. do {
  349. void *data = NULL;
  350. size_t bytes_stored = 0;
  351. locked_len = faux_buf_dwrite_lock_easy(async->ibuf, &data);
  352. if (locked_len <= 0)
  353. return -1;
  354. // Read data
  355. bytes_readed = read(async->fd, data, locked_len);
  356. if (bytes_readed < 0) {
  357. faux_buf_dwrite_unlock_easy(async->ibuf, 0);
  358. if ( // Something went wrong
  359. (errno != EINTR) &&
  360. (errno != EAGAIN) &&
  361. (errno != EWOULDBLOCK)
  362. )
  363. return -1;
  364. break;
  365. }
  366. faux_buf_dwrite_unlock_easy(async->ibuf, bytes_readed);
  367. total_readed += bytes_readed;
  368. if (!async->read_cb) // No read callback
  369. continue;
  370. // Check for amount of stored data
  371. while ((bytes_stored = faux_buf_len(async->ibuf)) >= async->min) {
  372. size_t copy_len = 0;
  373. // Calculate length of user-requested block
  374. if (FAUX_ASYNC_UNLIMITED == async->max) { // Indefinite
  375. copy_len = bytes_stored; // Take all data
  376. } else {
  377. copy_len = (bytes_stored < async->max) ?
  378. bytes_stored : async->max;
  379. }
  380. // Execute callback
  381. async->read_cb(async, async->ibuf,
  382. copy_len, async->read_udata);
  383. }
  384. } while (bytes_readed == locked_len);
  385. return total_readed;
  386. }