예제 #1
0
void GaduSocketNotifiers::createSocketNotifiers()
{
	kdebugf();

	deleteSocketNotifiers();

	if (0 >= Socket)
		return;

	ReadNotifier = new QSocketNotifier(Socket, QSocketNotifier::Read, this);
	connect(ReadNotifier, SIGNAL(activated(int)), this, SLOT(dataReceived()));
	if (!checkRead())
		ReadNotifier->setEnabled(false);

	WriteNotifier = new QSocketNotifier(Socket, QSocketNotifier::Write, this);
	connect(WriteNotifier, SIGNAL(activated(int)), this, SLOT(dataSent()));
	if (!checkWrite())
		WriteNotifier->setEnabled(false);

	TimeoutTimer = new QTimer();
	TimeoutTimer->setSingleShot(true);
	connect(TimeoutTimer, SIGNAL(timeout()), this, SLOT(socketTimeout()));

	Started = true;

	int tout = timeout();
	if (0 < tout)
		TimeoutTimer->start(tout);

	kdebugf2();
}
void TcpSocketThread::run() {

    _timeoutTimer = new QTimer(this);
    _timeoutTimer->moveToThread(this);
    _timeoutTimer->setInterval(SOCKET_TIMEOUT);
    connect(_timeoutTimer, SIGNAL(timeout()), this, SLOT(socketTimeout()));
    _timeoutTimer->start(SOCKET_TIMEOUT);

    // Ist noch kein Socket Deskriptor gesetzt, so ist beim setzen ein
    // Fehler aufgetreten, und wir beenden diesen Thread...
    if (_socket.socketDescriptor() == -1) {
        logError("Socket descriptor for this socket not set. Exit tcp thread...");
        return;
    }

    // IP Adresse & Port loggen...
    logNotice( QString("Client connection from IP %1, port %2, to port %3")
                    .arg(_socket.localAddress().toString(),
                         QString::number( _socket.localPort() ),
                         QString::number( _socket.peerPort() )
                         )
               );

    logDebug("Thread for the connection started.");

    logDebug("Install eventhandler, to close this thread, when tcp connection closed.");
    connect(&_socket, SIGNAL(disconnected()), this, SLOT(quit()));
    connect(&_socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(socketError(QAbstractSocket::SocketError)));

    // Wenn neue Daten zum lesen vorhanden sind:
    connect(&_socket, SIGNAL(readyRead()), this, SLOT(newDataToRead()));

    // Datenstream auf Socket setzen:
    _socketBlockSize = 0;

    /**
      * Nun fangen wir an, am Tcp Port zu lauschen...
      */
    logDebug("And now, i am waiting for data to read...");

    exec();
}