You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "broadcast.h"
  2. Broadcast::Broadcast(bool isServer, QObject* p) : QObject(p)
  3. {
  4. m_isServer = isServer;
  5. m_server = new QUdpSocket(this);
  6. connect(m_server, SIGNAL(readyRead()), this, SLOT(readyRead()));
  7. connect(&m_timeoutTimer, SIGNAL(timeout()), this, SLOT(clearData()));
  8. connect(&m_bcTimer, SIGNAL(timeout()), this, SLOT(broadcastClient()));
  9. m_timeoutTimer.setSingleShot(true);
  10. m_bcTimer.setSingleShot(true);
  11. setTimeout(2500);
  12. setBroadcastInterval(5000);
  13. }
  14. QString Broadcast::getError() const
  15. {
  16. return m_server->errorString();
  17. }
  18. bool Broadcast::isServer() const
  19. {
  20. return m_isServer;
  21. }
  22. QByteArray Broadcast::clientData()
  23. {
  24. return PTSocket::handshakeData() + "bcastc";
  25. }
  26. QByteArray Broadcast::serverData()
  27. {
  28. return PTSocket::handshakeData() + "bcasts";
  29. }
  30. void Broadcast::setTimeout(int t)
  31. {
  32. if(m_timeoutTimer.isActive())
  33. m_timeoutTimer.start(t);
  34. else
  35. m_timeoutTimer.setInterval(t);
  36. }
  37. void Broadcast::setBroadcastInterval(int t)
  38. {
  39. if(m_bcTimer.isActive())
  40. m_bcTimer.start(t);
  41. else
  42. m_bcTimer.setInterval(t);
  43. }
  44. bool Broadcast::start(int port, int r, QHostAddress ad)
  45. {
  46. m_remotePort = r;
  47. bool ok = m_server->bind(ad, port);
  48. if(m_isServer)
  49. m_server->writeDatagram(serverData(), QHostAddress::Broadcast, m_remotePort);
  50. else
  51. broadcastClient();
  52. return ok;
  53. }
  54. void Broadcast::broadcastClient()
  55. {
  56. m_server->writeDatagram(clientData(), QHostAddress::Broadcast, m_remotePort);
  57. // m_bcTimer.start();
  58. }
  59. void Broadcast::readyRead()
  60. {
  61. while(m_server->hasPendingDatagrams())
  62. {
  63. QByteArray data;
  64. data.resize(m_server->pendingDatagramSize());
  65. QHostAddress host;
  66. quint16 port = 0;
  67. m_server->readDatagram(data.data(), data.size(), &host, &port);
  68. BcClient client(host.toIPv4Address(), port);
  69. m_data[client].append(data);
  70. if(m_isServer)
  71. {
  72. if(m_data[client].size() < clientData().size())
  73. continue;
  74. if(m_data[client] == clientData())
  75. m_server->writeDatagram(serverData(), QHostAddress(client.first), m_remotePort);
  76. m_data.remove(client);
  77. }
  78. else
  79. {
  80. if(m_data[client].size() < serverData().size())
  81. continue;
  82. if(m_data[client] == serverData())
  83. emit serverDiscovered(QHostAddress(client.first), client.second);
  84. m_data.remove(client);
  85. }
  86. }
  87. m_timeoutTimer.start();
  88. }
  89. void Broadcast::clearData()
  90. {
  91. m_data.clear();
  92. }