Torc  0.1
torcssdp.h
Go to the documentation of this file.
1 #ifndef TORCSSDP_H
2 #define TORCSSDP_H
3 
4 // Qt
5 #include <QObject>
6 #include <QUdpSocket>
7 #include <QTimer>
8 #include <QQueue>
9 
10 // Torc
11 
12 #include "torcqthread.h"
13 #include "torcupnp.h"
14 #include "torchttpserver.h"
15 
16 #define TORC_SSDP_UDP_MULTICAST_PORT 1900
17 #define TORC_IPV4_UDP_MULTICAST_ADDR QStringLiteral("239.255.255.250")
18 #define TORC_IPV4_UDP_MULTICAST_URL QStringLiteral("%1:%2").arg(TORC_IPV4_UDP_MULTICAST_ADDR).arg(TORC_SSDP_UDP_MULTICAST_PORT)
19 #define TORC_IPV6_UDP_MULTICAST_ADDR QStringLiteral("[FF05::C]")
20 #define TORC_IPV6_UDP_MULTICAST_ADDR2 QStringLiteral("FF05::C")
21 #define TORC_IPV6_UDP_MULTICAST_URL QStringLiteral("%1:%2").arg(TORC_IPV6_UDP_MULTICAST_ADDR).arg(TORC_SSDP_UDP_MULTICAST_PORT)
22 
24 {
25  public:
27  {
28  None = (0 << 0),
29  Root = (1 << 0),
30  UUID = (1 << 1),
31  Device = (1 << 2)
32  };
33 
34  Q_DECLARE_FLAGS(ResponseTypes, ResponseType)
35 
36  TorcSSDPSearchResponse(const QHostAddress &Address, const ResponseTypes Types, int Port);
38 
39  public:
40  QHostAddress m_responseAddress;
41  ResponseTypes m_responseTypes;
42  int m_port;
43 };
44 
45 class TorcSSDP final : public QObject
46 {
47  friend class TorcSSDPThread;
48 
49  Q_OBJECT
50 
51  public:
52  static void Search (TorcHTTPServer::Status Options);
53  static void CancelSearch (void);
54  static void Announce (TorcHTTPServer::Status Options);
55  static void CancelAnnounce (void);
56 
57  static TorcSSDP *gSSDP;
58  static QMutex *gSSDPLock;
59  static bool gSearchEnabled;
61  static bool gAnnounceEnabled;
63 
64  protected:
65  TorcSSDP();
66  ~TorcSSDP();
67 
68  static TorcSSDP* Create (bool Destroy = false);
69 
70  protected slots:
71  void SearchPriv (void);
72  void CancelSearchPriv (void);
73  void AnnouncePriv (void);
74  void CancelAnnouncePriv (void);
75 
76  void SendSearch (void);
77  void SendAnnounce (bool IsIPv6, bool Alive);
78 
79  bool event (QEvent *Event) override;
80  void Read (void);
81  void ProcessResponses (void);
82 
83  private:
84  Q_DISABLE_COPY(TorcSSDP)
85  static QUdpSocket* CreateSearchSocket (const QHostAddress &HostAddress, TorcSSDP *Owner);
86  static QUdpSocket* CreateMulticastSocket (const QHostAddress &HostAddress, TorcSSDP *Owner, const QNetworkInterface &Interface);
87 
88  void Start (void);
89  void Stop (void);
90  void Refresh (void);
91  void ProcessDevice (const QMap<QString,QString> &Headers, qint64 Expires, bool Add);
92  qint64 GetExpiryTime (const QString &Expires);
93  void StartSearch (void);
94  void StopSearch (void);
95  void StartAnnounce (void);
96  void StopAnnounce (void);
97  void ProcessResponse (const TorcSSDPSearchResponse &Response);
98 
99  private:
100  TorcHTTPServer::Status m_searchOptions;
101  TorcHTTPServer::Status m_announceOptions;
102  QString m_serverString;
103  bool m_searching;
104  bool m_searchDebugged;
105  int m_firstSearchTimer;
106  int m_secondSearchTimer;
107  int m_firstAnnounceTimer;
108  int m_secondAnnounceTimer;
109  int m_refreshTimer;
110  bool m_started;
111  QString m_ipv4Address;
112  QString m_ipv6Address;
113  QList<QHostAddress> m_addressess;
114  QHostAddress m_ipv4GroupAddress;
115  QUdpSocket *m_ipv4MulticastSocket;
116  QHostAddress m_ipv6LinkGroupBaseAddress;
117  QUdpSocket *m_ipv6LinkMulticastSocket;
118  QHash<QString,TorcUPNPDescription> m_discoveredDevices;
119  QUdpSocket *m_ipv4UnicastSocket;
120  QUdpSocket *m_ipv6UnicastSocket;
121  QTimer m_responseTimer;
122  QMap<qint64,TorcSSDPSearchResponse> m_responseQueue;
123 };
124 
125 class TorcSSDPThread final : public TorcQThread
126 {
127  Q_OBJECT
128 
129  public:
130  TorcSSDPThread();
131 
132  void Start(void) override;
133  void Finish(void) override;
134 };
135 #endif // TORCSSDP_H
static TorcSSDP * gSSDP
Definition: torcssdp.h:57
static QMutex * gSSDPLock
Definition: torcssdp.h:58
QHostAddress m_responseAddress
Definition: torcssdp.h:40
ResponseTypes m_responseTypes
Definition: torcssdp.h:41
The public class for handling Simple Service Discovery Protocol searches and announcements.
Definition: torcssdp.h:45
static TorcHTTPServer::Status gAnnounceOptions
Definition: torcssdp.h:62
static bool gAnnounceEnabled
Definition: torcssdp.h:61
void Finish(void) override
Definition: torcssdp.cpp:1022
static bool gSearchEnabled
Definition: torcssdp.h:59
static TorcHTTPServer::Status gSearchOptions
Definition: torcssdp.h:60
void Start(void) override
Definition: torcssdp.cpp:1016
A Torc specific wrapper around QThread.
Definition: torcqthread.h:7
An HTTP server.