selectdispatcher.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/private/selectdispatcher.h
  3. // Purpose: wxSelectDispatcher class
  4. // Authors: Lukasz Michalski and Vadim Zeitlin
  5. // Created: December 2006
  6. // Copyright: (c) Lukasz Michalski
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef _WX_PRIVATE_SELECTDISPATCHER_H_
  10. #define _WX_PRIVATE_SELECTDISPATCHER_H_
  11. #include "wx/defs.h"
  12. #if wxUSE_SELECT_DISPATCHER
  13. #if defined(HAVE_SYS_SELECT_H) || defined(__WATCOMC__)
  14. #include <sys/time.h>
  15. #include <sys/select.h>
  16. #endif
  17. #ifdef __WATCOMC__
  18. #include <types.h>
  19. #include <sys/ioctl.h>
  20. #include <tcpustd.h>
  21. #else
  22. #include <sys/types.h>
  23. #endif
  24. #include "wx/private/fdiodispatcher.h"
  25. // helper class storing all the select() fd sets
  26. class WXDLLIMPEXP_BASE wxSelectSets
  27. {
  28. public:
  29. // ctor zeroes out all fd_sets
  30. wxSelectSets();
  31. // default copy ctor, assignment operator and dtor are ok
  32. // return true if fd appears in any of the sets
  33. bool HasFD(int fd) const;
  34. // add or remove FD to our sets depending on whether flags contains
  35. // wxFDIO_INPUT/OUTPUT/EXCEPTION bits
  36. bool SetFD(int fd, int flags);
  37. // same as SetFD() except it unsets the bits set in the flags for the given
  38. // fd
  39. bool ClearFD(int fd)
  40. {
  41. return SetFD(fd, 0);
  42. }
  43. // call select() with our sets: the other parameters are the same as for
  44. // select() itself
  45. int Select(int nfds, struct timeval *tv);
  46. // call the handler methods corresponding to the sets having this fd if it
  47. // is present in any set and return true if it is
  48. bool Handle(int fd, wxFDIOHandler& handler) const;
  49. private:
  50. typedef void (wxFDIOHandler::*Callback)();
  51. // the FD sets indices
  52. enum
  53. {
  54. Read,
  55. Write,
  56. Except,
  57. Max
  58. };
  59. // the sets used with select()
  60. fd_set m_fds[Max];
  61. // the wxFDIO_XXX flags, functions and names (used for debug messages only)
  62. // corresponding to the FD sets above
  63. static int ms_flags[Max];
  64. static const char *ms_names[Max];
  65. static Callback ms_handlers[Max];
  66. };
  67. class WXDLLIMPEXP_BASE wxSelectDispatcher : public wxMappedFDIODispatcher
  68. {
  69. public:
  70. // default ctor
  71. wxSelectDispatcher() { m_maxFD = -1; }
  72. // implement pure virtual methods of the base class
  73. virtual bool RegisterFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
  74. virtual bool ModifyFD(int fd, wxFDIOHandler *handler, int flags = wxFDIO_ALL);
  75. virtual bool UnregisterFD(int fd);
  76. virtual bool HasPending() const;
  77. virtual int Dispatch(int timeout = TIMEOUT_INFINITE);
  78. private:
  79. // common part of RegisterFD() and ModifyFD()
  80. bool DoUpdateFDAndHandler(int fd, wxFDIOHandler *handler, int flags);
  81. // call the handlers for the fds present in the given sets, return the
  82. // number of handlers we called
  83. int ProcessSets(const wxSelectSets& sets);
  84. // helper of ProcessSets(): call the handler if its fd is in the set
  85. void DoProcessFD(int fd, const fd_set& fds, wxFDIOHandler *handler,
  86. const char *name);
  87. // common part of HasPending() and Dispatch(): calls select() with the
  88. // specified timeout
  89. int DoSelect(wxSelectSets& sets, int timeout) const;
  90. // the select sets containing all the registered fds
  91. wxSelectSets m_sets;
  92. // the highest registered fd value or -1 if none
  93. int m_maxFD;
  94. };
  95. #endif // wxUSE_SELECT_DISPATCHER
  96. #endif // _WX_PRIVATE_SOCKETEVTDISPATCH_H_