IXWebSocket/ixwebsocket/IXCancellationRequest.cpp

36 lines
940 B
C++
Raw Permalink Normal View History

2019-01-03 21:53:44 +01:00
/*
* IXCancellationRequest.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
*/
#include "IXCancellationRequest.h"
#include <cassert>
#include <chrono>
2019-01-03 21:53:44 +01:00
namespace ix
2019-01-03 21:53:44 +01:00
{
2019-09-23 19:25:23 +02:00
CancellationRequest makeCancellationRequestWithTimeout(
int secs, std::atomic<bool>& requestInitCancellation)
2019-01-03 21:53:44 +01:00
{
assert(secs > 0);
2019-01-03 21:53:44 +01:00
auto start = std::chrono::system_clock::now();
auto timeout = std::chrono::seconds(secs);
2019-09-23 19:25:23 +02:00
auto isCancellationRequested = [&requestInitCancellation, start, timeout]() -> bool {
2019-01-03 21:53:44 +01:00
// Was an explicit cancellation requested ?
if (requestInitCancellation) return true;
2019-01-03 21:53:44 +01:00
auto now = std::chrono::system_clock::now();
if ((now - start) > timeout) return true;
// No cancellation request
return false;
};
return isCancellationRequested;
}
2019-09-23 19:25:23 +02:00
} // namespace ix