TTKMusicPlayer  3.7.0.0
TTKMusicPlayer imitates Kugou UI, the music player uses of qmmp core library based on Qt for windows and linux
qdlnafinder.cpp
Go to the documentation of this file.
1 #include "qdlnafinder.h"
2 #include "qdlnaclient.h"
3 
4 #include <QUdpSocket>
5 #include <QApplication>
6 
7 static constexpr const char *DEFAULT_ROUTER_IP = "192.168.0.1";
8 
12 class QDlnaFinderPrivate : public TTKPrivate<QDlnaFinder>
13 {
14 public:
17 
18  void find();
19  void removeClients();
20  bool findClient(const QString &server);
21 
22  QUdpSocket *m_udpSock;
23  QList<QDlnaClient*> m_clients;
24 
25 };
26 
28  : m_udpSock(new QUdpSocket)
29 {
30  m_udpSock->bind(QHostAddress(QHostAddress::Any), 6000);
31 }
32 
34 {
35  delete m_udpSock;
36  removeClients();
37 }
38 
40 {
41  removeClients();
42  const QByteArray &data = "M-SEARCH * HTTP/1.1\r\n"
43  "HOST:239.255.255.250:1900\r\n"
44  "MAN:\"ssdp:discover\"\r\n"
45  "ST:ssdp:all\r\n"
46  "MX:3\r\n\r\n";
47  m_udpSock->writeDatagram(data, QHostAddress("239.255.255.250"), 1900);
48 }
49 
51 {
52  qDeleteAll(m_clients);
53 }
54 
55 bool QDlnaFinderPrivate::findClient(const QString &server)
56 {
57  for(QDlnaClient *client : qAsConst(m_clients))
58  {
59  if(client->server() == server)
60  {
61  return true;
62  }
63  }
64  return false;
65 }
66 
67 
68 
69 QDlnaFinder::QDlnaFinder(QObject *parent)
70  : QObject(parent)
71 {
74  connect(d->m_udpSock, SIGNAL(readyRead()), SLOT(handleReadyRead()));
75 }
76 
78 {
80  d->find();
81 }
82 
84 {
86  if(index < 0 || index >= d->m_clients.count())
87  {
88  return nullptr;
89  }
90 
91  return d->m_clients[index];
92 }
93 
94 QList<QDlnaClient*> QDlnaFinder::clients() const
95 {
97  return d->m_clients;
98 }
99 
100 QStringList QDlnaFinder::clientNames() const
101 {
103  QStringList names;
104  for(QDlnaClient *client : qAsConst(d->m_clients))
105  {
106  names.push_back(client->serverName());
107  }
108  return names;
109 }
110 
112 {
114  while(d->m_udpSock->hasPendingDatagrams())
115  {
116  QByteArray datagram;
117  datagram.resize(d->m_udpSock->pendingDatagramSize());
118  d->m_udpSock->readDatagram(datagram.data(), datagram.length());
119 
120  QDlnaClient *client = new QDlnaClient(QString::fromUtf8(datagram.data()));
121  if(client->server() == DEFAULT_ROUTER_IP || d->findClient(client->server()))
122  {
123  delete client;
124  continue;
125  }
126 
127  int tryTimes = 3;
128  do
129  {
130  --tryTimes;
131  client->connect();
132  qApp->processEvents();
133  } while(!client->isConnected() && tryTimes > 0);
134 
135  d->m_clients.push_back(client);
136  Q_EMIT finished();
137  }
138 }
QString serverName()
void finished()
QDlnaFinder(QObject *parent=nullptr)
Definition: qdlnafinder.cpp:69
The class of the dlna client.
Definition: qdlnaclient.h:29
The class of the dlna finder.
Definition: qdlnafinder.h:31
QString server()
static constexpr const char * DEFAULT_ROUTER_IP
Definition: qdlnafinder.cpp:7
QStringList clientNames() const
bool connect()
QList< QDlnaClient * > clients() const
Definition: qdlnafinder.cpp:94
QDlnaClient * client(int index) const
Definition: qdlnafinder.cpp:83
#define qAsConst
Definition: ttkqtglobal.h:53
bool findClient(const QString &server)
Definition: qdlnafinder.cpp:55
QList< QDlnaClient * > m_clients
Definition: qdlnafinder.cpp:23
void handleReadyRead()
QUdpSocket * m_udpSock
Definition: qdlnafinder.cpp:22
The class of the dlna finder private.
Definition: qdlnafinder.cpp:12
#define TTK_INIT_PRIVATE(Class)
Definition: ttkprivate.h:33
The class of the ttk private base.
Definition: ttkprivate.h:48
bool isConnected() const
#define TTK_D(Class)
Definition: ttkprivate.h:41