+// wxSocketWaitModeChanger: temporarily change the socket flags affecting its
+// wait mode
+class wxSocketWaitModeChanger
+{
+public:
+ // temporarily set the flags to include the flag value which may be either
+ // wxSOCKET_NOWAIT or wxSOCKET_WAITALL
+ wxSocketWaitModeChanger(wxSocketBase *socket, int flag)
+ : m_socket(socket),
+ m_oldflags(socket->GetFlags())
+
+ {
+ wxASSERT_MSG( flag == wxSOCKET_WAITALL || flag == wxSOCKET_NOWAIT,
+ "not a wait flag" );
+
+ // preserve wxSOCKET_BLOCK value when switching to wxSOCKET_WAITALL
+ // mode but not when switching to wxSOCKET_NOWAIT as the latter is
+ // incompatible with wxSOCKET_BLOCK
+ if ( flag != wxSOCKET_NOWAIT )
+ flag |= m_oldflags & wxSOCKET_BLOCK;
+
+ socket->SetFlags(flag);
+ }
+
+ ~wxSocketWaitModeChanger()
+ {
+ m_socket->SetFlags(m_oldflags);
+ }
+
+private:
+ wxSocketBase * const m_socket;
+ const int m_oldflags;
+
+ DECLARE_NO_COPY_CLASS(wxSocketWaitModeChanger)
+};
+
+// wxSocketRead/WriteGuard are instantiated before starting reading
+// from/writing to the socket
+class wxSocketReadGuard
+{
+public:
+ wxSocketReadGuard(wxSocketBase *socket)
+ : m_socket(socket)
+ {
+ wxASSERT_MSG( !m_socket->m_reading, "read reentrancy?" );
+
+ m_socket->m_reading = true;
+ }
+
+ ~wxSocketReadGuard()
+ {
+ m_socket->m_reading = false;
+
+ m_socket->m_impl->ReenableEvents(wxSOCKET_INPUT_FLAG);
+ }
+
+private:
+ wxSocketBase * const m_socket;
+
+ DECLARE_NO_COPY_CLASS(wxSocketReadGuard)
+};
+
+class wxSocketWriteGuard
+{
+public:
+ wxSocketWriteGuard(wxSocketBase *socket)
+ : m_socket(socket)
+ {
+ wxASSERT_MSG( !m_socket->m_writing, "write reentrancy?" );
+
+ m_socket->m_writing = true;
+
+ m_socket->m_impl->ReenableEvents(wxSOCKET_OUTPUT_FLAG);
+ }
+
+ ~wxSocketWriteGuard()
+ {
+ m_socket->m_writing = false;
+ }
+
+private:
+ wxSocketBase * const m_socket;
+
+ DECLARE_NO_COPY_CLASS(wxSocketWriteGuard)
+};
+