- // 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)
- m_lcount += ret;
- }
- else if (m_flags & SPEED & WAITALL) // SPEED, WAITALL
- {
- while (ret > 0 && nbytes > 0)
- {
- ret = GSocket_Read(m_socket, buffer, nbytes);
- m_lcount += ret;
- buffer += ret;
- nbytes -= ret;
- }
- // In case the last call was an error ...
- if (ret < 0)
- m_lcount ++;
- }
- else if (m_flags & SPEED) // SPEED, !WAITALL
- {
- ret = GSocket_Read(m_socket, buffer, nbytes);
-
- if (ret > 0)
- m_lcount += ret;
- }
- else // NONE or WAITALL
- {
- ret = DeferRead(buffer, nbytes);
-
- if (ret > 0)
- m_lcount += ret;
- }
-
- // If we have read some data, then it is not an error, even
- // when in WAITALL mode, the last low-level IO call might
- // have failed.
- if (!m_lcount)
- m_error = TRUE;
-
- return *this;
-}
-
-wxSocketBase& wxSocketBase::ReadMsg(char* buffer, wxUint32 nbytes)
-{
-#define MAX_BUFSIZE (10 * 1024)
-
- int old_flags;
- unsigned long len, len2, sig;
- struct
- {
- char sig[4];
- char len[4];
- } msg;
-
- // sig should be an explicit 32-bit unsigned integer; I've seen
- // compilers in which wxUint32 was actually a 16-bit unsigned integer
-
- old_flags = m_flags;
- SetFlags(old_flags | WAITALL);
-
- Read((char *)&msg, sizeof(msg));
- if (m_lcount != sizeof(msg))
- {
- SetFlags(old_flags);
- m_error = TRUE;
- return *this;
- }
-
- sig = msg.sig[0] & 0xff;
- sig |= (wxUint32)(msg.sig[1] & 0xff) << 8;
- sig |= (wxUint32)(msg.sig[2] & 0xff) << 16;
- sig |= (wxUint32)(msg.sig[3] & 0xff) << 24;
-
- if (sig != 0xfeeddead)
- {
- wxLogDebug(_T("Warning: invalid signature returned to ReadMsg\n"));
- SetFlags(old_flags);
- m_error = TRUE;
- return *this;
- }
-
- len = msg.len[0] & 0xff;
- len |= (wxUint32)(msg.len[1] & 0xff) << 8;
- len |= (wxUint32)(msg.len[2] & 0xff) << 16;
- len |= (wxUint32)(msg.len[3] & 0xff) << 24;
-
- if (len > nbytes)
- {
- len2 = len - nbytes;
- len = nbytes;
- }
- else
- len2 = 0;
-
- // The "len &&" in the following statements is necessary so
- // that we don't attempt to read (and possibly hang the system)
- // if the message was zero bytes long
- if (len && Read(buffer, len).LastCount() != len)
- {
- SetFlags(old_flags);
- m_error = TRUE;
- return *this;
- }
- if (len2)
- {
- char *discard_buffer = new char[MAX_BUFSIZE];
- long discard_len;