TcpServer.h 3.9 KB

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