Async read from Poco HTTPClientSession
Asked Answered
T

1

9

the usual sample code for using HTTPClientSession goes something like this:

Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
std::ostream& ostr = session.sendRequest(req);

// Receive the response.
Poco::Net::HTTPResponse res;
std::istream& rs = session.receiveResponse(res);

the question is, how can I read from the rs input stream the entire data, while making sure that all of operations are non-blocking, so that I can cancel them at any time?

Thorndike answered 29/4, 2013 at 14:38 Comment(0)
B
2

Just an idea, Try to put your code inside a thread.

http://pocoproject.org/slides/130-Threads.pdf

Regards

Barrus answered 29/4, 2013 at 15:43 Comment(5)
how would that help? I would just get this new thread blocked for an indefinite amount of timeThorndike
If you put your session.receiveResponse() inside a thread, you shouldn't worry about if it is blocking or not because you can run your main code in separated thread. If you want to cancel the blocking section, you could kill the thread or stop it in a ordered way. To exchange data between threads use references/pointers or queues synchronized with mutexes for example. If you want to block the receiveResponse() for a time you should look Time-Alives(setKeepAliveTimeout method) and timeouts. Good luckBarrus
Ok, then try with HTTPClientSession::setKeepAliveTimeout() method to configure a small TimeOut for your current connection. From documentation, "The stream is valid until sendRequest() is called or the session is destroyed", so if your rs stream doesn't receive nothing for a time period, it should continue without problems.Barrus
You can abort the blocking read on the HTTPClientSession by calling HTTPClientSession::abort() from another thread.Kenton
@Günter: calling abort() from another thread is not a viable option on Android (it works well on OSX and iOS though...)Tanika

© 2022 - 2024 — McMap. All rights reserved.