1

I've this code here in a program I'm making but I've a problem, How can I keep the program waiting for data on http2 before returning to the tcpserver class? And how can I get the data in the tcpserver class?

This is like a checkpoint were I need to get the data from the server and then keep running the tcpserver and use that data there.

tcpserver.cpp

#include "tcpserver.h"
#include "protocol.h"
#include "http2.h"

QTextStream in(stdin);

tcpserver::tcpserver(QObject *parent) :
    QObject(parent)
{
    server = new QTcpServer(this);

    [ ... Other Server Stuff ... ]

    http2 *h = new http2(this); 

}

I've tried this with no luck:

http2.cpp

#include "http2.h"
bool httpdonne = false;
QByteArray finaldata;

http2::http2(QObject *parent, QByteArray url, QByteArray data) :
    QObject(parent)
{
    url.append(data);

    m_manager = new QNetworkAccessManager(this);
    connect(m_manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(httpdown(QNetworkReply*)));

    QNetworkRequest request;
    request.setUrl(QUrl(url));
    request.setRawHeader("User-Agent", "MyOwnBrowser 1.0");

    m_manager->get(request);

    while ( httpdonne == false ) {

    }

    finaldata.append("HTTP: ");
    qDebug() << finaldata;

}

QByteArray http2::httpdown(QNetworkReply* result)
{
    QByteArray data = result->readAll();
    finaldata = data;
    httpdonne = true;
    return data;
}

Thanks a lot! ;)

TCB13
  • 3,067
  • 2
  • 39
  • 68
  • 1
    Blocking in a function call until the data is ready is generally a poor design... for example, what happens if someone pulls the client's Ethernet cable out of the wall during the HTTP transaction? A: Your program will lock up for several minutes, until the TCP connection times out. If possible, you'd be better off designing your program in an event-driven manner that never blocks inside a function call, waiting for the network. – Jeremy Friesner Aug 27 '11 at 02:51
  • But the problem is... to make use of a custom signal I will need to split my tcpserver class in two right? It would make a mess of my program and I would need to deal with other security concerns then since for instance when a client send a message and it's not verified yet agains the php server he would be able to send another... What's the best way to deal with the situation? – TCB13 Aug 27 '11 at 16:42

4 Answers4

2

Qt makes use of signals and slots and the event loop. If you have no QEventLoop running which is normally provided by QApplication your events will not be handled.

Take a peek at this question to see how to create an event loop to simulate a blocking (synchronous) using the asynchronous programming model.

In addition I normally add a QTimer too:

QEventLoop loop;
..
QTimer timer;
timer.setInterval(2000);
timer.setSingleShot(true);
loop.connect(&timer, SIGNAL(timeout()), SLOT(handleTimeout()));
timer.start();
..
//Setup your objects / connections here...
..
loop.exec(); //Your signals and slots will be triggered / handled now!
Community
  • 1
  • 1
Derick Schoonbee
  • 2,971
  • 1
  • 23
  • 39
  • 2
    Keep in mind you normally do not need blocking calls except for very special cases. – Derick Schoonbee Aug 27 '11 at 15:52
  • You answer is good it works but I ended creating custom signals and slots to deal with this... Everyone was telling that the best option is to asynchronous and now I do understand that. - Thanks for the info, it can be useful in other circumstances. – TCB13 Aug 28 '11 at 13:15
  • I can't accept to answers so... everyone reading this after me should also read the next one by blueskin. – TCB13 Aug 28 '11 at 13:17
1

Qt network classes are all signal-driven. That is, you create functions which handle various events (data ready etc) and connect them to the appropriate slots. Blocking isn't really the way they are to be used.

dragonroot
  • 5,653
  • 3
  • 38
  • 63
0

You can use my WaitForSignalHelper class posted here , it does what you want: wait for a timeout OR signal to be emitted...

Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151
0

Of course I would never recommend what you are trying to achieve but if you still wanna "shoot yourself in the foot" :) then here's what you can try,

run the manager as a separate thread and make the http2 or tcpserver thread wait on a condition variable. And when the manager is done invoke the threads waiting the the condition variable.

Chenna V
  • 10,185
  • 11
  • 77
  • 104
  • Thanks for the feedback, C is all about that allowing the programmer to shot itself in the foot but.. this C++ classes and QT stuff is a little bit confusing for me... someone who professionally program for AVR/PICs and are trying to develop for computers at home ;) – TCB13 Aug 27 '11 at 16:59
  • I understand .... But learning is Qt worth it. Id say it is one of the most elegant library I've known. and especially if you learn it by understanding the inner implementation details then you'll learn a lot about design and programming. – Chenna V Aug 27 '11 at 17:03
  • blueskin - Is there any good book you can recommend specially for someone who's trying to learn C++ specially using QT with a C background? With code examples.. Thanks. – TCB13 Aug 27 '11 at 17:37
  • I'd recommend http://www.amazon.com/Programming-Prentice-Source-Software-Development/dp/0132354160 and for reference use the online Qt documentation. The book is a bit old but still a good reference. – Chenna V Aug 28 '11 at 16:10