vmime/examples/example6_timeoutHandler.hpp

61 lines
1.3 KiB
C++
Raw Normal View History

2016-03-02 19:33:55 +00:00
#include <ctime>
2014-03-16 21:52:40 +00:00
/** Time out handler.
* Used to stop the current operation after too much time, or if the user
* requested cancellation.
*/
2018-09-05 21:54:48 +00:00
class timeoutHandler : public vmime::net::timeoutHandler {
2014-03-16 21:52:40 +00:00
public:
2016-03-02 19:33:55 +00:00
timeoutHandler()
2018-09-05 21:54:48 +00:00
: m_start(time(NULL)) {
2016-03-02 19:33:55 +00:00
}
2018-09-05 21:54:48 +00:00
bool isTimeOut() {
2014-03-16 21:52:40 +00:00
// This is a cancellation point: return true if you want to cancel
// the current operation. If you return true, handleTimeOut() will
// be called just after this, and before actually cancelling the
// operation
2016-03-02 19:33:55 +00:00
// 10 seconds timeout
return (time(NULL) - m_start) >= 10; // seconds
2014-03-16 21:52:40 +00:00
}
2018-09-05 21:54:48 +00:00
void resetTimeOut() {
2014-03-16 21:52:40 +00:00
// Called at the beginning of an operation (eg. connecting,
// a read() or a write() on a socket...)
2016-03-02 19:33:55 +00:00
m_start = time(NULL);
2014-03-16 21:52:40 +00:00
}
2018-09-05 21:54:48 +00:00
bool handleTimeOut() {
2014-03-16 21:52:40 +00:00
// If isTimeOut() returned true, this function will be called. This
// allows you to interact with the user, ie. display a prompt to
// know whether he wants to cancel the operation.
2016-03-02 19:33:55 +00:00
// If you return false here, the operation will be actually cancelled.
// If true, the time out is reset and the operation continues.
return false;
2014-03-16 21:52:40 +00:00
}
2016-03-02 19:33:55 +00:00
private:
time_t m_start;
2014-03-16 21:52:40 +00:00
};
2018-09-05 21:54:48 +00:00
class timeoutHandlerFactory : public vmime::net::timeoutHandlerFactory {
2014-03-16 21:52:40 +00:00
public:
2018-09-05 21:54:48 +00:00
vmime::shared_ptr <vmime::net::timeoutHandler> create() {
2014-03-16 21:52:40 +00:00
return vmime::make_shared <timeoutHandler>();
}
};