async.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /** @file async.c
  2. */
  3. #ifdef HAVE_CONFIG_H
  4. #include "config.h"
  5. #endif /* HAVE_CONFIG_H */
  6. #include <stdlib.h>
  7. #include <stdint.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <unistd.h>
  12. #include <errno.h>
  13. #include <sys/types.h>
  14. #include <sys/stat.h>
  15. #include <fcntl.h>
  16. #include "faux/faux.h"
  17. #include "faux/str.h"
  18. #include "faux/net.h"
  19. #include "faux/async.h"
  20. #include "private.h"
  21. #define DATA_CHUNK 4096
  22. /** @brief Create new async I/O object.
  23. *
  24. * Constructor gets associated file descriptor to operate on it. File
  25. * descriptor must be nonblocked. If not so then constructor will set
  26. * nonblock flag itself.
  27. *
  28. * @param [in] fd File descriptor.
  29. * @return Allocated object or NULL on error.
  30. */
  31. faux_async_t *faux_async_new(int fd)
  32. {
  33. faux_async_t *async = NULL;
  34. int fflags = 0;
  35. // Prepare FD
  36. if (fd < 0) // Illegal fd
  37. return NULL;
  38. if ((fflags = fcntl(fd, F_GETFL)) == -1)
  39. return NULL;
  40. if (fcntl(fd, F_SETFL, fflags | O_NONBLOCK) == -1)
  41. return NULL;
  42. async = faux_zmalloc(sizeof(*async));
  43. assert(async);
  44. if (!async)
  45. return NULL;
  46. // Init
  47. async->fd = fd;
  48. // Read (Input)
  49. async->read_cb = NULL;
  50. async->read_udata = NULL;
  51. async->min = 1;
  52. async->max = 0; // Indefinite
  53. async->i_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  54. NULL, NULL, faux_free);
  55. async->i_pos = 0;
  56. // Write (Output)
  57. async->stall_cb = NULL;
  58. async->stall_udata = NULL;
  59. async->overflow = 10000000l; // ~ 10M
  60. async->o_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  61. NULL, NULL, faux_free);
  62. async->o_pos = 0;
  63. return async;
  64. }
  65. /** @brief Free async I/O object.
  66. *
  67. * @param [in] Async I/O object.
  68. */
  69. void faux_async_free(faux_async_t *async)
  70. {
  71. if (!async)
  72. return;
  73. faux_list_free(async->i_list);
  74. faux_list_free(async->o_list);
  75. faux_free(async);
  76. }
  77. /** @brief Get file descriptor from async I/O object.
  78. *
  79. * @param [in] async Allocated and initialized async I/O object.
  80. * @return Serviced file descriptor.
  81. */
  82. int faux_async_fd(const faux_async_t *async)
  83. {
  84. assert(async);
  85. if (!async)
  86. return -1;
  87. return async->fd;
  88. }
  89. /** @brief Set read callback and associated user data.
  90. *
  91. * If callback function pointer is NULL then class will drop all readed data.
  92. *
  93. * @param [in] async Allocated and initialized async I/O object.
  94. * @param [in] read_cb Read callback.
  95. * @param [in] user_data Associated user data.
  96. */
  97. void faux_async_set_read_cb(faux_async_t *async,
  98. faux_async_read_cb_f read_cb, void *user_data)
  99. {
  100. assert(async);
  101. if (!async)
  102. return;
  103. async->read_cb = read_cb;
  104. async->read_udata = user_data;
  105. }
  106. /** @brief Set read limits.
  107. *
  108. * Read limits define conditions when the read callback will be executed.
  109. * Buffer must contain data amount greater or equal to "min" value. Callback
  110. * will not get data amount greater than "max" value. If min == max then
  111. * callback will be executed with fixed data size. The "max" value can be "0".
  112. * It means indefinite i.e. data transferred to callback can be really large.
  113. *
  114. * @param [in] async Allocated and initialized async I/O object.
  115. * @param [in] min Minimal data amount.
  116. * @param [in] max Maximal data amount. The "0" means indefinite.
  117. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  118. */
  119. bool_t faux_async_set_read_limits(faux_async_t *async, size_t min, size_t max)
  120. {
  121. assert(async);
  122. if (!async)
  123. return BOOL_FALSE;
  124. if (min < 1)
  125. return BOOL_FALSE;
  126. if ((min > max) && (max != 0))
  127. return BOOL_FALSE;
  128. async->min = min;
  129. async->max = max;
  130. return BOOL_TRUE;
  131. }
  132. /** @brief Set stall callback and associated user data.
  133. *
  134. * @param [in] async Allocated and initialized async I/O object.
  135. * @param [in] stall_cb Stall callback.
  136. * @param [in] user_data Associated user data.
  137. */
  138. void faux_async_set_stall_cb(faux_async_t *async,
  139. faux_async_stall_cb_f stall_cb, void *user_data)
  140. {
  141. assert(async);
  142. if (!async)
  143. return;
  144. async->stall_cb = stall_cb;
  145. async->stall_udata = user_data;
  146. }
  147. /** @brief Set overflow value.
  148. *
  149. * "Overflow" is a value when engine consider data consumer as a stalled.
  150. * Data gets into the async I/O object buffer but object can't write it to
  151. * serviced fd for too long time. So it accumulates great amount of data.
  152. *
  153. * @param [in] async Allocated and initialized async I/O object.
  154. * @param [in] overflow Overflow value.
  155. */
  156. void faux_async_set_overflow(faux_async_t *async, size_t overflow)
  157. {
  158. assert(async);
  159. if (!async)
  160. return;
  161. async->overflow = overflow;
  162. }