TTKMusicPlayer  4.1.3.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  m_udpSock->disconnectFromHost();
36  delete m_udpSock;
37  removeClients();
38 }
39 
41 {
42  removeClients();
43  const QByteArray &data = "M-SEARCH * HTTP/1.1\r\n"
44  "HOST:239.255.255.250:1900\r\n"
45  "MAN:\"ssdp:discover\"\r\n"
46  "ST:ssdp:all\r\n"
47  "MX:3\r\n\r\n";
48  m_udpSock->writeDatagram(data, QHostAddress("239.255.255.250"), 1900);
49 }
50 
52 {
53  qDeleteAll(m_clients);
54  m_clients.clear();
55 }
56 
57 bool QDlnaFinderPrivate::findClient(const QString &server)
58 {
59  for(QDlnaClient *client : qAsConst(m_clients))
60  {
61  if(client->server() == server)
62  {
63  return true;
64  }
65  }
66  return false;
67 }
68 
69 
70 
71 QDlnaFinder::QDlnaFinder(QObject *parent)
72  : QObject(parent)
73 {
76  connect(d->m_udpSock, SIGNAL(readyRead()), SLOT(handleReadyRead()));
77 }
78 
80 {
82  d->find();
83 }
84 
86 {
88  if(index < 0 || index >= d->m_clients.count())
89  {
90  return nullptr;
91  }
92 
93  return d->m_clients[index];
94 }
95 
96 QList<QDlnaClient*> QDlnaFinder::clients() const
97 {
99  return d->m_clients;
100 }
101 
102 QStringList QDlnaFinder::clientNames() const
103 {
105  QStringList names;
106  for(QDlnaClient *client : qAsConst(d->m_clients))
107  {
108  names.push_back(client->serverName());
109  }
110  return names;
111 }
112 
114 {
116  while(d->m_udpSock->hasPendingDatagrams())
117  {
118  QByteArray datagram;
119  datagram.resize(d->m_udpSock->pendingDatagramSize());
120  d->m_udpSock->readDatagram(datagram.data(), datagram.length());
121 
122  QDlnaClient *client = new QDlnaClient(QString::fromUtf8(datagram.data()));
123  if(client->server() == DEFAULT_ROUTER_IP || d->findClient(client->server()))
124  {
125  delete client;
126  continue;
127  }
128 
129  int tryTimes = 3;
130  do
131  {
132  --tryTimes;
133  client->connect();
134  qApp->processEvents();
135  } while(!client->isConnected() && tryTimes > 0);
136 
137  d->m_clients.push_back(client);
138  Q_EMIT finished();
139  }
140 }
void finished()
QDlnaFinder(QObject *parent=nullptr)
Definition: qdlnafinder.cpp:71
The class of the dlna client.
Definition: qdlnaclient.h:29
The class of the dlna finder.
Definition: qdlnafinder.h:31
static constexpr const char * DEFAULT_ROUTER_IP
Definition: qdlnafinder.cpp:7
QStringList clientNames() const
QList< QDlnaClient * > clients() const
Definition: qdlnafinder.cpp:96
QDlnaClient * client(int index) const
Definition: qdlnafinder.cpp:85
#define qAsConst
Definition: ttkqtglobal.h:51
bool findClient(const QString &server)
Definition: qdlnafinder.cpp:57
bool connect() const
QList< QDlnaClient * > m_clients
Definition: qdlnafinder.cpp:23
void handleReadyRead()
QUdpSocket * m_udpSock
Definition: qdlnafinder.cpp:22
QString server() const
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
QString serverName() const
#define TTK_D(Class)
Definition: ttkprivate.h:41