From 2b203c461655eb871a3e6f7f04dc6dce00a88092 Mon Sep 17 00:00:00 2001 From: Benjamin Sergeant Date: Wed, 7 Nov 2018 12:24:08 -0800 Subject: [PATCH] Add missing files ... --- ixwebsocket/IXEventFd.cpp | 82 +++++++++++++++++++++++++++++++++++++++ ixwebsocket/IXEventFd.h | 23 +++++++++++ 2 files changed, 105 insertions(+) create mode 100644 ixwebsocket/IXEventFd.cpp create mode 100644 ixwebsocket/IXEventFd.h diff --git a/ixwebsocket/IXEventFd.cpp b/ixwebsocket/IXEventFd.cpp new file mode 100644 index 00000000..607d1cba --- /dev/null +++ b/ixwebsocket/IXEventFd.cpp @@ -0,0 +1,82 @@ +/* + * IXEventFd.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. + */ + +// +// Linux/Android has a special type of virtual files. select(2) will react +// when reading/writing to those files, unlike closing sockets. +// +// https://linux.die.net/man/2/eventfd +// http://www.sourcexr.com/articles/2013/10/26/lightweight-inter-process-signaling-with-eventfd +// +// eventfd was added in Linux kernel 2.x, and our oldest Android (Kitkat 4.4) +// is on Kernel 3.x +// +// cf Android/Kernel table here +// https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel +// + +#include "IXEventFd.h" + +#ifdef __linux__ +# include +#endif + +#ifndef _WIN32 +#include // for write +#endif + +namespace ix +{ + EventFd::EventFd() : + _eventfd(-1) + { +#ifdef __linux__ + _eventfd = eventfd(0, 0); +#endif + } + + EventFd::~EventFd() + { +#ifdef __linux__ + ::close(_eventfd); +#endif + } + + bool EventFd::notify() + { +#if defined(__linux__) + if (_eventfd == -1) return false; + + // select will wake up when a non-zero value is written to our eventfd + uint64_t value = 1; + + // we should write 8 bytes for an uint64_t + return write(_eventfd, &value, sizeof(value)) == 8; +#else + return true; +#endif + } + + bool EventFd::clear() + { +#if defined(__linux__) + if (_eventfd == -1) return false; + + // 0 is a special value ; select will not wake up + uint64_t value = 0; + + // we should write 8 bytes for an uint64_t + return write(_eventfd, &value, sizeof(value)) == 8; +#else + return true; +#endif + } + + int EventFd::getFd() + { + return _eventfd; + } +} diff --git a/ixwebsocket/IXEventFd.h b/ixwebsocket/IXEventFd.h new file mode 100644 index 00000000..3c263a83 --- /dev/null +++ b/ixwebsocket/IXEventFd.h @@ -0,0 +1,23 @@ +/* + * IXEventFd.h + * Author: Benjamin Sergeant + * Copyright (c) 2018 Machine Zone, Inc. All rights reserved. + */ + +#pragma once + +namespace ix +{ + class EventFd { + public: + EventFd(); + virtual ~EventFd(); + + bool notify(); + bool clear(); + int getFd(); + + private: + int _eventfd; + }; +}