hloop.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. #ifndef HV_LOOP_H_
  2. #define HV_LOOP_H_
  3. #include "hexport.h"
  4. #include "hplatform.h"
  5. #include "hdef.h"
  6. #include "hssl.h"
  7. typedef struct hloop_s hloop_t;
  8. typedef struct hevent_s hevent_t;
  9. // NOTE: The following structures are subclasses of hevent_t,
  10. // inheriting hevent_t data members and function members.
  11. typedef struct hidle_s hidle_t;
  12. typedef struct htimer_s htimer_t;
  13. typedef struct htimeout_s htimeout_t;
  14. typedef struct hperiod_s hperiod_t;
  15. typedef struct hio_s hio_t;
  16. typedef void (*hevent_cb) (hevent_t* ev);
  17. typedef void (*hidle_cb) (hidle_t* idle);
  18. typedef void (*htimer_cb) (htimer_t* timer);
  19. typedef void (*hio_cb) (hio_t* io);
  20. typedef void (*haccept_cb) (hio_t* io);
  21. typedef void (*hconnect_cb) (hio_t* io);
  22. typedef void (*hread_cb) (hio_t* io, void* buf, int readbytes);
  23. typedef void (*hwrite_cb) (hio_t* io, const void* buf, int writebytes);
  24. typedef void (*hclose_cb) (hio_t* io);
  25. typedef enum {
  26. HLOOP_STATUS_STOP,
  27. HLOOP_STATUS_RUNNING,
  28. HLOOP_STATUS_PAUSE
  29. } hloop_status_e;
  30. typedef enum {
  31. HEVENT_TYPE_NONE = 0,
  32. HEVENT_TYPE_IO = 0x00000001,
  33. HEVENT_TYPE_TIMEOUT = 0x00000010,
  34. HEVENT_TYPE_PERIOD = 0x00000020,
  35. HEVENT_TYPE_TIMER = HEVENT_TYPE_TIMEOUT|HEVENT_TYPE_PERIOD,
  36. HEVENT_TYPE_IDLE = 0x00000100,
  37. HEVENT_TYPE_CUSTOM = 0x00000400, // 1024
  38. } hevent_type_e;
  39. #define HEVENT_LOWEST_PRIORITY (-5)
  40. #define HEVENT_LOW_PRIORITY (-3)
  41. #define HEVENT_NORMAL_PRIORITY 0
  42. #define HEVENT_HIGH_PRIORITY 3
  43. #define HEVENT_HIGHEST_PRIORITY 5
  44. #define HEVENT_PRIORITY_SIZE (HEVENT_HIGHEST_PRIORITY-HEVENT_LOWEST_PRIORITY+1)
  45. #define HEVENT_PRIORITY_INDEX(priority) (priority-HEVENT_LOWEST_PRIORITY)
  46. #define HEVENT_FLAGS \
  47. unsigned destroy :1; \
  48. unsigned active :1; \
  49. unsigned pending :1;
  50. #define HEVENT_FIELDS \
  51. hloop_t* loop; \
  52. hevent_type_e event_type; \
  53. uint64_t event_id; \
  54. hevent_cb cb; \
  55. void* userdata; \
  56. void* privdata; \
  57. struct hevent_s* pending_next; \
  58. int priority; \
  59. HEVENT_FLAGS
  60. // sizeof(struct hevent_s)=64 on x64
  61. struct hevent_s {
  62. HEVENT_FIELDS
  63. };
  64. #define hevent_set_priority(ev, prio) ((hevent_t*)(ev))->priority = prio
  65. #define hevent_set_userdata(ev, udata) ((hevent_t*)(ev))->userdata = (void*)udata
  66. #define hevent_loop(ev) (((hevent_t*)(ev))->loop)
  67. #define hevent_type(ev) (((hevent_t*)(ev))->event_type)
  68. #define hevent_id(ev) (((hevent_t*)(ev))->event_id)
  69. #define hevent_priority(ev) (((hevent_t*)(ev))->priority)
  70. #define hevent_userdata(ev) (((hevent_t*)(ev))->userdata)
  71. typedef enum {
  72. HIO_TYPE_UNKNOWN = 0,
  73. HIO_TYPE_STDIN = 0x00000001,
  74. HIO_TYPE_STDOUT = 0x00000002,
  75. HIO_TYPE_STDERR = 0x00000004,
  76. HIO_TYPE_STDIO = 0x0000000F,
  77. HIO_TYPE_FILE = 0x00000010,
  78. HIO_TYPE_IP = 0x00000100,
  79. HIO_TYPE_SOCK_RAW = 0x00000F00,
  80. HIO_TYPE_UDP = 0x00001000,
  81. HIO_TYPE_KCP = 0x00002000,
  82. HIO_TYPE_DTLS = 0x00010000,
  83. HIO_TYPE_SOCK_DGRAM = 0x000FF000,
  84. HIO_TYPE_TCP = 0x00100000,
  85. HIO_TYPE_SSL = 0x01000000,
  86. HIO_TYPE_TLS = HIO_TYPE_SSL,
  87. HIO_TYPE_SOCK_STREAM= 0x0FF00000,
  88. HIO_TYPE_SOCKET = 0x0FFFFF00,
  89. } hio_type_e;
  90. typedef enum {
  91. HIO_SERVER_SIDE = 0,
  92. HIO_CLIENT_SIDE = 1,
  93. } hio_side_e;
  94. #define HIO_DEFAULT_CONNECT_TIMEOUT 5000 // ms
  95. #define HIO_DEFAULT_CLOSE_TIMEOUT 60000 // ms
  96. #define HIO_DEFAULT_KEEPALIVE_TIMEOUT 75000 // ms
  97. #define HIO_DEFAULT_HEARTBEAT_INTERVAL 10000 // ms
  98. BEGIN_EXTERN_C
  99. // loop
  100. #define HLOOP_FLAG_RUN_ONCE 0x00000001
  101. #define HLOOP_FLAG_AUTO_FREE 0x00000002
  102. #define HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS 0x00000004
  103. HV_EXPORT hloop_t* hloop_new(int flags DEFAULT(HLOOP_FLAG_AUTO_FREE));
  104. // WARN: Forbid to call hloop_free if HLOOP_FLAG_AUTO_FREE set.
  105. HV_EXPORT void hloop_free(hloop_t** pp);
  106. // NOTE: when no active events, loop will quit if HLOOP_FLAG_QUIT_WHEN_NO_ACTIVE_EVENTS set.
  107. HV_EXPORT int hloop_run(hloop_t* loop);
  108. // NOTE: hloop_stop called in loop-thread just set flag to quit in next loop,
  109. // if called in other thread, it will wakeup loop-thread from blocking poll system call,
  110. // then you should join loop thread to safely exit loop thread.
  111. HV_EXPORT int hloop_stop(hloop_t* loop);
  112. HV_EXPORT int hloop_pause(hloop_t* loop);
  113. HV_EXPORT int hloop_resume(hloop_t* loop);
  114. HV_EXPORT int hloop_wakeup(hloop_t* loop);
  115. HV_EXPORT hloop_status_e hloop_status(hloop_t* loop);
  116. HV_EXPORT void hloop_update_time(hloop_t* loop);
  117. HV_EXPORT uint64_t hloop_now(hloop_t* loop); // s
  118. HV_EXPORT uint64_t hloop_now_ms(hloop_t* loop); // ms
  119. HV_EXPORT uint64_t hloop_now_hrtime(hloop_t* loop); // us
  120. #define hloop_now_us hloop_now_hrtime
  121. // @return pid of hloop_run
  122. HV_EXPORT long hloop_pid(hloop_t* loop);
  123. // @return tid of hloop_run
  124. HV_EXPORT long hloop_tid(hloop_t* loop);
  125. // userdata
  126. HV_EXPORT void hloop_set_userdata(hloop_t* loop, void* userdata);
  127. HV_EXPORT void* hloop_userdata(hloop_t* loop);
  128. // custom_event
  129. /*
  130. * hevent_t ev;
  131. * memset(&ev, 0, sizeof(hevent_t));
  132. * ev.event_type = (hevent_type_e)(HEVENT_TYPE_CUSTOM + 1);
  133. * ev.cb = custom_event_cb;
  134. * ev.userdata = userdata;
  135. * hloop_post_event(loop, &ev);
  136. */
  137. // NOTE: hloop_post_event is thread-safe, used to post event from other thread to loop thread.
  138. HV_EXPORT void hloop_post_event(hloop_t* loop, hevent_t* ev);
  139. // idle
  140. HV_EXPORT hidle_t* hidle_add(hloop_t* loop, hidle_cb cb, uint32_t repeat DEFAULT(INFINITE));
  141. HV_EXPORT void hidle_del(hidle_t* idle);
  142. // timer
  143. // @param timeout: unit(ms)
  144. HV_EXPORT htimer_t* htimer_add(hloop_t* loop, htimer_cb cb, uint32_t timeout, uint32_t repeat DEFAULT(INFINITE));
  145. /*
  146. * minute hour day week month cb
  147. * 0~59 0~23 1~31 0~6 1~12
  148. * -1 -1 -1 -1 -1 cron.minutely
  149. * 30 -1 -1 -1 -1 cron.hourly
  150. * 30 1 -1 -1 -1 cron.daily
  151. * 30 1 15 -1 -1 cron.monthly
  152. * 30 1 -1 5 -1 cron.weekly
  153. * 30 1 1 -1 10 cron.yearly
  154. */
  155. HV_EXPORT htimer_t* htimer_add_period(hloop_t* loop, htimer_cb cb,
  156. int8_t minute DEFAULT(0), int8_t hour DEFAULT(-1), int8_t day DEFAULT(-1),
  157. int8_t week DEFAULT(-1), int8_t month DEFAULT(-1), uint32_t repeat DEFAULT(INFINITE));
  158. HV_EXPORT void htimer_del(htimer_t* timer);
  159. HV_EXPORT void htimer_reset(htimer_t* timer);
  160. // io
  161. //-----------------------low-level apis---------------------------------------
  162. #define HV_READ 0x0001
  163. #define HV_WRITE 0x0004
  164. #define HV_RDWR (HV_READ|HV_WRITE)
  165. /*
  166. const char* hio_engine() {
  167. #ifdef EVENT_SELECT
  168. return "select";
  169. #elif defined(EVENT_POLL)
  170. return "poll";
  171. #elif defined(EVENT_EPOLL)
  172. return "epoll";
  173. #elif defined(EVENT_KQUEUE)
  174. return "kqueue";
  175. #elif defined(EVENT_IOCP)
  176. return "iocp";
  177. #elif defined(EVENT_PORT)
  178. return "evport";
  179. #else
  180. return "noevent";
  181. #endif
  182. }
  183. */
  184. HV_EXPORT const char* hio_engine();
  185. HV_EXPORT hio_t* hio_get(hloop_t* loop, int fd);
  186. HV_EXPORT int hio_add(hio_t* io, hio_cb cb, int events DEFAULT(HV_READ));
  187. HV_EXPORT int hio_del(hio_t* io, int events DEFAULT(HV_RDWR));
  188. // NOTE: io detach from old loop and attach to new loop
  189. /* @see examples/multi-thread/one-acceptor-multi-workers.c
  190. void new_conn_event(hevent_t* ev) {
  191. hloop_t* loop = ev->loop;
  192. hio_t* io = (hio_t*)hevent_userdata(ev);
  193. hio_attach(loop, io);
  194. }
  195. void on_accpet(hio_t* io) {
  196. hio_detach(io);
  197. hloop_t* worker_loop = get_one_loop();
  198. hevent_t ev;
  199. memset(&ev, 0, sizeof(ev));
  200. ev.loop = worker_loop;
  201. ev.cb = new_conn_event;
  202. ev.userdata = io;
  203. hloop_post_event(worker_loop, &ev);
  204. }
  205. */
  206. HV_EXPORT void hio_detach(/*hloop_t* loop,*/ hio_t* io);
  207. HV_EXPORT void hio_attach(hloop_t* loop, hio_t* io);
  208. HV_EXPORT bool hio_exists(hloop_t* loop, int fd);
  209. // hio_t fields
  210. // NOTE: fd cannot be used as unique identifier, so we provide an id.
  211. HV_EXPORT uint32_t hio_id (hio_t* io);
  212. HV_EXPORT int hio_fd (hio_t* io);
  213. HV_EXPORT int hio_error (hio_t* io);
  214. HV_EXPORT int hio_events (hio_t* io);
  215. HV_EXPORT int hio_revents (hio_t* io);
  216. HV_EXPORT hio_type_e hio_type (hio_t* io);
  217. HV_EXPORT struct sockaddr* hio_localaddr(hio_t* io);
  218. HV_EXPORT struct sockaddr* hio_peeraddr (hio_t* io);
  219. HV_EXPORT void hio_set_context(hio_t* io, void* ctx);
  220. HV_EXPORT void* hio_context(hio_t* io);
  221. HV_EXPORT bool hio_is_opened(hio_t* io);
  222. HV_EXPORT bool hio_is_closed(hio_t* io);
  223. // #include "hbuf.h"
  224. typedef struct fifo_buf_s hio_readbuf_t;
  225. HV_EXPORT hio_readbuf_t* hio_get_readbuf(hio_t* io);
  226. HV_EXPORT size_t hio_write_bufsize(hio_t* io);
  227. #define hio_write_queue_is_empty(io) (hio_write_bufsize(io) == 0)
  228. HV_EXPORT uint64_t hio_last_read_time(hio_t* io); // ms
  229. HV_EXPORT uint64_t hio_last_write_time(hio_t* io); // ms
  230. // set callbacks
  231. HV_EXPORT void hio_setcb_accept (hio_t* io, haccept_cb accept_cb);
  232. HV_EXPORT void hio_setcb_connect (hio_t* io, hconnect_cb connect_cb);
  233. HV_EXPORT void hio_setcb_read (hio_t* io, hread_cb read_cb);
  234. HV_EXPORT void hio_setcb_write (hio_t* io, hwrite_cb write_cb);
  235. HV_EXPORT void hio_setcb_close (hio_t* io, hclose_cb close_cb);
  236. // get callbacks
  237. HV_EXPORT haccept_cb hio_getcb_accept(hio_t* io);
  238. HV_EXPORT hconnect_cb hio_getcb_connect(hio_t* io);
  239. HV_EXPORT hread_cb hio_getcb_read(hio_t* io);
  240. HV_EXPORT hwrite_cb hio_getcb_write(hio_t* io);
  241. HV_EXPORT hclose_cb hio_getcb_close(hio_t* io);
  242. // some useful settings
  243. // Enable SSL/TLS is so easy :)
  244. HV_EXPORT int hio_enable_ssl(hio_t* io);
  245. HV_EXPORT bool hio_is_ssl(hio_t* io);
  246. HV_EXPORT hssl_t hio_get_ssl(hio_t* io);
  247. HV_EXPORT int hio_set_ssl(hio_t* io, hssl_t ssl);
  248. // NOTE: One loop per thread, one readbuf per loop.
  249. // But you can pass in your own readbuf instead of the default readbuf to avoid memcopy.
  250. HV_EXPORT void hio_set_readbuf(hio_t* io, void* buf, size_t len);
  251. // connect timeout => hclose_cb
  252. HV_EXPORT void hio_set_connect_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CONNECT_TIMEOUT));
  253. // close timeout => hclose_cb
  254. HV_EXPORT void hio_set_close_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_CLOSE_TIMEOUT));
  255. // read timeout => hclose_cb
  256. HV_EXPORT void hio_set_read_timeout(hio_t* io, int timeout_ms);
  257. // write timeout => hclose_cb
  258. HV_EXPORT void hio_set_write_timeout(hio_t* io, int timeout_ms);
  259. // keepalive timeout => hclose_cb
  260. HV_EXPORT void hio_set_keepalive_timeout(hio_t* io, int timeout_ms DEFAULT(HIO_DEFAULT_KEEPALIVE_TIMEOUT));
  261. /*
  262. void send_heartbeat(hio_t* io) {
  263. static char buf[] = "PING\r\n";
  264. hio_write(io, buf, 6);
  265. }
  266. hio_set_heartbeat(io, 3000, send_heartbeat);
  267. */
  268. typedef void (*hio_send_heartbeat_fn)(hio_t* io);
  269. // heartbeat interval => hio_send_heartbeat_fn
  270. HV_EXPORT void hio_set_heartbeat(hio_t* io, int interval_ms, hio_send_heartbeat_fn fn);
  271. // Nonblocking, poll IO events in the loop to call corresponding callback.
  272. // hio_add(io, HV_READ) => accept => haccept_cb
  273. HV_EXPORT int hio_accept (hio_t* io);
  274. // connect => hio_add(io, HV_WRITE) => hconnect_cb
  275. HV_EXPORT int hio_connect(hio_t* io);
  276. // hio_add(io, HV_READ) => read => hread_cb
  277. HV_EXPORT int hio_read (hio_t* io);
  278. #define hio_read_start(io) hio_read(io)
  279. #define hio_read_stop(io) hio_del(io, HV_READ)
  280. // hio_read_start => hread_cb => hio_read_stop
  281. HV_EXPORT int hio_read_once (hio_t* io);
  282. // hio_read_once => hread_cb(len)
  283. HV_EXPORT int hio_read_until_length(hio_t* io, unsigned int len);
  284. // hio_read_once => hread_cb(...delim)
  285. HV_EXPORT int hio_read_until_delim (hio_t* io, unsigned char delim);
  286. // @see examples/tinyhttpd.c
  287. #define hio_readline(io) hio_read_until_delim(io, '\n')
  288. #define hio_readstring(io) hio_read_until_delim(io, '\0')
  289. #define hio_readbytes(io, len) hio_read_until_length(io, len)
  290. #define hio_read_until(io, len) hio_read_until_length(io, len)
  291. // NOTE: hio_write is thread-safe, locked by recursive_mutex, allow to be called by other threads.
  292. // hio_try_write => hio_add(io, HV_WRITE) => write => hwrite_cb
  293. HV_EXPORT int hio_write (hio_t* io, const void* buf, size_t len);
  294. // NOTE: hio_close is thread-safe, hio_close_async will be called actually in other thread.
  295. // hio_del(io, HV_RDWR) => close => hclose_cb
  296. HV_EXPORT int hio_close (hio_t* io);
  297. // NOTE: hloop_post_event(hio_close_event)
  298. HV_EXPORT int hio_close_async(hio_t* io);
  299. //------------------high-level apis-------------------------------------------
  300. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  301. HV_EXPORT hio_t* hread (hloop_t* loop, int fd, void* buf, size_t len, hread_cb read_cb);
  302. // hio_get -> hio_setcb_write -> hio_write
  303. HV_EXPORT hio_t* hwrite (hloop_t* loop, int fd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  304. // hio_get -> hio_close
  305. HV_EXPORT void hclose (hloop_t* loop, int fd);
  306. // tcp
  307. // hio_get -> hio_setcb_accept -> hio_accept
  308. HV_EXPORT hio_t* haccept (hloop_t* loop, int listenfd, haccept_cb accept_cb);
  309. // hio_get -> hio_setcb_connect -> hio_connect
  310. HV_EXPORT hio_t* hconnect (hloop_t* loop, int connfd, hconnect_cb connect_cb);
  311. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  312. HV_EXPORT hio_t* hrecv (hloop_t* loop, int connfd, void* buf, size_t len, hread_cb read_cb);
  313. // hio_get -> hio_setcb_write -> hio_write
  314. HV_EXPORT hio_t* hsend (hloop_t* loop, int connfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  315. // udp
  316. HV_EXPORT void hio_set_type(hio_t* io, hio_type_e type);
  317. HV_EXPORT void hio_set_localaddr(hio_t* io, struct sockaddr* addr, int addrlen);
  318. HV_EXPORT void hio_set_peeraddr (hio_t* io, struct sockaddr* addr, int addrlen);
  319. // NOTE: must call hio_set_peeraddr before hrecvfrom/hsendto
  320. // hio_get -> hio_set_readbuf -> hio_setcb_read -> hio_read
  321. HV_EXPORT hio_t* hrecvfrom (hloop_t* loop, int sockfd, void* buf, size_t len, hread_cb read_cb);
  322. // hio_get -> hio_setcb_write -> hio_write
  323. HV_EXPORT hio_t* hsendto (hloop_t* loop, int sockfd, const void* buf, size_t len, hwrite_cb write_cb DEFAULT(NULL));
  324. //-----------------top-level apis---------------------------------------------
  325. // @hio_create_socket: socket -> bind -> listen
  326. // sockaddr_set_ipport -> socket -> hio_get(loop, sockfd) ->
  327. // side == HIO_SERVER_SIDE ? bind ->
  328. // type & HIO_TYPE_SOCK_STREAM ? listen ->
  329. HV_EXPORT hio_t* hio_create_socket(hloop_t* loop, const char* host, int port,
  330. hio_type_e type DEFAULT(HIO_TYPE_TCP),
  331. hio_side_e side DEFAULT(HIO_SERVER_SIDE));
  332. // @tcp_server: hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_SERVER_SIDE) -> hio_setcb_accept -> hio_accept
  333. // @see examples/tcp_echo_server.c
  334. HV_EXPORT hio_t* hloop_create_tcp_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
  335. // @tcp_client: hio_create_socket(loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE) -> hio_setcb_connect -> hio_connect
  336. // @see examples/nc.c
  337. HV_EXPORT hio_t* hloop_create_tcp_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
  338. // @ssl_server: hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_SERVER_SIDE) -> hio_setcb_accept -> hio_accept
  339. // @see examples/tcp_echo_server.c => #define TEST_SSL 1
  340. HV_EXPORT hio_t* hloop_create_ssl_server (hloop_t* loop, const char* host, int port, haccept_cb accept_cb);
  341. // @ssl_client: hio_create_socket(loop, host, port, HIO_TYPE_SSL, HIO_CLIENT_SIDE) -> hio_setcb_connect -> hio_connect
  342. // @see examples/nc.c => #define TEST_SSL 1
  343. HV_EXPORT hio_t* hloop_create_ssl_client (hloop_t* loop, const char* host, int port, hconnect_cb connect_cb);
  344. // @udp_server: hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_SERVER_SIDE)
  345. // @see examples/udp_echo_server.c
  346. HV_EXPORT hio_t* hloop_create_udp_server (hloop_t* loop, const char* host, int port);
  347. // @udp_server: hio_create_socket(loop, host, port, HIO_TYPE_UDP, HIO_CLIENT_SIDE)
  348. // @see examples/nc.c
  349. HV_EXPORT hio_t* hloop_create_udp_client (hloop_t* loop, const char* host, int port);
  350. //-----------------upstream---------------------------------------------
  351. // hio_read(io)
  352. // hio_read(io->upstream_io)
  353. HV_EXPORT void hio_read_upstream(hio_t* io);
  354. // hio_write(io->upstream_io, buf, bytes)
  355. HV_EXPORT void hio_write_upstream(hio_t* io, void* buf, int bytes);
  356. // hio_close(io->upstream_io)
  357. HV_EXPORT void hio_close_upstream(hio_t* io);
  358. // io1->upstream_io = io2;
  359. // io2->upstream_io = io1;
  360. // hio_setcb_read(io1, hio_write_upstream);
  361. // hio_setcb_read(io2, hio_write_upstream);
  362. HV_EXPORT void hio_setup_upstream(hio_t* io1, hio_t* io2);
  363. // @return io->upstream_io
  364. HV_EXPORT hio_t* hio_get_upstream(hio_t* io);
  365. // @tcp_upstream: hio_create -> hio_setup_upstream -> hio_setcb_close(hio_close_upstream) -> hconnect -> on_connect -> hio_read_upstream
  366. // @return upstream_io
  367. // @see examples/tcp_proxy_server.c
  368. HV_EXPORT hio_t* hio_setup_tcp_upstream(hio_t* io, const char* host, int port, int ssl DEFAULT(0));
  369. #define hio_setup_ssl_upstream(io, host, port) hio_setup_tcp_upstream(io, host, port, 1)
  370. // @udp_upstream: hio_create -> hio_setup_upstream -> hio_read_upstream
  371. // @return upstream_io
  372. // @see examples/udp_proxy_server.c
  373. HV_EXPORT hio_t* hio_setup_udp_upstream(hio_t* io, const char* host, int port);
  374. //-----------------unpack---------------------------------------------
  375. typedef enum {
  376. UNPACK_BY_FIXED_LENGTH = 1, // Not recommended
  377. UNPACK_BY_DELIMITER = 2, // Suitable for text protocol
  378. UNPACK_BY_LENGTH_FIELD = 3, // Suitable for binary protocol
  379. } unpack_mode_e;
  380. #define DEFAULT_PACKAGE_MAX_LENGTH (1 << 21) // 2M
  381. // UNPACK_BY_DELIMITER
  382. #define PACKAGE_MAX_DELIMITER_BYTES 8
  383. // UNPACK_BY_LENGTH_FIELD
  384. typedef enum {
  385. ENCODE_BY_VARINT = 17, // 1 MSB + 7 bits
  386. ENCODE_BY_LITTEL_ENDIAN = LITTLE_ENDIAN, // 1234
  387. ENCODE_BY_BIG_ENDIAN = BIG_ENDIAN, // 4321
  388. } unpack_coding_e;
  389. typedef struct unpack_setting_s {
  390. unpack_mode_e mode;
  391. unsigned int package_max_length;
  392. union {
  393. // UNPACK_BY_FIXED_LENGTH
  394. struct {
  395. unsigned int fixed_length;
  396. };
  397. // UNPACK_BY_DELIMITER
  398. struct {
  399. unsigned char delimiter[PACKAGE_MAX_DELIMITER_BYTES];
  400. unsigned short delimiter_bytes;
  401. };
  402. // UNPACK_BY_LENGTH_FIELD
  403. /* package_len = head_len + body_len + length_adjustment
  404. *
  405. * if (length_field_coding == ENCODE_BY_VARINT) head_len = body_offset + varint_bytes - length_field_bytes;
  406. * else head_len = body_offset;
  407. *
  408. * body_len calc by length_field
  409. *
  410. */
  411. struct {
  412. unsigned short body_offset;
  413. unsigned short length_field_offset;
  414. unsigned short length_field_bytes;
  415. short length_adjustment;
  416. unpack_coding_e length_field_coding;
  417. };
  418. };
  419. #ifdef __cplusplus
  420. unpack_setting_s() {
  421. // Recommended setting:
  422. // head = flags:1byte + length:4bytes = 5bytes
  423. mode = UNPACK_BY_LENGTH_FIELD;
  424. package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  425. fixed_length = 0;
  426. delimiter_bytes = 0;
  427. body_offset = 5;
  428. length_field_offset = 1;
  429. length_field_bytes = 4;
  430. length_field_coding = ENCODE_BY_BIG_ENDIAN;
  431. length_adjustment = 0;
  432. }
  433. #endif
  434. } unpack_setting_t;
  435. // @see examples/jsonrpc examples/protorpc
  436. HV_EXPORT void hio_set_unpack(hio_t* io, unpack_setting_t* setting);
  437. HV_EXPORT void hio_unset_unpack(hio_t* io);
  438. // unpack examples
  439. /*
  440. unpack_setting_t ftp_unpack_setting;
  441. memset(&ftp_unpack_setting, 0, sizeof(unpack_setting_t));
  442. ftp_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  443. ftp_unpack_setting.mode = UNPACK_BY_DELIMITER;
  444. ftp_unpack_setting.delimiter[0] = '\r';
  445. ftp_unpack_setting.delimiter[1] = '\n';
  446. ftp_unpack_setting.delimiter_bytes = 2;
  447. unpack_setting_t mqtt_unpack_setting = {
  448. .mode = UNPACK_BY_LENGTH_FIELD,
  449. .package_max_length = DEFAULT_PACKAGE_MAX_LENGTH,
  450. .body_offset = 2,
  451. .length_field_offset = 1,
  452. .length_field_bytes = 1,
  453. .length_field_coding = ENCODE_BY_VARINT,
  454. };
  455. unpack_setting_t grpc_unpack_setting = {
  456. .mode = UNPACK_BY_LENGTH_FIELD,
  457. .package_max_length = DEFAULT_PACKAGE_MAX_LENGTH,
  458. .body_offset = 5,
  459. .length_field_offset = 1,
  460. .length_field_bytes = 4,
  461. .length_field_coding = ENCODE_BY_BIG_ENDIAN,
  462. };
  463. */
  464. //-----------------rudp---------------------------------------------
  465. #if WITH_KCP
  466. #define WITH_RUDP 1
  467. #endif
  468. #if WITH_RUDP
  469. // NOTE: hio_close_rudp is thread-safe.
  470. HV_EXPORT int hio_close_rudp(hio_t* io, struct sockaddr* peeraddr DEFAULT(NULL));
  471. #endif
  472. #if WITH_KCP
  473. typedef struct kcp_setting_s {
  474. // ikcp_create(conv, ...)
  475. unsigned int conv;
  476. // ikcp_nodelay(kcp, nodelay, interval, fastresend, nocwnd)
  477. int nodelay;
  478. int interval;
  479. int fastresend;
  480. int nocwnd;
  481. // ikcp_wndsize(kcp, sndwnd, rcvwnd)
  482. int sndwnd;
  483. int rcvwnd;
  484. // ikcp_setmtu(kcp, mtu)
  485. int mtu;
  486. // ikcp_update
  487. int update_interval;
  488. #ifdef __cplusplus
  489. kcp_setting_s() {
  490. conv = 0x11223344;
  491. // normal mode
  492. nodelay = 0;
  493. interval = 40;
  494. fastresend = 0;
  495. nocwnd = 0;
  496. // fast mode
  497. // nodelay = 1;
  498. // interval = 10;
  499. // fastresend = 2;
  500. // nocwnd = 1;
  501. sndwnd = 0;
  502. rcvwnd = 0;
  503. mtu = 1400;
  504. update_interval = 10; // ms
  505. }
  506. #endif
  507. } kcp_setting_t;
  508. // @see examples/udp_echo_server.c => #define TEST_KCP 1
  509. HV_EXPORT int hio_set_kcp(hio_t* io, kcp_setting_t* setting DEFAULT(NULL));
  510. #endif
  511. END_EXTERN_C
  512. #endif // HV_LOOP_H_