RtpServer.h 3.0 KB

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