TcpServer.h 4.0 KB

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