tcp_client_test.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * tcp client demo
  3. *
  4. * @build make examples
  5. * @server bin/tcp_echo_server 1234
  6. * @client bin/tcp_client_test 127.0.0.1 1234
  7. *
  8. */
  9. #include "hloop.h"
  10. #include "hssl.h"
  11. #include "hmutex.h"
  12. #include "hbase.h"
  13. #include "herr.h"
  14. #define TEST_SSL 0
  15. #define TEST_UNPACK 0
  16. #define TEST_RECONNECT 1
  17. // @see mqtt/mqtt_client.h
  18. typedef struct tcp_client_s {
  19. // connect: host:port
  20. char host[256];
  21. int port;
  22. int connect_timeout; // ms
  23. // reconnect
  24. reconn_setting_t* reconn_setting;
  25. // flags
  26. unsigned char ssl: 1; // Read Only
  27. unsigned char alloced_ssl_ctx: 1; // intern
  28. unsigned char connected : 1;
  29. // privdata
  30. hloop_t* loop;
  31. hio_t* io;
  32. htimer_t* reconn_timer;
  33. // SSL/TLS
  34. hssl_ctx_t ssl_ctx;
  35. // thread-safe
  36. hmutex_t mutex_;
  37. // ...
  38. } tcp_client_t;
  39. static tcp_client_t* tcp_client_new(hloop_t* loop DEFAULT(NULL));
  40. static void tcp_client_run (tcp_client_t* cli);
  41. static void tcp_client_stop(tcp_client_t* cli);
  42. static void tcp_client_free(tcp_client_t* cli);
  43. // SSL/TLS
  44. static int tcp_client_set_ssl_ctx(tcp_client_t* cli, hssl_ctx_t ssl_ctx);
  45. static int tcp_client_new_ssl_ctx(tcp_client_t* cli, hssl_ctx_opt_t* opt);
  46. // reconnect
  47. static int tcp_client_set_reconnect(tcp_client_t* cli, reconn_setting_t* reconn);
  48. static int tcp_client_reconnect(tcp_client_t* cli);
  49. static void tcp_client_set_connnect_timeout(tcp_client_t* cli, int timeout_ms);
  50. static int tcp_client_connect(tcp_client_t* cli, const char* host, int port, int ssl);
  51. static int tcp_client_disconnect(tcp_client_t* cli);
  52. static bool tcp_client_is_connected(tcp_client_t* cli);
  53. static int tcp_client_send(tcp_client_t* cli, const void* buf, int len);
  54. static void reconnect_timer_cb(htimer_t* timer) {
  55. tcp_client_t* cli = (tcp_client_t*)hevent_userdata(timer);
  56. if (cli == NULL) return;
  57. cli->reconn_timer = NULL;
  58. tcp_client_reconnect(cli);
  59. }
  60. static void on_close(hio_t* io) {
  61. printf("onclose: connfd=%d error=%d\n", hio_fd(io), hio_error(io));
  62. tcp_client_t* cli = (tcp_client_t*)hevent_userdata(io);
  63. cli->connected = 0;
  64. // reconnect
  65. if (cli->reconn_setting && reconn_setting_can_retry(cli->reconn_setting)) {
  66. uint32_t delay = reconn_setting_calc_delay(cli->reconn_setting);
  67. printf("reconnect cnt=%d, delay=%d ...\n", cli->reconn_setting->cur_retry_cnt, cli->reconn_setting->cur_delay);
  68. cli->reconn_timer = htimer_add(cli->loop, reconnect_timer_cb, delay, 1);
  69. hevent_set_userdata(cli->reconn_timer, cli);
  70. }
  71. }
  72. static void on_message(hio_t* io, void* buf, int len) {
  73. printf("onmessage: %.*s\n", len, (char*)buf);
  74. tcp_client_t* cli = (tcp_client_t*)hevent_userdata(io);
  75. // ...
  76. }
  77. static void on_connect(hio_t* io) {
  78. printf("onconnect: connfd=%d\n", hio_fd(io));
  79. tcp_client_t* cli = (tcp_client_t*)hevent_userdata(io);
  80. cli->connected = 1;
  81. #if TEST_UNPACK
  82. static unpack_setting_t s_unpack_setting;
  83. s_unpack_setting.mode = UNPACK_BY_DELIMITER;
  84. s_unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  85. s_unpack_setting.delimiter_bytes = 2;
  86. s_unpack_setting.delimiter[0] = '\r';
  87. s_unpack_setting.delimiter[1] = '\n';
  88. hio_set_unpack(io, &s_unpack_setting);
  89. #endif
  90. hio_write(io, "hello\r\n", 7);
  91. hio_setcb_read(io, on_message);
  92. hio_read(io);
  93. }
  94. // hloop_new -> malloc(tcp_client_t)
  95. tcp_client_t* tcp_client_new(hloop_t* loop) {
  96. if (loop == NULL) {
  97. loop = hloop_new(HLOOP_FLAG_AUTO_FREE);
  98. if (loop == NULL) return NULL;
  99. }
  100. tcp_client_t* cli = NULL;
  101. HV_ALLOC_SIZEOF(cli);
  102. if (cli == NULL) return NULL;
  103. cli->loop = loop;
  104. hmutex_init(&cli->mutex_);
  105. return cli;
  106. }
  107. // hloop_free -> free(tcp_client_t)
  108. void tcp_client_free(tcp_client_t* cli) {
  109. if (!cli) return;
  110. hmutex_destroy(&cli->mutex_);
  111. if (cli->ssl_ctx && cli->alloced_ssl_ctx) {
  112. hssl_ctx_free(cli->ssl_ctx);
  113. cli->ssl_ctx = NULL;
  114. }
  115. HV_FREE(cli->reconn_setting);
  116. HV_FREE(cli);
  117. }
  118. void tcp_client_run (tcp_client_t* cli) {
  119. if (!cli || !cli->loop) return;
  120. hloop_run(cli->loop);
  121. }
  122. void tcp_client_stop(tcp_client_t* cli) {
  123. if (!cli || !cli->loop) return;
  124. hloop_stop(cli->loop);
  125. }
  126. int tcp_client_set_ssl_ctx(tcp_client_t* cli, hssl_ctx_t ssl_ctx) {
  127. cli->ssl_ctx = ssl_ctx;
  128. return 0;
  129. }
  130. // hssl_ctx_new(opt) -> tcp_client_set_ssl_ctx
  131. int tcp_client_new_ssl_ctx(tcp_client_t* cli, hssl_ctx_opt_t* opt) {
  132. opt->endpoint = HSSL_CLIENT;
  133. hssl_ctx_t ssl_ctx = hssl_ctx_new(opt);
  134. if (ssl_ctx == NULL) return ERR_NEW_SSL_CTX;
  135. cli->alloced_ssl_ctx = true;
  136. return tcp_client_set_ssl_ctx(cli, ssl_ctx);
  137. }
  138. int tcp_client_set_reconnect(tcp_client_t* cli, reconn_setting_t* reconn) {
  139. if (reconn == NULL) {
  140. HV_FREE(cli->reconn_setting);
  141. return 0;
  142. }
  143. if (cli->reconn_setting == NULL) {
  144. HV_ALLOC_SIZEOF(cli->reconn_setting);
  145. }
  146. *cli->reconn_setting = *reconn;
  147. return 0;
  148. }
  149. int tcp_client_reconnect(tcp_client_t* cli) {
  150. tcp_client_connect(cli, cli->host, cli->port, cli->ssl);
  151. return 0;
  152. }
  153. int tcp_client_connect(tcp_client_t* cli, const char* host, int port, int ssl) {
  154. if (!cli) return -1;
  155. hv_strncpy(cli->host, host, sizeof(cli->host));
  156. cli->port = port;
  157. cli->ssl = ssl;
  158. hio_t* io = hio_create_socket(cli->loop, host, port, HIO_TYPE_TCP, HIO_CLIENT_SIDE);
  159. if (io == NULL) return -1;
  160. if (ssl) {
  161. if (cli->ssl_ctx) {
  162. hio_set_ssl_ctx(io, cli->ssl_ctx);
  163. }
  164. hio_enable_ssl(io);
  165. }
  166. if (cli->connect_timeout > 0) {
  167. hio_set_connect_timeout(io, cli->connect_timeout);
  168. }
  169. cli->io = io;
  170. hevent_set_userdata(io, cli);
  171. hio_setcb_connect(io, on_connect);
  172. hio_setcb_close(io, on_close);
  173. return hio_connect(io);
  174. }
  175. int tcp_client_disconnect(tcp_client_t* cli) {
  176. if (!cli || !cli->io) return -1;
  177. // cancel reconnect first
  178. tcp_client_set_reconnect(cli, NULL);
  179. return hio_close(cli->io);
  180. }
  181. bool tcp_client_is_connected(tcp_client_t* cli) {
  182. return cli && cli->connected;
  183. }
  184. int tcp_client_send(tcp_client_t* cli, const void* buf, int len) {
  185. if (!cli || !cli->io || !buf || len == 0) return -1;
  186. if (!cli->connected) return -2;
  187. // thread-safe
  188. hmutex_lock(&cli->mutex_);
  189. int nwrite = hio_write(cli->io, buf, len);
  190. hmutex_unlock(&cli->mutex_);
  191. return nwrite;
  192. }
  193. int main(int argc, char** argv) {
  194. if (argc < 3) {
  195. printf("Usage: %s host port\n", argv[0]);
  196. return -10;
  197. }
  198. const char* host = argv[1];
  199. int port = atoi(argv[2]);
  200. tcp_client_t* cli = tcp_client_new(NULL);
  201. if (!cli) return -20;
  202. #if TEST_RECONNECT
  203. reconn_setting_t reconn;
  204. reconn_setting_init(&reconn);
  205. reconn.min_delay = 1000;
  206. reconn.max_delay = 10000;
  207. reconn.delay_policy = 2;
  208. tcp_client_set_reconnect(cli, &reconn);
  209. #endif
  210. int ssl = 0;
  211. #if TEST_SSL
  212. ssl = 1;
  213. #endif
  214. tcp_client_connect(cli, host, port, ssl);
  215. tcp_client_run(cli);
  216. tcp_client_free(cli);
  217. return 0;
  218. }