Add configurable I/O timeout

This commit is contained in:
pixl 2020-07-08 03:41:05 -04:00
parent 055d136b09
commit bc8f1a983a
No known key found for this signature in database
GPG Key ID: 1866C148CD593B6E
4 changed files with 40 additions and 5 deletions

View File

@ -25,6 +25,7 @@
using namespace logid;
using namespace libconfig;
using namespace std::chrono;
Configuration::Configuration(const std::string& config_file)
{
@ -49,6 +50,23 @@ Configuration::Configuration(const std::string& config_file)
return;
}
_io_timeout = LOGID_DEFAULT_RAWDEVICE_TIMEOUT;
try {
auto& timeout = root["io_timeout"];
if(timeout.isNumber()) {
auto t = timeout.getType();
if(timeout.getType() == libconfig::Setting::TypeFloat)
_io_timeout = duration_cast<milliseconds>(
duration<double, std::milli>(timeout));
else
_io_timeout = milliseconds((int)timeout);
} else
logPrintf(WARN, "Line %d: io_timeout must be a number.",
timeout.getSourceLine());
} catch(const SettingNotFoundException& e) {
// Ignore
}
for(int i = 0; i < devices->getLength(); i++) {
const Setting &device = (*devices)[i];
std::string name;
@ -90,3 +108,8 @@ const char * Configuration::DeviceNotFound::what() const noexcept
{
return _name.c_str();
}
std::chrono::milliseconds Configuration::ioTimeout() const
{
return _io_timeout;
}

View File

@ -22,6 +22,9 @@
#include <map>
#include <libconfig.h++>
#include <memory>
#include <chrono>
#define LOGID_DEFAULT_RAWDEVICE_TIMEOUT std::chrono::seconds(2)
namespace logid
{
@ -41,8 +44,11 @@ namespace logid
private:
std::string _name;
};
std::chrono::milliseconds ioTimeout() const;
private:
std::map<std::string, std::string> _device_paths;
std::chrono::milliseconds _io_timeout;
libconfig::Config _config;
};

View File

@ -22,6 +22,7 @@
#include "../dj/defs.h"
#include "../../util/log.h"
#include "../hidpp/Report.h"
#include "../../Configuration.h"
#include <string>
#include <system_error>
@ -258,8 +259,15 @@ int RawDevice::_readReport(std::vector<uint8_t>& report, std::size_t maxDataLeng
int ret;
report.resize(maxDataLength);
timeval timeout = { duration_cast<milliseconds>(HIDPP_IO_TIMEOUT).count(),
duration_cast<microseconds>(HIDPP_IO_TIMEOUT).count() };
timeval timeout{};
timeout.tv_sec = duration_cast<seconds>(global_config->ioTimeout())
.count();
timeout.tv_usec = duration_cast<microseconds>(
global_config->ioTimeout()).count() %
duration_cast<microseconds>(seconds(1)).count();
auto timeout_ms = duration_cast<milliseconds>(
global_config->ioTimeout()).count();
fd_set fds;
do {
@ -269,7 +277,7 @@ int RawDevice::_readReport(std::vector<uint8_t>& report, std::size_t maxDataLeng
ret = select(std::max(_fd, _pipe[0]) + 1,
&fds, nullptr, nullptr,
(HIDPP_IO_TIMEOUT.count() > 0 ? nullptr : &timeout));
(timeout_ms > 0 ? nullptr : &timeout));
} while(ret == -1 && errno == EINTR);
if(ret == -1)

View File

@ -30,8 +30,6 @@
#include "defs.h"
#include "../../util/mutex_queue.h"
#define HIDPP_IO_TIMEOUT std::chrono::seconds(2)
namespace logid {
namespace backend {
namespace raw