tcp_echo_server.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * tcp echo server
  3. *
  4. * @build make examples
  5. * @server bin/tcp_echo_server 1234
  6. * @client bin/nc 127.0.0.1 1234
  7. * nc 127.0.0.1 1234
  8. * telnet 127.0.0.1 1234
  9. */
  10. #include "hloop.h"
  11. #include "hsocket.h"
  12. #include "hssl.h"
  13. /*
  14. * @test ssl_server
  15. * #define TEST_SSL 1
  16. *
  17. * @build ./configure --with-openssl && make clean && make
  18. * @server bin/tcp_echo_server 1234
  19. * @client bin/nc -s 127.0.0.1 1234
  20. *
  21. */
  22. #define TEST_SSL 0
  23. #define TEST_READ_ONCE 0
  24. #define TEST_READLINE 0
  25. #define TEST_READSTRING 0
  26. #define TEST_READBYTES 0
  27. #define TEST_READ_STOP 0
  28. #define TEST_UNPACK 0
  29. #if TEST_UNPACK
  30. static unpack_setting_t unpack_setting;
  31. #endif
  32. // hloop_create_tcp_server -> on_accept -> hio_read -> on_recv -> hio_write
  33. static void on_close(hio_t* io) {
  34. printf("on_close fd=%d error=%d\n", hio_fd(io), hio_error(io));
  35. }
  36. static void on_recv(hio_t* io, void* buf, int readbytes) {
  37. printf("on_recv fd=%d readbytes=%d\n", hio_fd(io), readbytes);
  38. char localaddrstr[SOCKADDR_STRLEN] = {0};
  39. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  40. printf("[%s] <=> [%s]\n",
  41. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  42. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  43. printf("< %.*s", readbytes, (char*)buf);
  44. // echo
  45. printf("> %.*s", readbytes, (char*)buf);
  46. hio_write(io, buf, readbytes);
  47. #if TEST_READ_STOP
  48. hio_read_stop(io);
  49. #elif TEST_READ_ONCE
  50. hio_read_once(io);
  51. #elif TEST_READLINE
  52. hio_readline(io);
  53. #elif TEST_READSTRING
  54. hio_readstring(io);
  55. #elif TEST_READBYTES
  56. hio_readbytes(io, TEST_READBYTES);
  57. #endif
  58. }
  59. static void on_accept(hio_t* io) {
  60. printf("on_accept connfd=%d\n", hio_fd(io));
  61. char localaddrstr[SOCKADDR_STRLEN] = {0};
  62. char peeraddrstr[SOCKADDR_STRLEN] = {0};
  63. printf("accept connfd=%d [%s] <= [%s]\n", hio_fd(io),
  64. SOCKADDR_STR(hio_localaddr(io), localaddrstr),
  65. SOCKADDR_STR(hio_peeraddr(io), peeraddrstr));
  66. hio_setcb_close(io, on_close);
  67. hio_setcb_read(io, on_recv);
  68. #if TEST_UNPACK
  69. hio_set_unpack(io, &unpack_setting);
  70. #endif
  71. #if TEST_READ_ONCE
  72. hio_read_once(io);
  73. #elif TEST_READLINE
  74. hio_readline(io);
  75. #elif TEST_READSTRING
  76. hio_readstring(io);
  77. #elif TEST_READBYTES
  78. hio_readbytes(io, TEST_READBYTES);
  79. #else
  80. hio_read_start(io);
  81. #endif
  82. }
  83. int main(int argc, char** argv) {
  84. if (argc < 2) {
  85. printf("Usage: %s port|path\n", argv[0]);
  86. return -10;
  87. }
  88. const char* host = "0.0.0.0";
  89. int port = atoi(argv[1]);
  90. #if ENABLE_UDS
  91. if (port == 0) {
  92. host = argv[1];
  93. port = -1;
  94. }
  95. #endif
  96. #if TEST_UNPACK
  97. memset(&unpack_setting, 0, sizeof(unpack_setting_t));
  98. unpack_setting.package_max_length = DEFAULT_PACKAGE_MAX_LENGTH;
  99. unpack_setting.mode = UNPACK_BY_DELIMITER;
  100. unpack_setting.delimiter[0] = '\r';
  101. unpack_setting.delimiter[1] = '\n';
  102. unpack_setting.delimiter_bytes = 2;
  103. #endif
  104. hloop_t* loop = hloop_new(0);
  105. #if TEST_SSL
  106. hio_t* listenio = hloop_create_ssl_server(loop, host, port, on_accept);
  107. #else
  108. hio_t* listenio = hloop_create_tcp_server(loop, host, port, on_accept);
  109. #endif
  110. if (listenio == NULL) {
  111. return -20;
  112. }
  113. #if TEST_SSL
  114. hssl_ctx_opt_t ssl_param;
  115. memset(&ssl_param, 0, sizeof(ssl_param));
  116. ssl_param.crt_file = "cert/server.crt";
  117. ssl_param.key_file = "cert/server.key";
  118. ssl_param.endpoint = HSSL_SERVER;
  119. if (hio_new_ssl_ctx(listenio, &ssl_param) != 0) {
  120. fprintf(stderr, "hssl_ctx_new failed!\n");
  121. return -30;
  122. }
  123. #endif
  124. printf("listenfd=%d\n", hio_fd(listenio));
  125. hloop_run(loop);
  126. hloop_free(&loop);
  127. return 0;
  128. }