qftrans/csocketmanager.cpp
2009-04-20 13:29:36 +02:00

114 lines
3.6 KiB
C++

/***************************************************************************
* Copyright (C) 2008 by Oliver Groß *
* z.o.gross@gmx.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "csocketmanager.h"
#include "cworker.h"
#include <QTcpServer>
#include <QThread>
namespace qftrans {
CSocketManager::CSocketManager(QObject * parent) : QObject(parent),
m_Server(NULL), m_Socket(NULL), m_Worker(NULL) {
}
CSocketManager::~CSocketManager() {
if (m_Socket)
delete m_Socket;
}
void CSocketManager::setup(bool server, quint16 port, QString & host) {
m_Host = host;
m_Port = port;
if (server) {
m_Server = new QTcpServer(this); //TODO proper setup server
connect(m_Server, SIGNAL(newConnection()), this, SLOT(handleServerConnected()));
m_Server->listen(QHostAddress(host), port);
}
else {
m_Socket = new QTcpSocket(); //TODO proper setup client
connect(m_Socket, SIGNAL(connected()), this, SLOT(handleClientConnected()));
connect(m_Socket, SIGNAL(disconnected()), this, SLOT(handleDisconnected()));
m_Socket->connectToHost(host, port);
}
}
void CSocketManager::handleServerConnected() {
qDebug() << "srv conn... thread id" << QThread::currentThreadId();
m_Socket = m_Server->nextPendingConnection();
connect(m_Socket, SIGNAL(disconnected()), this, SLOT(handleDisconnected()));
m_Worker = new CWorker(m_Socket, m_Socket);
// emit workerReady(m_Worker);
emit connectionChanged(true);
m_Server->close();
}
void CSocketManager::handleClientConnected() {
qDebug() << "clt conn... thread id" << QThread::currentThreadId();
m_Worker = new CWorker(m_Socket, m_Socket);
emit connectionChanged(true);
}
void CSocketManager::handleDisconnected() {
qDebug() << "clt/srv disconn.. thread id" << QThread::currentThreadId();
/* m_Worker->deleteLater();
m_Worker = NULL;*/
// if (m_ActAsServer) {
// m_Socket->deleteLater();
// m_Socket = NULL;
//
// m_Server->listen(, 8899);
// emit connectionChanged(false);
// }
// else {
emit connectionChanged(false);
if (m_Server) {
m_Socket->deleteLater();
m_Socket = NULL;
m_Worker = NULL;
m_Server->listen(QHostAddress(m_Host), m_Port);
}
else
QThread::currentThread()->quit();
// }
}
/*
void CSocketManager::connectToPeer() {
if (!m_Server && m_Socket)
m_Socket->connectToHost(host, port);
}*/
// void CSocketManager::disconnectFromPeer() {
// if (m_Socket)
// m_Socket->disconnectFromHost();
// }
}