TcpServer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/xia-chu/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef TCPSERVER_TCPSERVER_H
  11. #define TCPSERVER_TCPSERVER_H
  12. #include <assert.h>
  13. #include <mutex>
  14. #include <memory>
  15. #include <exception>
  16. #include <functional>
  17. #include <unordered_map>
  18. #include "Network/Server.h"
  19. #include "Network/TcpSession.h"
  20. #include "Poller/Timer.h"
  21. #include "Thread/semaphore.h"
  22. #include "Util/logger.h"
  23. #include "Util/util.h"
  24. #include "Util/uv_errno.h"
  25. using namespace std;
  26. namespace toolkit {
  27. //TCP服务器,可配置的;配置通过TcpSession::attachServer方法传递给会话对象
  28. class TcpServer : public Server {
  29. public:
  30. using Ptr = std::shared_ptr<TcpServer>;
  31. /**
  32. * 创建tcp服务器,listen fd的accept事件会加入到所有的poller线程中监听
  33. * 在调用TcpServer::start函数时,内部会创建多个子TcpServer对象,
  34. * 这些子TcpServer对象通过Socket对象克隆的方式在多个poller线程中监听同一个listen fd
  35. * 这样这个TCP服务器将会通过抢占式accept的方式把客户端均匀的分布到不同的poller线程
  36. * 通过该方式能实现客户端负载均衡以及提高连接接收速度
  37. */
  38. explicit TcpServer(const EventPoller::Ptr &poller = nullptr);
  39. ~TcpServer() override;
  40. /**
  41. * @brief 开始tcp server
  42. * @param port 本机端口,0则随机
  43. * @param host 监听网卡ip
  44. * @param backlog tcp listen backlog
  45. */
  46. template <typename SessionType>
  47. void start(uint16_t port, const std::string &host = "0.0.0.0", uint32_t backlog = 1024) {
  48. //TcpSession创建器,通过它创建不同类型的服务器
  49. _session_alloc = [](const TcpServer::Ptr &server, const Socket::Ptr &sock) {
  50. auto session = std::make_shared<SessionType>(sock);
  51. session->setOnCreateSocket(server->_on_create_socket);
  52. return std::make_shared<SessionHelper>(server, session);
  53. };
  54. start_l(port, host, backlog);
  55. }
  56. /**
  57. * @brief 获取服务器监听端口号, 服务器可以选择监听随机端口
  58. */
  59. uint16_t getPort();
  60. /**
  61. * @brief 自定义socket构建行为
  62. */
  63. void setOnCreateSocket(Socket::onCreateSocket cb);
  64. protected:
  65. virtual void cloneFrom(const TcpServer &that);
  66. virtual TcpServer::Ptr onCreatServer(const EventPoller::Ptr &poller);
  67. virtual void onAcceptConnection(const Socket::Ptr &sock);
  68. virtual Socket::Ptr onBeforeAcceptConnection(const EventPoller::Ptr &poller);
  69. private:
  70. void onManagerSession();
  71. Socket::Ptr createSocket(const EventPoller::Ptr &poller);
  72. void start_l(uint16_t port, const std::string &host, uint32_t backlog);
  73. Ptr getServer(const EventPoller *) const;
  74. private:
  75. bool _is_on_manager = false;
  76. const TcpServer *_parent = nullptr;
  77. Socket::Ptr _socket;
  78. std::shared_ptr<Timer> _timer;
  79. Socket::onCreateSocket _on_create_socket;
  80. unordered_map<SessionHelper *, SessionHelper::Ptr> _session_map;
  81. function<SessionHelper::Ptr(const TcpServer::Ptr &server, const Socket::Ptr &)> _session_alloc;
  82. unordered_map<const EventPoller *, Ptr> _cloned_server;
  83. //对象个数统计
  84. ObjectStatistic<TcpServer> _statistic;
  85. };
  86. } /* namespace toolkit */
  87. #endif /* TCPSERVER_TCPSERVER_H */