+// All IO operations {Read, Write, ReadMsg, WriteMsg, Peek,
+// Unread, Discard} update m_error and m_lcount.
+//
+// TODO: Should Connect, Accept and AcceptWith update m_error?
+
+class _wxSocketInternalTimer: public wxTimer
+{
+public:
+ int *m_state;
+ unsigned long m_new_val;
+
+ void Notify()
+ {
+ *m_state = m_new_val; // Change the value
+ }
+};
+
+wxUint32 wxSocketBase::DeferRead(char *buffer, wxUint32 nbytes)
+{
+ // Timer for timeout
+ _wxSocketInternalTimer timer;
+
+ //wxLogMessage("Entrando a DeferRead, nbytes = %d", nbytes);
+ wxASSERT(m_defering == NO_DEFER);
+
+ // Set the defering mode to READ.
+ m_defering = DEFER_READ;
+
+ // Set the current buffer.
+ m_defer_buffer = buffer;
+ m_defer_nbytes = nbytes;
+ m_defer_timer = &timer;
+
+ timer.m_state = (int *)&m_defer_buffer;
+ timer.m_new_val = 0;
+
+ timer.Start(m_timeout * 1000, FALSE);
+
+ // If the socket is readable, call DoDefer for the first time
+ if (GSocket_Select(m_socket, GSOCK_INPUT_FLAG))
+ {
+ //wxLogMessage("Llamando al primer DoDefer");
+ DoDefer();
+ }
+
+ // Wait for buffer completion.
+ while (m_defer_buffer != NULL)
+ PROCESS_EVENTS();
+
+ timer.Stop();
+
+ // Disable defering mode.
+ m_defering = NO_DEFER;
+ m_defer_timer = NULL;
+
+ // Return the number of bytes read from the socket.
+ //wxLogMessage("Saliendo de DeferRead: total: %d bytes", nbytes - m_defer_nbytes);
+ return nbytes-m_defer_nbytes;
+}
+
+wxSocketBase& wxSocketBase::Read(char* buffer, wxUint32 nbytes)
+{
+ // Mask read events
+ m_reading = TRUE;
+
+ m_lcount = _Read(buffer, nbytes);
+
+ // If in WAITALL mode, all bytes should have been read.
+ if (m_flags & WAITALL)
+ m_error = (m_lcount != nbytes);
+ else
+ m_error = (m_lcount == 0);
+
+ // Trigger another read event if there is still data available.
+ m_reading = FALSE;
+ // TODO: TriggerRead
+
+ return *this;
+}
+
+wxUint32 wxSocketBase::_Read(char* buffer, wxUint32 nbytes)
+{
+ int total;
+ int ret = 1;
+
+ // we try this even if the connection has already been closed.
+ total = GetPushback(buffer, nbytes, FALSE);
+ nbytes -= total;
+ buffer += total;
+
+ // If the socket is not connected, or we have got the whole
+ // needed buffer, return immedately
+ if (!m_connected || !m_socket || !nbytes)
+ return total;
+
+ // Possible combinations (they are checked in this order)
+ // NOWAIT
+ // SPEED | WAITALL
+ // SPEED
+ // WAITALL
+ // NONE
+ //
+ if (m_flags & NOWAIT) // NOWAIT
+ {
+ GSocket_SetNonBlocking(m_socket, TRUE);
+ ret = GSocket_Read(m_socket, buffer, nbytes);
+ GSocket_SetNonBlocking(m_socket, FALSE);
+
+ if (ret > 0)
+ total += ret;
+ }
+ else if ((m_flags & SPEED) && (m_flags & WAITALL)) // SPEED, WAITALL
+ {
+ while (ret > 0 && nbytes > 0)
+ {
+ ret = GSocket_Read(m_socket, buffer, nbytes);
+ total += ret;
+ buffer += ret;
+ nbytes -= ret;
+ }
+ // In case the last call was an error ...
+ if (ret < 0)
+ total++;
+ }
+ else if (m_flags & SPEED) // SPEED, !WAITALL
+ {
+ ret = GSocket_Read(m_socket, buffer, nbytes);
+
+ if (ret > 0)
+ total += ret;
+ }
+ else // NONE or WAITALL
+ {
+ ret = DeferRead(buffer, nbytes);
+
+ if (ret > 0)
+ total += ret;
+ }
+
+ return total;
+}
+
+wxSocketBase& wxSocketBase::ReadMsg(char* buffer, wxUint32 nbytes)