async.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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 Get input buffer from async I/O object.
  109. *
  110. * @param [in] async Allocated and initialized async I/O object.
  111. * @return faux_buf_t object.
  112. */
  113. faux_buf_t *faux_async_ibuf(const faux_async_t *async)
  114. {
  115. assert(async);
  116. if (!async)
  117. return NULL;
  118. return async->ibuf;
  119. }
  120. /** @brief Get output buffer from async I/O object.
  121. *
  122. * @param [in] async Allocated and initialized async I/O object.
  123. * @return faux_buf_t object.
  124. */
  125. faux_buf_t *faux_async_obuf(const faux_async_t *async)
  126. {
  127. assert(async);
  128. if (!async)
  129. return NULL;
  130. return async->obuf;
  131. }
  132. /** @brief Set read callback and associated user data.
  133. *
  134. * If callback function pointer is NULL then class will drop all readed data.
  135. *
  136. * @param [in] async Allocated and initialized async I/O object.
  137. * @param [in] read_cb Read callback.
  138. * @param [in] user_data Associated user data.
  139. */
  140. void faux_async_set_read_cb(faux_async_t *async,
  141. faux_async_read_cb_fn read_cb, void *user_data)
  142. {
  143. assert(async);
  144. if (!async)
  145. return;
  146. async->read_cb = read_cb;
  147. async->read_udata = user_data;
  148. }
  149. /** @brief Set read limits.
  150. *
  151. * Read limits define conditions when the read callback will be executed.
  152. * Buffer must contain data amount greater or equal to "min" value. Callback
  153. * will not get data amount greater than "max" value. If min == max then
  154. * callback will be executed with fixed data size. The "max" value can be "0".
  155. * It means indefinite i.e. data transferred to callback can be really large.
  156. *
  157. * @param [in] async Allocated and initialized async I/O object.
  158. * @param [in] min Minimal data amount.
  159. * @param [in] max Maximal data amount. The "0" means indefinite.
  160. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  161. */
  162. bool_t faux_async_set_read_limits(faux_async_t *async, size_t min, size_t max)
  163. {
  164. assert(async);
  165. if (!async)
  166. return BOOL_FALSE;
  167. if (min < 1)
  168. return BOOL_FALSE;
  169. if ((min > max) && (max != 0))
  170. return BOOL_FALSE;
  171. async->min = min;
  172. async->max = max;
  173. return BOOL_TRUE;
  174. }
  175. /** @brief Set stall callback and associated user data.
  176. *
  177. * @param [in] async Allocated and initialized async I/O object.
  178. * @param [in] stall_cb Stall callback.
  179. * @param [in] user_data Associated user data.
  180. */
  181. void faux_async_set_stall_cb(faux_async_t *async,
  182. faux_async_stall_cb_fn stall_cb, void *user_data)
  183. {
  184. assert(async);
  185. if (!async)
  186. return;
  187. async->stall_cb = stall_cb;
  188. async->stall_udata = user_data;
  189. }
  190. /** @brief Set write overflow value.
  191. *
  192. * "Overflow" is a value when engine consider data consumer as a stalled.
  193. * Data gets into the async I/O object buffer but object can't write it to
  194. * serviced fd for too long time. So it accumulates great amount of data.
  195. *
  196. * @param [in] async Allocated and initialized async I/O object.
  197. * @param [in] overflow Overflow value.
  198. */
  199. void faux_async_set_write_overflow(faux_async_t *async, size_t overflow)
  200. {
  201. assert(async);
  202. if (!async)
  203. return;
  204. faux_buf_set_limit(async->obuf, overflow);
  205. }
  206. /** @brief Set read overflow value.
  207. *
  208. * "Overflow" is a value when engine consider data consumer as a stalled.
  209. * Data gets into the async I/O object buffer but object can't write it to
  210. * serviced fd for too long time. So it accumulates great amount of data.
  211. *
  212. * @param [in] async Allocated and initialized async I/O object.
  213. * @param [in] overflow Overflow value.
  214. */
  215. void faux_async_set_read_overflow(faux_async_t *async, size_t overflow)
  216. {
  217. assert(async);
  218. if (!async)
  219. return;
  220. faux_buf_set_limit(async->ibuf, overflow);
  221. }
  222. /** @brief Async data write.
  223. *
  224. * All given data will be stored to internal buffer (list of data chunks).
  225. * Then function will try to write stored data to file descriptor in
  226. * non-blocking mode. Note some data can be left within buffer. In this case
  227. * the "stall" callback will be executed to inform about it. To try to write
  228. * the rest of the data user can be call faux_async_out() function. Both
  229. * functions will not block.
  230. *
  231. * @param [in] async Allocated and initialized async I/O object.
  232. * @param [in] data Data buffer to write.
  233. * @param [in] len Data length to write.
  234. * @return Length of stored/writed data or < 0 on error.
  235. */
  236. ssize_t faux_async_write(faux_async_t *async, void *data, size_t len)
  237. {
  238. ssize_t data_written = len;
  239. assert(async);
  240. if (!async)
  241. return -1;
  242. assert(data);
  243. if (!data)
  244. return -1;
  245. data_written = faux_buf_write(async->obuf, data, len);
  246. if (data_written < 0)
  247. return -1;
  248. // Try to real write data to fd in nonblocked mode
  249. faux_async_out(async);
  250. return len;
  251. }
  252. /** @brief Async "struct iovec" write.
  253. *
  254. * This function is like a faux_async_write() function but uses scatter/gather.
  255. *
  256. * @see faux_async_write().
  257. * @param [in] async Allocated and initialized async I/O object.
  258. * @param [in] iov Array of "struct iovec" structures.
  259. * @param [in] iovcnt Number of iov array members.
  260. * @return Length of stored/writed data or < 0 on error.
  261. */
  262. ssize_t faux_async_writev(faux_async_t *async,
  263. const struct iovec *iov, int iovcnt)
  264. {
  265. size_t total_written = 0;
  266. int i = 0;
  267. assert(async);
  268. if (!async)
  269. return -1;
  270. if (!iov)
  271. return -1;
  272. if (iovcnt == 0)
  273. return 0;
  274. for (i = 0; i < iovcnt; i++) {
  275. ssize_t bytes_written = 0;
  276. if (iov[i].iov_len == 0)
  277. continue;
  278. bytes_written = faux_buf_write(async->obuf,
  279. iov[i].iov_base, iov[i].iov_len);
  280. if (bytes_written < 0) { // Error
  281. if (total_written != 0)
  282. break;
  283. return -1;
  284. }
  285. if (0 == bytes_written) // Insufficient space
  286. break;
  287. total_written += bytes_written;
  288. }
  289. // Try to real write data to fd in nonblocked mode
  290. if (total_written > 0)
  291. faux_async_out(async);
  292. return total_written;
  293. }
  294. /** @brief Write output buffer to fd in non-blocking mode.
  295. *
  296. * Previously data must be written to internal buffer by faux_async_write()
  297. * function. But some data can be left within internal buffer because can't be
  298. * written to fd in non-blocking mode. This function tries to write the rest of
  299. * data to fd in non-blocking mode. So function doesn't block. It can be called
  300. * after select() or poll() if fd is ready to be written to. If function can't
  301. * to write all buffer to fd it executes "stall" callback to inform about it.
  302. *
  303. * @param [in] async Allocated and initialized async I/O object.
  304. * @return Length of data actually written or < 0 on error.
  305. */
  306. static ssize_t faux_async_out_internal(faux_async_t *async,
  307. bool_t process_all_data)
  308. {
  309. ssize_t total_written = 0;
  310. assert(async);
  311. if (!async)
  312. return -1;
  313. while (faux_buf_len(async->obuf) > 0) {
  314. ssize_t data_to_write = 0;
  315. ssize_t bytes_written = 0;
  316. bool_t postpone = BOOL_FALSE;
  317. void *data = NULL;
  318. data_to_write = faux_buf_dread_lock_easy(async->obuf, &data);
  319. if (data_to_write <= 0)
  320. return -1;
  321. bytes_written = write(async->fd, data, data_to_write);
  322. if (bytes_written > 0) {
  323. total_written += bytes_written;
  324. faux_buf_dread_unlock_easy(async->obuf, bytes_written);
  325. } else {
  326. faux_buf_dread_unlock_easy(async->obuf, 0);
  327. }
  328. if (bytes_written < 0) {
  329. if ( // Something went wrong
  330. (errno != EINTR) &&
  331. (errno != EAGAIN) &&
  332. (errno != EWOULDBLOCK)
  333. )
  334. return -1;
  335. // Postpone next read
  336. postpone = BOOL_TRUE;
  337. // Not whole data block was written
  338. } else if (bytes_written != data_to_write) {
  339. // Postpone next read
  340. postpone = BOOL_TRUE;
  341. // Write only one data block and buffer is not empty
  342. // Programm can be more responsive if to write only one data
  343. // block and then allow other events to be processed
  344. } else if (!process_all_data && (faux_buf_len(async->obuf) > 0)) {
  345. postpone = BOOL_TRUE;
  346. }
  347. // Postponed
  348. if (postpone) {
  349. // Execute callback
  350. if (async->stall_cb)
  351. async->stall_cb(async,
  352. faux_buf_len(async->obuf),
  353. async->stall_udata);
  354. break;
  355. }
  356. }
  357. return total_written;
  358. }
  359. ssize_t faux_async_out(faux_async_t *async)
  360. {
  361. return faux_async_out_internal(async, BOOL_TRUE);
  362. }
  363. ssize_t faux_async_out_easy(faux_async_t *async)
  364. {
  365. return faux_async_out_internal(async, BOOL_FALSE);
  366. }
  367. /** @brief Read data and store it to internal buffer in non-blocking mode.
  368. *
  369. * Reads fd and puts data to internal buffer. It can't be blocked. If length of
  370. * data stored within internal buffer is greater or equal than "min" limit then
  371. * function will execute "read" callback. It gives faux_buf_t object to callback.
  372. * If "max" limit is "0"
  373. * (it means indefinite) then function will pass all available data to callback.
  374. *
  375. * @param [in] async Allocated and initialized async I/O object.
  376. * @return Length of data actually readed or < 0 on error.
  377. */
  378. static ssize_t faux_async_in_internal(faux_async_t *async,
  379. bool_t process_all_data)
  380. {
  381. ssize_t total_readed = 0;
  382. ssize_t bytes_readed = 0;
  383. ssize_t locked_len = 0;
  384. assert(async);
  385. if (!async)
  386. return -1;
  387. do {
  388. void *data = NULL;
  389. size_t bytes_stored = 0;
  390. locked_len = faux_buf_dwrite_lock_easy(async->ibuf, &data);
  391. if (locked_len <= 0)
  392. break; // May be buffer is full
  393. // Read data
  394. bytes_readed = read(async->fd, data, locked_len);
  395. if (bytes_readed < 0) {
  396. faux_buf_dwrite_unlock_easy(async->ibuf, 0);
  397. if ( // Something went wrong
  398. (errno != EINTR) &&
  399. (errno != EAGAIN) &&
  400. (errno != EWOULDBLOCK)
  401. )
  402. return -1;
  403. break;
  404. }
  405. faux_buf_dwrite_unlock_easy(async->ibuf, bytes_readed);
  406. total_readed += bytes_readed;
  407. if (!async->read_cb) // No read callback
  408. continue;
  409. // Check for amount of stored data
  410. while ((bytes_stored = faux_buf_len(async->ibuf)) >= async->min) {
  411. size_t copy_len = 0;
  412. // Calculate length of user-requested block
  413. if (FAUX_ASYNC_UNLIMITED == async->max) { // Indefinite
  414. copy_len = bytes_stored; // Take all data
  415. } else {
  416. copy_len = (bytes_stored < async->max) ?
  417. bytes_stored : async->max;
  418. }
  419. // Execute callback
  420. async->read_cb(async, async->ibuf,
  421. copy_len, async->read_udata);
  422. }
  423. } while ((bytes_readed == locked_len) && process_all_data);
  424. return total_readed;
  425. }
  426. ssize_t faux_async_in(faux_async_t *async)
  427. {
  428. return faux_async_in_internal(async, BOOL_TRUE);
  429. }
  430. ssize_t faux_async_in_easy(faux_async_t *async)
  431. {
  432. return faux_async_in_internal(async, BOOL_FALSE);
  433. }