PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/gadu_protocol/socket-notifiers/gadu-socket-notifiers.cpp

https://gitlab.com/mziab/kadu
C++ | 177 lines | 117 code | 40 blank | 20 comment | 15 complexity | da2c830c97472821f6511863ca5f91c8 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, BSD-3-Clause, CC-BY-3.0, GPL-2.0
  1. /*
  2. * %kadu copyright begin%
  3. * Copyright 2012, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
  4. * Copyright 2011, 2013, 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
  5. * %kadu copyright end%
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <QtCore/QSocketNotifier>
  21. #include <QtCore/QTimer>
  22. #include "debug.h"
  23. #include "gadu-socket-notifiers.h"
  24. GaduSocketNotifiers::GaduSocketNotifiers(QObject *parent)
  25. : QObject(parent), Socket(-1), Started(false), ReadNotifier(0), WriteNotifier(0), TimeoutTimer(0)
  26. {
  27. kdebugf();
  28. kdebugf2();
  29. }
  30. GaduSocketNotifiers::~GaduSocketNotifiers()
  31. {
  32. kdebugf();
  33. deleteSocketNotifiers();
  34. kdebugf2();
  35. }
  36. void GaduSocketNotifiers::createSocketNotifiers()
  37. {
  38. kdebugf();
  39. deleteSocketNotifiers();
  40. if (-1 == Socket)
  41. return;
  42. // Old code was using 0 instead of -1 for invalid socket. Check whether we didn't forget to fix something.
  43. Q_ASSERT(0 != Socket);
  44. ReadNotifier = new QSocketNotifier(Socket, QSocketNotifier::Read, this);
  45. connect(ReadNotifier, SIGNAL(activated(int)), this, SLOT(dataReceived()));
  46. WriteNotifier = new QSocketNotifier(Socket, QSocketNotifier::Write, this);
  47. connect(WriteNotifier, SIGNAL(activated(int)), this, SLOT(dataSent()));
  48. Started = true;
  49. enable();
  50. kdebugf2();
  51. }
  52. void GaduSocketNotifiers::deleteSocketNotifiers()
  53. {
  54. kdebugf();
  55. if (!Started)
  56. return;
  57. Started = false;
  58. ReadNotifier->setEnabled(false);
  59. ReadNotifier->deleteLater();
  60. ReadNotifier = 0;
  61. WriteNotifier->setEnabled(false);
  62. WriteNotifier->deleteLater();
  63. WriteNotifier = 0;
  64. if (TimeoutTimer)
  65. {
  66. TimeoutTimer->stop();
  67. TimeoutTimer->deleteLater();
  68. TimeoutTimer = 0;
  69. }
  70. kdebugf2();
  71. }
  72. void GaduSocketNotifiers::disable()
  73. {
  74. kdebugf();
  75. if (!Started)
  76. return;
  77. ReadNotifier->setEnabled(false);
  78. WriteNotifier->setEnabled(false);
  79. if (TimeoutTimer)
  80. TimeoutTimer->stop();
  81. }
  82. void GaduSocketNotifiers::enable()
  83. {
  84. kdebugf();
  85. if (!Started)
  86. return;
  87. ReadNotifier->setEnabled(checkRead());
  88. WriteNotifier->setEnabled(checkWrite());
  89. int tout = timeout();
  90. if (tout > 0)
  91. {
  92. if (!TimeoutTimer)
  93. {
  94. TimeoutTimer = new QTimer(this);
  95. TimeoutTimer->setSingleShot(true);
  96. connect(TimeoutTimer, SIGNAL(timeout()), this, SLOT(socketTimeout()));
  97. }
  98. TimeoutTimer->start(tout);
  99. }
  100. else if (TimeoutTimer)
  101. {
  102. TimeoutTimer->stop();
  103. TimeoutTimer->deleteLater();
  104. TimeoutTimer = 0;
  105. }
  106. }
  107. void GaduSocketNotifiers::watchFor(int socket)
  108. {
  109. kdebugmf(KDEBUG_NETWORK | KDEBUG_INFO, "notifier: %p, old socket: %d, socket: %d\n", this, Socket, socket);
  110. if (Socket == socket)
  111. return;
  112. Socket = socket;
  113. createSocketNotifiers();
  114. }
  115. void GaduSocketNotifiers::socketTimeout()
  116. {
  117. kdebugf();
  118. if (!handleSoftTimeout())
  119. connectionTimeout();
  120. }
  121. void GaduSocketNotifiers::dataReceived()
  122. {
  123. kdebugf();
  124. disable();
  125. socketEvent();
  126. enable();
  127. kdebugf2();
  128. }
  129. void GaduSocketNotifiers::dataSent()
  130. {
  131. kdebugf();
  132. disable();
  133. socketEvent();
  134. enable();
  135. kdebugf2();
  136. }
  137. #include "moc_gadu-socket-notifiers.cpp"