RtpServer.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #ifndef _RTPSERVER_H_
  2. #define _RTPSERVER_H_
  3. #include <QTcpServer>
  4. #include <QList>
  5. #include <QEventLoop>
  6. #include "spdlog/spdlog.h"
  7. #include "Rtpcommon.h"
  8. #include "GlobalVariable.h"
  9. #include "RtpOneRoadThread.h"
  10. /**
  11. RTP监听服务,监听客户端发起的数据接受请求
  12. 功能:
  13. 1、监听客户端的连接请求,获取客户端发送过来的UDP端口
  14. 2、从ThreadManager中获取RTP发送线程实例,将需要发送的对比项通道信息发送给
  15. RTP发送线程,RTP发送线程会将数据发送到客户端的UDP端口
  16. 3、RTP发送线程使用的UDP端口从这里确定,与客户端信息一同发送过去
  17. 接收的数据:
  18. 0是登录,1是心跳,2是注销
  19. 登录:
  20. 客户端在连接成功后,发送过来的登录信息是 RtpRecvClientInfo_t 结构体中的内容。
  21. */
  22. class RTPServer : public QObject
  23. {
  24. Q_OBJECT
  25. public:
  26. explicit RTPServer(QObject *parent = nullptr);
  27. ~RTPServer();
  28. /**
  29. * @brief 启动RTP服务
  30. * @param port 监听端口
  31. * @return 成功返回true,失败返回false
  32. */
  33. bool thread_task(int port);
  34. /* 停止线程 */
  35. void stopThreadBlock();
  36. private slots:
  37. /* 处理新连接 */
  38. void do_newConnection();
  39. /* 监听服务错误 */
  40. void do_error(QAbstractSocket::SocketError socketError);
  41. /* 断开连接 */
  42. void do_disconnect();
  43. /* 接收消息 */
  44. void do_receiveMessage();
  45. /* 接收到释放端口的信号 */
  46. void do_udpClosed(int soundCardNum, int roadNum, int localPort);
  47. private:
  48. /* 处理登录请求 */
  49. void handleLogin(RtpSendClientInfo_t& clientInfo);
  50. /* 处理心跳请求 */
  51. void handleHeartbeat(RtpSendClientInfo_t& clientInfo);
  52. /* 处理注销请求 */
  53. void handleLogout(RtpSendClientInfo_t& clientInfo);
  54. /* 查找本地出口IP,适用于单个出口IP,如果电脑上有多个网口,需要手动指定IP */
  55. QString findLocalIP();
  56. private:
  57. std::shared_ptr<spdlog::logger> m_logger = nullptr; /* 日志记录器 */
  58. QEventLoop m_eventLoop; /* 事件循环,用于处理异步操作 */
  59. std::atomic_bool m_isStoped = true; /* 线程是否已经停止标志位 */
  60. QTcpServer *m_tcpServer; /* TCP服务器对象 */
  61. int m_port; /* 监听端口 */
  62. QString m_localIP; /* 本地IP地址,给UDP使用,指定本地出口IP,防止自动连接到 0.0.0.0 上 */
  63. QList<QTcpSocket*> m_listClients; /* 客户端连接列表,用于管理多个客户端连接 */
  64. /* 录音通道线程列表,键为录音通道ID,值为通道使用到的本地端口 */
  65. QMap<SoundCardRoadKey_t, int> m_mapSoundCardRoadPorts;
  66. };
  67. #endif // _RTPSERVER_H_