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.

114 lines
3.6KB

  1. /***************************************************************************
  2. * Copyright (C) 2008 by Oliver Groß *
  3. * z.o.gross@gmx.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #include "csocketmanager.h"
  21. #include "cworker.h"
  22. #include <QTcpServer>
  23. #include <QThread>
  24. namespace qftrans {
  25. CSocketManager::CSocketManager(QObject * parent) : QObject(parent),
  26. m_Server(NULL), m_Socket(NULL), m_Worker(NULL) {
  27. }
  28. CSocketManager::~CSocketManager() {
  29. if (m_Socket)
  30. delete m_Socket;
  31. }
  32. void CSocketManager::setup(bool server, quint16 port, QString & host) {
  33. m_Host = host;
  34. m_Port = port;
  35. if (server) {
  36. m_Server = new QTcpServer(this); //TODO proper setup server
  37. connect(m_Server, SIGNAL(newConnection()), this, SLOT(handleServerConnected()));
  38. m_Server->listen(QHostAddress(host), port);
  39. }
  40. else {
  41. m_Socket = new QTcpSocket(); //TODO proper setup client
  42. connect(m_Socket, SIGNAL(connected()), this, SLOT(handleClientConnected()));
  43. connect(m_Socket, SIGNAL(disconnected()), this, SLOT(handleDisconnected()));
  44. m_Socket->connectToHost(host, port);
  45. }
  46. }
  47. void CSocketManager::handleServerConnected() {
  48. qDebug() << "srv conn... thread id" << QThread::currentThreadId();
  49. m_Socket = m_Server->nextPendingConnection();
  50. connect(m_Socket, SIGNAL(disconnected()), this, SLOT(handleDisconnected()));
  51. m_Worker = new CWorker(m_Socket, m_Socket);
  52. // emit workerReady(m_Worker);
  53. emit connectionChanged(true);
  54. m_Server->close();
  55. }
  56. void CSocketManager::handleClientConnected() {
  57. qDebug() << "clt conn... thread id" << QThread::currentThreadId();
  58. m_Worker = new CWorker(m_Socket, m_Socket);
  59. emit connectionChanged(true);
  60. }
  61. void CSocketManager::handleDisconnected() {
  62. qDebug() << "clt/srv disconn.. thread id" << QThread::currentThreadId();
  63. /* m_Worker->deleteLater();
  64. m_Worker = NULL;*/
  65. // if (m_ActAsServer) {
  66. // m_Socket->deleteLater();
  67. // m_Socket = NULL;
  68. //
  69. // m_Server->listen(, 8899);
  70. // emit connectionChanged(false);
  71. // }
  72. // else {
  73. emit connectionChanged(false);
  74. if (m_Server) {
  75. m_Socket->deleteLater();
  76. m_Socket = NULL;
  77. m_Worker = NULL;
  78. m_Server->listen(QHostAddress(m_Host), m_Port);
  79. }
  80. else
  81. QThread::currentThread()->quit();
  82. // }
  83. }
  84. /*
  85. void CSocketManager::connectToPeer() {
  86. if (!m_Server && m_Socket)
  87. m_Socket->connectToHost(host, port);
  88. }*/
  89. // void CSocketManager::disconnectFromPeer() {
  90. // if (m_Socket)
  91. // m_Socket->disconnectFromHost();
  92. // }
  93. }