Add configurable I/O timeout
This commit is contained in:
		@@ -25,6 +25,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
using namespace logid;
 | 
					using namespace logid;
 | 
				
			||||||
using namespace libconfig;
 | 
					using namespace libconfig;
 | 
				
			||||||
 | 
					using namespace std::chrono;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Configuration::Configuration(const std::string& config_file)
 | 
					Configuration::Configuration(const std::string& config_file)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -49,6 +50,23 @@ Configuration::Configuration(const std::string& config_file)
 | 
				
			|||||||
        return;
 | 
					        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++) {
 | 
					    for(int i = 0; i < devices->getLength(); i++) {
 | 
				
			||||||
        const Setting &device = (*devices)[i];
 | 
					        const Setting &device = (*devices)[i];
 | 
				
			||||||
        std::string name;
 | 
					        std::string name;
 | 
				
			||||||
@@ -90,3 +108,8 @@ const char * Configuration::DeviceNotFound::what() const noexcept
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    return _name.c_str();
 | 
					    return _name.c_str();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					std::chrono::milliseconds Configuration::ioTimeout() const
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    return _io_timeout;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,6 +22,9 @@
 | 
				
			|||||||
#include <map>
 | 
					#include <map>
 | 
				
			||||||
#include <libconfig.h++>
 | 
					#include <libconfig.h++>
 | 
				
			||||||
#include <memory>
 | 
					#include <memory>
 | 
				
			||||||
 | 
					#include <chrono>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define LOGID_DEFAULT_RAWDEVICE_TIMEOUT std::chrono::seconds(2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
namespace logid
 | 
					namespace logid
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
@@ -41,8 +44,11 @@ namespace logid
 | 
				
			|||||||
        private:
 | 
					        private:
 | 
				
			||||||
            std::string _name;
 | 
					            std::string _name;
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        std::chrono::milliseconds ioTimeout() const;
 | 
				
			||||||
    private:
 | 
					    private:
 | 
				
			||||||
        std::map<std::string, std::string> _device_paths;
 | 
					        std::map<std::string, std::string> _device_paths;
 | 
				
			||||||
 | 
					        std::chrono::milliseconds _io_timeout;
 | 
				
			||||||
        libconfig::Config _config;
 | 
					        libconfig::Config _config;
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,6 +22,7 @@
 | 
				
			|||||||
#include "../dj/defs.h"
 | 
					#include "../dj/defs.h"
 | 
				
			||||||
#include "../../util/log.h"
 | 
					#include "../../util/log.h"
 | 
				
			||||||
#include "../hidpp/Report.h"
 | 
					#include "../hidpp/Report.h"
 | 
				
			||||||
 | 
					#include "../../Configuration.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <string>
 | 
					#include <string>
 | 
				
			||||||
#include <system_error>
 | 
					#include <system_error>
 | 
				
			||||||
@@ -258,8 +259,15 @@ int RawDevice::_readReport(std::vector<uint8_t>& report, std::size_t maxDataLeng
 | 
				
			|||||||
    int ret;
 | 
					    int ret;
 | 
				
			||||||
    report.resize(maxDataLength);
 | 
					    report.resize(maxDataLength);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    timeval timeout = { duration_cast<milliseconds>(HIDPP_IO_TIMEOUT).count(),
 | 
					    timeval timeout{};
 | 
				
			||||||
                       duration_cast<microseconds>(HIDPP_IO_TIMEOUT).count() };
 | 
					    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;
 | 
					    fd_set fds;
 | 
				
			||||||
    do {
 | 
					    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,
 | 
					        ret = select(std::max(_fd, _pipe[0]) + 1,
 | 
				
			||||||
                     &fds, nullptr, nullptr,
 | 
					                     &fds, nullptr, nullptr,
 | 
				
			||||||
                     (HIDPP_IO_TIMEOUT.count() > 0 ? nullptr : &timeout));
 | 
					                     (timeout_ms > 0 ? nullptr : &timeout));
 | 
				
			||||||
    } while(ret == -1 && errno == EINTR);
 | 
					    } while(ret == -1 && errno == EINTR);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(ret == -1)
 | 
					    if(ret == -1)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -30,8 +30,6 @@
 | 
				
			|||||||
#include "defs.h"
 | 
					#include "defs.h"
 | 
				
			||||||
#include "../../util/mutex_queue.h"
 | 
					#include "../../util/mutex_queue.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define HIDPP_IO_TIMEOUT std::chrono::seconds(2)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
namespace logid {
 | 
					namespace logid {
 | 
				
			||||||
namespace backend {
 | 
					namespace backend {
 | 
				
			||||||
namespace raw
 | 
					namespace raw
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user