TcpServer.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 setThreadNum(int num) {
  26. loop_threads.setThreadNum(num);
  27. }
  28. void start(bool wait_threads_started = true) {
  29. loop_threads.start(wait_threads_started, [this](const EventLoopPtr& loop){
  30. assert(listenfd >= 0);
  31. hio_t* listenio = haccept(loop->loop(), listenfd, onAccept);
  32. hevent_set_userdata(listenio, this);
  33. if (tls) {
  34. hio_enable_ssl(listenio);
  35. }
  36. });
  37. }
  38. void stop(bool wait_threads_stopped = true) {
  39. loop_threads.stop(wait_threads_stopped);
  40. }
  41. EventLoopPtr loop(int idx = -1) {
  42. return loop_threads.loop(idx);
  43. }
  44. hloop_t* hloop(int idx = -1) {
  45. return loop_threads.hloop(idx);
  46. }
  47. int withTLS(const char* cert_file, const char* key_file) {
  48. tls = true;
  49. hssl_ctx_init_param_t param;
  50. memset(&param, 0, sizeof(param));
  51. param.crt_file = cert_file;
  52. param.key_file = key_file;
  53. return hssl_ctx_init(&param) == NULL ? -1 : 0;
  54. }
  55. // channel
  56. SocketChannelPtr addChannel(hio_t* io) {
  57. std::lock_guard<std::mutex> locker(mutex_);
  58. SocketChannelPtr channel(new SocketChannel(io));
  59. int fd = channel->fd();
  60. if (fd >= channels.capacity()) {
  61. channels.resize(2 * fd);
  62. }
  63. channels[fd] = channel;
  64. return channel;
  65. }
  66. void removeChannel(const SocketChannelPtr& channel) {
  67. std::lock_guard<std::mutex> locker(mutex_);
  68. int fd = channel->fd();
  69. if (fd < channels.capacity()) {
  70. channels[fd] = NULL;
  71. }
  72. }
  73. private:
  74. static void onAccept(hio_t* connio) {
  75. TcpServer* server = (TcpServer*)hevent_userdata(connio);
  76. if (server->connection_num >= server->max_connections) {
  77. hlogw("over max_connections");
  78. hio_close(connio);
  79. return;
  80. }
  81. SocketChannelPtr channel = server->addChannel(connio);
  82. channel->status = SocketChannel::CONNECTED;
  83. ++server->connection_num;
  84. channel->onread = [server, &channel](Buffer* buf) {
  85. if (server->onMessage) {
  86. server->onMessage(channel, buf);
  87. }
  88. };
  89. channel->onwrite = [server, &channel](Buffer* buf) {
  90. if (server->onWriteComplete) {
  91. server->onWriteComplete(channel, buf);
  92. }
  93. };
  94. channel->onclose = [server, &channel]() {
  95. channel->status = SocketChannel::CLOSED;
  96. if (server->onConnection) {
  97. server->onConnection(channel);
  98. }
  99. server->removeChannel(channel);
  100. --server->connection_num;
  101. };
  102. channel->startRead();
  103. if (server->onConnection) {
  104. server->onConnection(channel);
  105. }
  106. }
  107. public:
  108. int listenfd;
  109. bool tls;
  110. // Callback
  111. ConnectionCallback onConnection;
  112. MessageCallback onMessage;
  113. WriteCompleteCallback onWriteComplete;
  114. uint32_t max_connections;
  115. std::atomic<uint32_t> connection_num;
  116. private:
  117. EventLoopThreadPool loop_threads;
  118. // with fd as index
  119. std::vector<SocketChannelPtr> channels; // GUAREDE_BY(mutex_)
  120. std::mutex mutex_;
  121. };
  122. }
  123. #endif // HV_TCP_SERVER_HPP_