TcpServer.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. max_connections = 0xFFFFFFFF;
  16. connection_num = 0;
  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 = 0;
  58. return hssl_ctx_init(&param) == NULL ? -1 : 0;
  59. }
  60. return 0;
  61. }
  62. // channel
  63. const SocketChannelPtr& addChannel(hio_t* io) {
  64. std::lock_guard<std::mutex> locker(mutex_);
  65. int fd = hio_fd(io);
  66. if (fd >= channels.capacity()) {
  67. channels.resize(2 * fd);
  68. }
  69. channels[fd].reset(new SocketChannel(io));
  70. return channels[fd];
  71. }
  72. void removeChannel(const SocketChannelPtr& channel) {
  73. std::lock_guard<std::mutex> locker(mutex_);
  74. int fd = channel->fd();
  75. if (fd < channels.capacity()) {
  76. channels[fd] = NULL;
  77. }
  78. }
  79. private:
  80. static void onAccept(hio_t* connio) {
  81. TcpServer* server = (TcpServer*)hevent_userdata(connio);
  82. if (server->connection_num >= server->max_connections) {
  83. hlogw("over max_connections");
  84. hio_close(connio);
  85. return;
  86. }
  87. const SocketChannelPtr& channel = server->addChannel(connio);
  88. channel->status = SocketChannel::CONNECTED;
  89. ++server->connection_num;
  90. channel->onread = [server, &channel](Buffer* buf) {
  91. if (server->onMessage) {
  92. server->onMessage(channel, buf);
  93. }
  94. };
  95. channel->onwrite = [server, &channel](Buffer* buf) {
  96. if (server->onWriteComplete) {
  97. server->onWriteComplete(channel, buf);
  98. }
  99. };
  100. channel->onclose = [server, &channel]() {
  101. channel->status = SocketChannel::CLOSED;
  102. if (server->onConnection) {
  103. server->onConnection(channel);
  104. }
  105. server->removeChannel(channel);
  106. --server->connection_num;
  107. };
  108. channel->startRead();
  109. if (server->onConnection) {
  110. server->onConnection(channel);
  111. }
  112. }
  113. public:
  114. int listenfd;
  115. bool tls;
  116. // Callback
  117. ConnectionCallback onConnection;
  118. MessageCallback onMessage;
  119. WriteCompleteCallback onWriteComplete;
  120. uint32_t max_connections;
  121. std::atomic<uint32_t> connection_num;
  122. private:
  123. EventLoopThreadPool loop_threads;
  124. // with fd as index
  125. std::vector<SocketChannelPtr> channels; // GUAREDE_BY(mutex_)
  126. std::mutex mutex_;
  127. };
  128. }
  129. #endif // HV_TCP_SERVER_HPP_