implement event loop for console applications (heavily modified patch 1715735)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46029 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
101
include/wx/private/fdiodispatcher.h
Normal file
101
include/wx/private/fdiodispatcher.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: wx/private/fdiodispatcher.h
|
||||
// Purpose: classes for dispatching IO notifications for file descriptors
|
||||
// Authors: Lukasz Michalski
|
||||
// Created: December 2006
|
||||
// Copyright: (c) Lukasz Michalski
|
||||
// RCS-ID: $Id$
|
||||
// Licence: wxWindows licence
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _WX_PRIVATE_FDIODISPATCHER_H_
|
||||
#define _WX_PRIVATE_FDIODISPATCHER_H_
|
||||
|
||||
#include "wx/hashmap.h"
|
||||
|
||||
// handler used to process events on descriptors
|
||||
class wxFDIOHandler
|
||||
{
|
||||
public:
|
||||
// called when descriptor is available for non-blocking read
|
||||
virtual void OnReadWaiting() = 0;
|
||||
|
||||
// called when descriptor is available for non-blocking write
|
||||
virtual void OnWriteWaiting() = 0;
|
||||
|
||||
// called when there is exception on descriptor
|
||||
virtual void OnExceptionWaiting() = 0;
|
||||
|
||||
// virtual dtor for the base class
|
||||
virtual ~wxFDIOHandler() { }
|
||||
};
|
||||
|
||||
// those flags describes sets where descriptor should be added
|
||||
enum wxFDIODispatcherEntryFlags
|
||||
{
|
||||
wxFDIO_INPUT = 1,
|
||||
wxFDIO_OUTPUT = 2,
|
||||
wxFDIO_EXCEPTION = 4,
|
||||
wxFDIO_ALL = wxFDIO_INPUT | wxFDIO_OUTPUT | wxFDIO_EXCEPTION
|
||||
};
|
||||
|
||||
struct wxFDIOHandlerEntry
|
||||
{
|
||||
wxFDIOHandlerEntry()
|
||||
{
|
||||
}
|
||||
|
||||
wxFDIOHandlerEntry(wxFDIOHandler *handler_, int flags_)
|
||||
: handler(handler_),
|
||||
flags(flags_)
|
||||
{
|
||||
}
|
||||
|
||||
wxFDIOHandler *handler;
|
||||
int flags;
|
||||
};
|
||||
|
||||
// this hash is used to map file descriptors to their handlers
|
||||
WX_DECLARE_HASH_MAP(
|
||||
int,
|
||||
wxFDIOHandlerEntry,
|
||||
wxIntegerHash,
|
||||
wxIntegerEqual,
|
||||
wxFDIOHandlerMap
|
||||
);
|
||||
|
||||
// base class for wxSelectDispatcher and wxEpollDispatcher
|
||||
//
|
||||
// notice that all pure virtual functions for FD management have implementation
|
||||
// in the base class and should be called from the derived classes
|
||||
class WXDLLIMPEXP_BASE wxFDIODispatcher
|
||||
{
|
||||
public:
|
||||
enum { TIMEOUT_INFINITE = -1 };
|
||||
|
||||
// find the handler for the given fd, return NULL if none
|
||||
wxFDIOHandler *FindHandler(int fd) const;
|
||||
|
||||
// register handler for the given descriptor with the dispatcher, return
|
||||
// true on success or false on error
|
||||
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags) = 0;
|
||||
|
||||
// modify descriptor flags or handler, return true on success
|
||||
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags) = 0;
|
||||
|
||||
// unregister descriptor previously registered with RegisterFD(), the
|
||||
// caller is responsible for deleting the returned handler pointer if
|
||||
// necessary
|
||||
virtual wxFDIOHandler *UnregisterFD(int fd, int flags) = 0;
|
||||
|
||||
// loops waiting for an event to happen on any of the descriptors
|
||||
virtual void RunLoop(int timeout) = 0;
|
||||
|
||||
virtual ~wxFDIODispatcher() { }
|
||||
|
||||
protected:
|
||||
// the fd -> handler map containing all the registered handlers
|
||||
wxFDIOHandlerMap m_handlers;
|
||||
};
|
||||
|
||||
#endif // _WX_PRIVATE_FDIODISPATCHER_H_
|
@@ -25,14 +25,14 @@ class WXDLLIMPEXP_CORE wxGSocketIOHandler : public wxFDIOHandler
|
||||
public:
|
||||
wxGSocketIOHandler(GSocket* socket);
|
||||
int GetFlags() const;
|
||||
void RemoveFlag(wxSelectDispatcherEntryFlags flag);
|
||||
void AddFlag(wxSelectDispatcherEntryFlags flag);
|
||||
void RemoveFlag(wxFDIODispatcherEntryFlags flag);
|
||||
void AddFlag(wxFDIODispatcherEntryFlags flag);
|
||||
|
||||
virtual void OnReadWaiting();
|
||||
virtual void OnWriteWaiting();
|
||||
virtual void OnExceptionWaiting();
|
||||
|
||||
private:
|
||||
virtual void OnReadWaiting(int fd);
|
||||
virtual void OnWriteWaiting(int fd);
|
||||
virtual void OnExceptionWaiting(int fd);
|
||||
|
||||
GSocket* m_socket;
|
||||
int m_flags;
|
||||
};
|
||||
|
@@ -1,8 +1,7 @@
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// Name: wx/private/selectdispatcher.h
|
||||
// Purpose: wxSelectDispatcher class
|
||||
// Authors: Lukasz Michalski
|
||||
// Modified by:
|
||||
// Authors: Lukasz Michalski and Vadim Zeitlin
|
||||
// Created: December 2006
|
||||
// Copyright: (c) Lukasz Michalski
|
||||
// RCS-ID: $Id$
|
||||
@@ -14,82 +13,100 @@
|
||||
|
||||
#include "wx/defs.h"
|
||||
|
||||
#include "wx/hashmap.h"
|
||||
#include "wx/private/fdiodispatcher.h"
|
||||
|
||||
static const int wxSELECT_TIMEOUT_INFINITE = -1;
|
||||
|
||||
// handler used to process events on descriptors
|
||||
class wxFDIOHandler
|
||||
// helper class storing all the select() fd sets
|
||||
class WXDLLIMPEXP_BASE wxSelectSets
|
||||
{
|
||||
public:
|
||||
// called when descriptor is available for non-blocking read
|
||||
virtual void OnReadWaiting(int fd) = 0;
|
||||
// ctor zeroes out all fd_sets
|
||||
wxSelectSets();
|
||||
|
||||
// called when descriptor is available for non-blocking write
|
||||
virtual void OnWriteWaiting(int fd) = 0;
|
||||
// default copy ctor, assignment operator and dtor are ok
|
||||
|
||||
// called when there is exception on descriptor
|
||||
virtual void OnExceptionWaiting(int fd) = 0;
|
||||
|
||||
// virtual dtor for the base class
|
||||
virtual ~wxFDIOHandler() { }
|
||||
};
|
||||
// return true if fd appears in any of the sets
|
||||
bool HasFD(int fd) const;
|
||||
|
||||
// those flags describes sets where descriptor should be added
|
||||
enum wxSelectDispatcherEntryFlags
|
||||
{
|
||||
wxSelectInput = 1,
|
||||
wxSelectOutput = 2,
|
||||
wxSelectException = 4,
|
||||
wxSelectAll = wxSelectInput | wxSelectOutput | wxSelectException
|
||||
};
|
||||
// add or remove FD to our sets depending on whether flags contains
|
||||
// wxFDIO_INPUT/OUTPUT/EXCEPTION bits
|
||||
bool SetFD(int fd, int flags);
|
||||
|
||||
WX_DECLARE_HASH_MAP(
|
||||
int,
|
||||
wxFDIOHandler*,
|
||||
wxIntegerHash,
|
||||
wxIntegerEqual,
|
||||
wxFDIOHandlerMap
|
||||
);
|
||||
|
||||
class WXDLLIMPEXP_CORE wxSelectDispatcher
|
||||
{
|
||||
public:
|
||||
// returns instance of the table
|
||||
static wxSelectDispatcher& Get();
|
||||
|
||||
virtual ~wxSelectDispatcher()
|
||||
// same as SetFD() except it unsets the bits set in the flags for the given
|
||||
// fd
|
||||
bool ClearFD(int fd, int flags)
|
||||
{
|
||||
return SetFD(fd, wxFDIO_ALL & ~flags);
|
||||
}
|
||||
|
||||
// register descriptor in sets.
|
||||
void RegisterFD(int fd, wxFDIOHandler* handler, int flags = wxSelectAll);
|
||||
|
||||
// unregister descriptor from sets and return handler for cleanup
|
||||
wxFDIOHandler* UnregisterFD(int fd, int flags = wxSelectAll);
|
||||
// call select() with our sets: the other parameters are the same as for
|
||||
// select() itself
|
||||
int Select(int nfds, struct timeval *tv);
|
||||
|
||||
// return handler for descriptor or null if fd is not registered
|
||||
wxFDIOHandler* FindHandler(int fd);
|
||||
|
||||
// calls select on registered descriptors and
|
||||
void RunLoop(int timeout = wxSELECT_TIMEOUT_INFINITE);
|
||||
|
||||
protected:
|
||||
wxSelectDispatcher() { m_maxFD = -1; }
|
||||
// call the handler methods corresponding to the sets having this fd
|
||||
void Handle(int fd, wxFDIOHandler& handler) const;
|
||||
|
||||
private:
|
||||
void ProcessSets(fd_set* readset, fd_set* writeset, fd_set* exeptset, int max_fd);
|
||||
typedef void (wxFDIOHandler::*Callback)();
|
||||
|
||||
fd_set m_readset;
|
||||
fd_set m_writeset;
|
||||
fd_set m_exeptset;
|
||||
// the FD sets indices
|
||||
enum
|
||||
{
|
||||
Read,
|
||||
Write,
|
||||
Except,
|
||||
Max
|
||||
};
|
||||
|
||||
// the sets used with select()
|
||||
fd_set m_fds[Max];
|
||||
|
||||
// the wxFDIO_XXX flags, functions and names (used for debug messages only)
|
||||
// corresponding to the FD sets above
|
||||
static int ms_flags[Max];
|
||||
static const char *ms_names[Max];
|
||||
static Callback ms_handlers[Max];
|
||||
};
|
||||
|
||||
class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxFDIODispatcher
|
||||
{
|
||||
public:
|
||||
// returns the unique instance of this class, the pointer shouldn't be
|
||||
// deleted and is normally never NULL
|
||||
static wxSelectDispatcher *Get();
|
||||
|
||||
// if we have any registered handlers, check for any pending events to them
|
||||
// and dispatch them -- this is used from wxX11 and wxDFB event loops
|
||||
// implementation
|
||||
static void DispatchPending();
|
||||
|
||||
// implement pure virtual methods of the base class
|
||||
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
|
||||
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
|
||||
virtual wxFDIOHandler *UnregisterFD(int fd, int flags = wxFDIO_ALL);
|
||||
virtual void RunLoop(int timeout = TIMEOUT_INFINITE);
|
||||
|
||||
protected:
|
||||
wxSelectDispatcher();
|
||||
|
||||
private:
|
||||
// common part of RegisterFD() and ModifyFD()
|
||||
bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags);
|
||||
|
||||
// call the handlers for the fds present in the given sets
|
||||
void ProcessSets(const wxSelectSets& sets);
|
||||
|
||||
// helper of ProcessSets(): call the handler if its fd is in the set
|
||||
void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler,
|
||||
const char *name);
|
||||
|
||||
|
||||
// the select sets containing all the registered fds
|
||||
wxSelectSets m_sets;
|
||||
|
||||
// the highest registered fd value or -1 if none
|
||||
int m_maxFD;
|
||||
wxFDIOHandlerMap m_handlers;
|
||||
|
||||
static wxSelectDispatcher *ms_instance;
|
||||
|
||||
friend class wxSelectDispatcherModule;
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user