This commit is contained in:
Adrian Lucrèce Céleste 2019-08-21 12:01:33 +00:00 committed by GitHub
commit d83b47645a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 67 additions and 52 deletions

View File

@ -64,7 +64,8 @@ App::App(IEventQueue* events, CreateTaskBarReceiverFunc createTaskBarReceiver, A
m_createTaskBarReceiver(createTaskBarReceiver), m_createTaskBarReceiver(createTaskBarReceiver),
m_appUtil(events), m_appUtil(events),
m_ipcClient(nullptr), m_ipcClient(nullptr),
m_socketMultiplexer(nullptr) m_socketMultiplexer(nullptr),
m_stdin(nullptr)
{ {
assert(s_instance == nullptr); assert(s_instance == nullptr);
s_instance = this; s_instance = this;
@ -74,6 +75,10 @@ App::~App()
{ {
s_instance = nullptr; s_instance = nullptr;
delete m_args; delete m_args;
if (m_stdin) {
delete(m_stdin);
}
} }
void void
@ -216,6 +221,14 @@ App::cleanupIpcClient()
delete m_ipcClient; delete m_ipcClient;
} }
void
App::initStdinListen()
{
#if !defined(_WIN32)
m_stdin = new StdinListen(m_events);
#endif
}
void void
App::handleIpcMessage(const Event& e, void*) App::handleIpcMessage(const Event& e, void*)
{ {

View File

@ -23,6 +23,7 @@
#include "base/String.h" #include "base/String.h"
#include "base/Log.h" #include "base/Log.h"
#include "base/EventQueue.h" #include "base/EventQueue.h"
#include "base/StdinListen.h"
#include "common/common.h" #include "common/common.h"
#if SYSAPI_WIN32 #if SYSAPI_WIN32
@ -106,6 +107,7 @@ private:
protected: protected:
void initIpcClient(); void initIpcClient();
void cleanupIpcClient(); void cleanupIpcClient();
void initStdinListen();
void runEventsLoop(void*); void runEventsLoop(void*);
IArchTaskBarReceiver* m_taskBarReceiver; IArchTaskBarReceiver* m_taskBarReceiver;
@ -120,6 +122,7 @@ private:
ARCH_APP_UTIL m_appUtil; ARCH_APP_UTIL m_appUtil;
IpcClient* m_ipcClient; IpcClient* m_ipcClient;
SocketMultiplexer* m_socketMultiplexer; SocketMultiplexer* m_socketMultiplexer;
StdinListen* m_stdin;
}; };
class MinimalApp : public App { class MinimalApp : public App {

View File

@ -455,6 +455,11 @@ ClientApp::mainLoop()
initIpcClient(); initIpcClient();
} }
// setup polling for stdin if not running in daemon mode
if (!argsBase().m_daemon) {
initStdinListen();
}
// run event loop. if startClient() failed we're supposed to retry // run event loop. if startClient() failed we're supposed to retry
// later. the timer installed by startClient() will take care of // later. the timer installed by startClient() will take care of
// that. // that.

View File

@ -748,6 +748,11 @@ ServerApp::mainLoop()
initIpcClient(); initIpcClient();
} }
// setup polling for stdin if not running in daemon mode
if (!argsBase().m_daemon) {
initStdinListen();
}
// handle hangup signal by reloading the server's configuration // handle hangup signal by reloading the server's configuration
ARCH->setSignalHandler(Arch::kHANGUP, &reloadSignalHandler, NULL); ARCH->setSignalHandler(Arch::kHANGUP, &reloadSignalHandler, NULL);
m_events->adoptHandler(m_events->forServerApp().reloadConfig(), m_events->adoptHandler(m_events->forServerApp().reloadConfig(),

View File

@ -201,23 +201,11 @@ EventQueue::adoptBuffer(IEventQueueBuffer* buffer)
} }
} }
bool
EventQueue::parent_requests_shutdown() const
{
char ch;
return m_parentStream.try_read_char(ch) && ch == ShutdownCh;
}
bool bool
EventQueue::getEvent(Event& event, double timeout) EventQueue::getEvent(Event& event, double timeout)
{ {
Stopwatch timer(true); Stopwatch timer(true);
retry: retry:
// before handling any events make sure we don't need to shutdown
if (parent_requests_shutdown()) {
event = Event(Event::kQuit);
return false;
}
// if no events are waiting then handle timers and then wait // if no events are waiting then handle timers and then wait
while (m_buffer->isEmpty()) { while (m_buffer->isEmpty()) {
// handle timers first // handle timers first

View File

@ -26,7 +26,6 @@
#include "base/Stopwatch.h" #include "base/Stopwatch.h"
#include "common/stdmap.h" #include "common/stdmap.h"
#include "common/stdset.h" #include "common/stdset.h"
#include "base/NonBlockingStream.h"
#include <queue> #include <queue>
@ -73,7 +72,6 @@ private:
bool hasTimerExpired(Event& event); bool hasTimerExpired(Event& event);
double getNextTimerTimeout() const; double getNextTimerTimeout() const;
void addEventToBuffer(const Event& event); void addEventToBuffer(const Event& event);
bool parent_requests_shutdown() const;
private: private:
class Timer { class Timer {
@ -186,7 +184,6 @@ private:
Mutex* m_readyMutex; Mutex* m_readyMutex;
CondVar<bool>* m_readyCondVar; CondVar<bool>* m_readyCondVar;
std::queue<Event> m_pending; std::queue<Event> m_pending;
NonBlockingStream m_parentStream;
}; };
#define EVENT_TYPE_ACCESSOR(type_) \ #define EVENT_TYPE_ACCESSOR(type_) \

View File

@ -17,44 +17,47 @@
#if !defined(_WIN32) #if !defined(_WIN32)
#include "base/NonBlockingStream.h" #include "base/StdinListen.h"
#include "../gui/src/ShutdownCh.h"
#include <unistd.h> // tcgetattr/tcsetattr, read #include <unistd.h> // tcgetattr/tcsetattr, read
#include <termios.h> // tcgetattr/tcsetattr #include <termios.h> // tcgetattr/tcsetattr
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
NonBlockingStream::NonBlockingStream(int fd) : StdinListen::StdinListen(IEventQueue* events) :
_fd(fd) m_events(events),
m_thread(NULL)
{ {
// disable ICANON & ECHO so we don't have to wait for a newline // disable ICANON & ECHO so we don't have to wait for a newline
// before we get data (and to keep it from being echoed back out) // before we get data (and to keep it from being echoed back out)
termios ta; termios ta;
tcgetattr(fd, &ta); tcgetattr(STDIN_FILENO, &ta);
_p_ta_previous = new termios(ta); _p_ta_previous = new termios(ta);
ta.c_lflag &= ~(ICANON | ECHO); ta.c_lflag &= ~(ICANON | ECHO);
tcsetattr(fd, TCSANOW, &ta); tcsetattr(STDIN_FILENO, TCSANOW, &ta);
// prevent IO from blocking so we can poll (read()) m_thread = new Thread(new TMethodJob<StdinListen>(
int _cntl_previous = fcntl(fd, F_GETFL); this, &StdinListen::stdinThread));
fcntl(fd, F_SETFL, _cntl_previous | O_NONBLOCK);
} }
NonBlockingStream::~NonBlockingStream() StdinListen::~StdinListen()
{ {
tcsetattr(_fd, TCSANOW, _p_ta_previous); tcsetattr(STDIN_FILENO, TCSANOW, _p_ta_previous);
fcntl(_fd, F_SETFL, _cntl_previous);
delete _p_ta_previous; delete _p_ta_previous;
} }
bool NonBlockingStream::try_read_char(char &ch) const void
StdinListen::stdinThread(void *)
{ {
int result = read(_fd, &ch, 1); char ch;
if (result == 1)
return true; while (1) {
assert(result == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)); if (read(STDIN_FILENO, &ch, 1) == 1) {
return false; if (ch == ShutdownCh) {
m_events->addEvent(Event(Event::kQuit));
break;
}
}
}
} }
#endif // !defined(_WIN32) #endif // !defined(_WIN32)

View File

@ -17,33 +17,34 @@
#pragma once #pragma once
// windows doesn't have a unistd.h so this class won't work as-written.
// at the moment barrier doesn't need this functionality on windows so
// it's left as a stub to be optimized out
#if defined(_WIN32) #if defined(_WIN32)
class NonBlockingStream class StdinListen
{ {
public: public:
bool try_read_char(char &ch) const { return false; }; void stdinThread(void*) {};
}; };
#else // non-windows platforms #else
#include "base/IEventQueue.h"
#include "base/TMethodJob.h"
#include "mt/Thread.h"
struct termios; struct termios;
class NonBlockingStream class StdinListen
{ {
public: public:
explicit NonBlockingStream(int fd = 0); explicit StdinListen(IEventQueue* events);
~NonBlockingStream(); ~StdinListen();
bool try_read_char(char &ch) const;
private: private:
int _fd; void stdinThread(void*);
termios * _p_ta_previous;
int _cntl_previous; IEventQueue* m_events;
Thread* m_thread;
termios* _p_ta_previous;
}; };
#endif #endif