2007-03-16 15:45:12 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/private/selectdispatcher.h
|
|
|
|
// Purpose: wxSelectDispatcher class
|
2007-05-14 23:43:39 +00:00
|
|
|
// Authors: Lukasz Michalski and Vadim Zeitlin
|
2007-03-16 15:45:12 +00:00
|
|
|
// Created: December 2006
|
|
|
|
// Copyright: (c) Lukasz Michalski
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef _WX_PRIVATE_SELECTDISPATCHER_H_
|
|
|
|
#define _WX_PRIVATE_SELECTDISPATCHER_H_
|
|
|
|
|
|
|
|
#include "wx/defs.h"
|
|
|
|
|
2007-07-14 19:06:18 +00:00
|
|
|
#if wxUSE_SELECT_DISPATCHER
|
|
|
|
|
2014-05-16 13:01:06 +00:00
|
|
|
#if defined(HAVE_SYS_SELECT_H)
|
2010-03-20 12:45:28 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-16 13:01:06 +00:00
|
|
|
#include <sys/types.h>
|
2007-05-18 21:19:37 +00:00
|
|
|
|
2016-02-21 17:47:46 +00:00
|
|
|
#include "wx/thread.h"
|
2007-05-14 23:43:39 +00:00
|
|
|
#include "wx/private/fdiodispatcher.h"
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// helper class storing all the select() fd sets
|
|
|
|
class WXDLLIMPEXP_BASE wxSelectSets
|
2007-03-16 15:45:12 +00:00
|
|
|
{
|
|
|
|
public:
|
2007-05-14 23:43:39 +00:00
|
|
|
// ctor zeroes out all fd_sets
|
|
|
|
wxSelectSets();
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// default copy ctor, assignment operator and dtor are ok
|
2007-03-16 16:17:04 +00:00
|
|
|
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// return true if fd appears in any of the sets
|
|
|
|
bool HasFD(int fd) const;
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// add or remove FD to our sets depending on whether flags contains
|
|
|
|
// wxFDIO_INPUT/OUTPUT/EXCEPTION bits
|
|
|
|
bool SetFD(int fd, int flags);
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// same as SetFD() except it unsets the bits set in the flags for the given
|
|
|
|
// fd
|
2007-07-14 19:41:46 +00:00
|
|
|
bool ClearFD(int fd)
|
2007-03-16 15:45:12 +00:00
|
|
|
{
|
2007-07-14 19:41:46 +00:00
|
|
|
return SetFD(fd, 0);
|
2007-03-16 15:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// call select() with our sets: the other parameters are the same as for
|
|
|
|
// select() itself
|
|
|
|
int Select(int nfds, struct timeval *tv);
|
|
|
|
|
2009-01-03 01:21:24 +00:00
|
|
|
// call the handler methods corresponding to the sets having this fd if it
|
|
|
|
// is present in any set and return true if it is
|
|
|
|
bool Handle(int fd, wxFDIOHandler& handler) const;
|
2007-05-14 23:43:39 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef void (wxFDIOHandler::*Callback)();
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
};
|
|
|
|
|
2007-06-02 13:51:05 +00:00
|
|
|
class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher
|
2007-05-14 23:43:39 +00:00
|
|
|
{
|
|
|
|
public:
|
2008-03-22 04:19:12 +00:00
|
|
|
// default ctor
|
|
|
|
wxSelectDispatcher() { m_maxFD = -1; }
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// implement pure virtual methods of the base class
|
2014-03-30 00:02:23 +00:00
|
|
|
virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL) wxOVERRIDE;
|
|
|
|
virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL) wxOVERRIDE;
|
|
|
|
virtual bool UnregisterFD(int fd) wxOVERRIDE;
|
|
|
|
virtual bool HasPending() const wxOVERRIDE;
|
|
|
|
virtual int Dispatch(int timeout = TIMEOUT_INFINITE) wxOVERRIDE;
|
2007-03-16 15:45:12 +00:00
|
|
|
|
|
|
|
private:
|
2007-05-14 23:43:39 +00:00
|
|
|
// common part of RegisterFD() and ModifyFD()
|
|
|
|
bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags);
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2009-01-03 01:21:24 +00:00
|
|
|
// call the handlers for the fds present in the given sets, return the
|
|
|
|
// number of handlers we called
|
|
|
|
int ProcessSets(const wxSelectSets& sets);
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// 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);
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2009-01-03 01:21:24 +00:00
|
|
|
// common part of HasPending() and Dispatch(): calls select() with the
|
|
|
|
// specified timeout
|
|
|
|
int DoSelect(wxSelectSets& sets, int timeout) const;
|
|
|
|
|
2007-03-16 15:45:12 +00:00
|
|
|
|
2016-02-21 17:47:46 +00:00
|
|
|
#if wxUSE_THREADS
|
|
|
|
wxCriticalSection m_cs;
|
|
|
|
#endif // wxUSE_THREADS
|
|
|
|
|
2007-05-14 23:43:39 +00:00
|
|
|
// the select sets containing all the registered fds
|
|
|
|
wxSelectSets m_sets;
|
|
|
|
|
|
|
|
// the highest registered fd value or -1 if none
|
|
|
|
int m_maxFD;
|
2007-03-16 15:45:12 +00:00
|
|
|
};
|
|
|
|
|
2007-07-14 19:06:18 +00:00
|
|
|
#endif // wxUSE_SELECT_DISPATCHER
|
2007-03-16 15:45:12 +00:00
|
|
|
|
|
|
|
#endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_
|