sockunix.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/unix/private/sockunix.h
  3. // Purpose: wxSocketImpl implementation for Unix systems
  4. // Authors: Guilhem Lavaux, Vadim Zeitlin
  5. // Created: April 1997
  6. // Copyright: (c) 1997 Guilhem Lavaux
  7. // (c) 2008 Vadim Zeitlin
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_UNIX_GSOCKUNX_H_
  11. #define _WX_UNIX_GSOCKUNX_H_
  12. #include <unistd.h>
  13. #include <sys/ioctl.h>
  14. // Under older (Open)Solaris versions FIONBIO is declared in this header only.
  15. // In the newer versions it's included by sys/ioctl.h but it's simpler to just
  16. // include it always instead of testing for whether it is or not.
  17. #ifdef __SOLARIS__
  18. #include <sys/filio.h>
  19. #endif
  20. #include "wx/private/fdiomanager.h"
  21. class wxSocketImplUnix : public wxSocketImpl,
  22. public wxFDIOHandler
  23. {
  24. public:
  25. wxSocketImplUnix(wxSocketBase& wxsocket)
  26. : wxSocketImpl(wxsocket)
  27. {
  28. m_fds[0] =
  29. m_fds[1] = -1;
  30. }
  31. virtual wxSocketError GetLastError() const;
  32. virtual void ReenableEvents(wxSocketEventFlags flags)
  33. {
  34. // enable the notifications about input/output being available again in
  35. // case they were disabled by OnRead/WriteWaiting()
  36. //
  37. // notice that we'd like to enable the events here only if there is
  38. // nothing more left on the socket right now as otherwise we're going
  39. // to get a "ready for whatever" notification immediately (well, during
  40. // the next event loop iteration) and disable the event back again
  41. // which is rather inefficient but unfortunately doing it like this
  42. // doesn't work because the existing code (e.g. src/common/sckipc.cpp)
  43. // expects to keep getting notifications about the data available from
  44. // the socket even if it didn't read all the data the last time, so we
  45. // absolutely have to continue generating them
  46. EnableEvents(flags);
  47. }
  48. // wxFDIOHandler methods
  49. virtual void OnReadWaiting();
  50. virtual void OnWriteWaiting();
  51. virtual void OnExceptionWaiting();
  52. virtual bool IsOk() const { return m_fd != INVALID_SOCKET; }
  53. private:
  54. virtual void DoClose()
  55. {
  56. DisableEvents();
  57. close(m_fd);
  58. }
  59. virtual void UnblockAndRegisterWithEventLoop()
  60. {
  61. int trueArg = 1;
  62. ioctl(m_fd, FIONBIO, &trueArg);
  63. EnableEvents();
  64. }
  65. // enable or disable notifications for socket input/output events
  66. void EnableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG)
  67. { DoEnableEvents(flags, true); }
  68. void DisableEvents(int flags = wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG)
  69. { DoEnableEvents(flags, false); }
  70. // really enable or disable socket input/output events
  71. void DoEnableEvents(int flags, bool enable);
  72. protected:
  73. // descriptors for input and output event notification channels associated
  74. // with the socket
  75. int m_fds[2];
  76. private:
  77. // notify the associated wxSocket about a change in socket state and shut
  78. // down the socket if the event is wxSOCKET_LOST
  79. void OnStateChange(wxSocketNotify event);
  80. // check if there is any input available, return 1 if yes, 0 if no or -1 on
  81. // error
  82. int CheckForInput();
  83. // give it access to our m_fds
  84. friend class wxSocketFDBasedManager;
  85. };
  86. // A version of wxSocketManager which uses FDs for socket IO: it is used by
  87. // Unix console applications and some X11-like ports (wxGTK and wxMotif but not
  88. // wxX11 currently) which implement their own port-specific wxFDIOManagers
  89. class wxSocketFDBasedManager : public wxSocketManager
  90. {
  91. public:
  92. wxSocketFDBasedManager()
  93. {
  94. m_fdioManager = NULL;
  95. }
  96. virtual bool OnInit();
  97. virtual void OnExit() { }
  98. virtual wxSocketImpl *CreateSocket(wxSocketBase& wxsocket)
  99. {
  100. return new wxSocketImplUnix(wxsocket);
  101. }
  102. virtual void Install_Callback(wxSocketImpl *socket_, wxSocketNotify event);
  103. virtual void Uninstall_Callback(wxSocketImpl *socket_, wxSocketNotify event);
  104. protected:
  105. // get the FD index corresponding to the given wxSocketNotify
  106. wxFDIOManager::Direction
  107. GetDirForEvent(wxSocketImpl *socket, wxSocketNotify event);
  108. // access the FDs we store
  109. int& FD(wxSocketImplUnix *socket, wxFDIOManager::Direction d)
  110. {
  111. return socket->m_fds[d];
  112. }
  113. wxFDIOManager *m_fdioManager;
  114. wxDECLARE_NO_COPY_CLASS(wxSocketFDBasedManager);
  115. };
  116. #endif /* _WX_UNIX_GSOCKUNX_H_ */