TcpServer.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. tls = true;
  52. if (cert_file) {
  53. hssl_ctx_init_param_t param;
  54. memset(&param, 0, sizeof(param));
  55. param.crt_file = cert_file;
  56. param.key_file = key_file;
  57. param.endpoint = HSSL_SERVER;
  58. return hssl_ctx_init(&param) == NULL ? -1 : 0;
  59. }
  60. return 0;
  61. }
  62. void setUnpack(unpack_setting_t* setting) {
  63. if (setting) {
  64. enable_unpack = true;
  65. unpack_setting = *setting;
  66. } else {
  67. enable_unpack = false;
  68. }
  69. }
  70. // channel
  71. const SocketChannelPtr& addChannel(hio_t* io) {
  72. std::lock_guard<std::mutex> locker(mutex_);
  73. int fd = hio_fd(io);
  74. channels[fd] = SocketChannelPtr(new SocketChannel(io));
  75. return channels[fd];
  76. }
  77. void removeChannel(const SocketChannelPtr& channel) {
  78. std::lock_guard<std::mutex> locker(mutex_);
  79. int fd = channel->fd();
  80. channels.erase(fd);
  81. }
  82. size_t connectionNum() {
  83. std::lock_guard<std::mutex> locker(mutex_);
  84. return channels.size();
  85. }
  86. private:
  87. static void onAccept(hio_t* connio) {
  88. TcpServer* server = (TcpServer*)hevent_userdata(connio);
  89. if (server->connectionNum() >= server->max_connections) {
  90. hlogw("over max_connections");
  91. hio_close(connio);
  92. return;
  93. }
  94. const SocketChannelPtr& channel = server->addChannel(connio);
  95. channel->status = SocketChannel::CONNECTED;
  96. channel->onread = [server, &channel](Buffer* buf) {
  97. if (server->onMessage) {
  98. server->onMessage(channel, buf);
  99. }
  100. };
  101. channel->onwrite = [server, &channel](Buffer* buf) {
  102. if (server->onWriteComplete) {
  103. server->onWriteComplete(channel, buf);
  104. }
  105. };
  106. channel->onclose = [server, &channel]() {
  107. channel->status = SocketChannel::CLOSED;
  108. if (server->onConnection) {
  109. server->onConnection(channel);
  110. }
  111. server->removeChannel(channel);
  112. // NOTE: After removeChannel, channel may be destroyed,
  113. // so in this lambda function, no code should be added below.
  114. };
  115. if (server->enable_unpack) {
  116. channel->setUnpack(&server->unpack_setting);
  117. }
  118. channel->startRead();
  119. if (server->onConnection) {
  120. server->onConnection(channel);
  121. }
  122. }
  123. public:
  124. int listenfd;
  125. bool tls;
  126. bool enable_unpack;
  127. unpack_setting_t unpack_setting;
  128. // Callback
  129. ConnectionCallback onConnection;
  130. MessageCallback onMessage;
  131. WriteCompleteCallback onWriteComplete;
  132. uint32_t max_connections;
  133. private:
  134. EventLoopThreadPool loop_threads;
  135. // fd => SocketChannelPtr
  136. std::map<int, SocketChannelPtr> channels; // GUAREDE_BY(mutex_)
  137. std::mutex mutex_;
  138. };
  139. }
  140. #endif // HV_TCP_SERVER_HPP_