TcpServer.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #ifndef HV_TCP_SERVER_HPP_
  2. #define HV_TCP_SERVER_HPP_
  3. #include "hsocket.h"
  4. #include "hssl.h"
  5. #include "hlog.h"
  6. #include "EventLoopThreadPool.h"
  7. #include "Callback.h"
  8. #include "Channel.h"
  9. namespace hv {
  10. class TcpServer {
  11. public:
  12. TcpServer() {
  13. listenfd = -1;
  14. tls = false;
  15. enable_unpack = false;
  16. max_connections = 0xFFFFFFFF;
  17. }
  18. virtual ~TcpServer() {
  19. }
  20. //@retval >=0 listenfd, <0 error
  21. int createsocket(int port, const char* host = "0.0.0.0") {
  22. listenfd = Listen(port, host);
  23. return listenfd;
  24. }
  25. void setMaxConnectionNum(uint32_t num) {
  26. max_connections = num;
  27. }
  28. void setThreadNum(int num) {
  29. loop_threads.setThreadNum(num);
  30. }
  31. void start(bool wait_threads_started = true) {
  32. loop_threads.start(wait_threads_started, [this](const EventLoopPtr& loop){
  33. assert(listenfd >= 0);
  34. hio_t* listenio = haccept(loop->loop(), listenfd, onAccept);
  35. hevent_set_userdata(listenio, this);
  36. if (tls) {
  37. hio_enable_ssl(listenio);
  38. }
  39. });
  40. }
  41. void stop(bool wait_threads_stopped = true) {
  42. loop_threads.stop(wait_threads_stopped);
  43. }
  44. EventLoopPtr loop(int idx = -1) {
  45. return loop_threads.loop(idx);
  46. }
  47. hloop_t* hloop(int idx = -1) {
  48. return loop_threads.hloop(idx);
  49. }
  50. int withTLS(const char* cert_file, const char* key_file) {
  51. if (cert_file) {
  52. hssl_ctx_init_param_t param;
  53. memset(&param, 0, sizeof(param));
  54. param.crt_file = cert_file;
  55. param.key_file = key_file;
  56. param.endpoint = HSSL_SERVER;
  57. if (hssl_ctx_init(&param) == NULL) {
  58. fprintf(stderr, "hssl_ctx_init failed!\n");
  59. return -1;
  60. }
  61. }
  62. tls = true;
  63. return 0;
  64. }
  65. void setUnpack(unpack_setting_t* setting) {
  66. if (setting) {
  67. enable_unpack = true;
  68. unpack_setting = *setting;
  69. } else {
  70. enable_unpack = false;
  71. }
  72. }
  73. // channel
  74. const SocketChannelPtr& addChannel(hio_t* io) {
  75. int fd = hio_fd(io);
  76. auto channel = SocketChannelPtr(new SocketChannel(io));
  77. std::lock_guard<std::mutex> locker(mutex_);
  78. channels[fd] = channel;
  79. return channels[fd];
  80. }
  81. void removeChannel(const SocketChannelPtr& channel) {
  82. int fd = channel->fd();
  83. std::lock_guard<std::mutex> locker(mutex_);
  84. channels.erase(fd);
  85. }
  86. size_t connectionNum() {
  87. std::lock_guard<std::mutex> locker(mutex_);
  88. return channels.size();
  89. }
  90. private:
  91. static void onAccept(hio_t* connio) {
  92. TcpServer* server = (TcpServer*)hevent_userdata(connio);
  93. if (server->connectionNum() >= server->max_connections) {
  94. hlogw("over max_connections");
  95. hio_close(connio);
  96. return;
  97. }
  98. const SocketChannelPtr& channel = server->addChannel(connio);
  99. channel->status = SocketChannel::CONNECTED;
  100. channel->onread = [server, &channel](Buffer* buf) {
  101. if (server->onMessage) {
  102. server->onMessage(channel, buf);
  103. }
  104. };
  105. channel->onwrite = [server, &channel](Buffer* buf) {
  106. if (server->onWriteComplete) {
  107. server->onWriteComplete(channel, buf);
  108. }
  109. };
  110. channel->onclose = [server, &channel]() {
  111. channel->status = SocketChannel::CLOSED;
  112. if (server->onConnection) {
  113. server->onConnection(channel);
  114. }
  115. server->removeChannel(channel);
  116. // NOTE: After removeChannel, channel may be destroyed,
  117. // so in this lambda function, no code should be added below.
  118. };
  119. if (server->enable_unpack) {
  120. channel->setUnpack(&server->unpack_setting);
  121. }
  122. channel->startRead();
  123. if (server->onConnection) {
  124. server->onConnection(channel);
  125. }
  126. }
  127. public:
  128. int listenfd;
  129. bool tls;
  130. bool enable_unpack;
  131. unpack_setting_t unpack_setting;
  132. // Callback
  133. ConnectionCallback onConnection;
  134. MessageCallback onMessage;
  135. WriteCompleteCallback onWriteComplete;
  136. uint32_t max_connections;
  137. private:
  138. // fd => SocketChannelPtr
  139. std::map<int, SocketChannelPtr> channels; // GUAREDE_BY(mutex_)
  140. std::mutex mutex_;
  141. EventLoopThreadPool loop_threads;
  142. };
  143. }
  144. #endif // HV_TCP_SERVER_HPP_