+wxSocketBase& wxSocketBase::Write(const void *buffer, wxUint32 nbytes)
+{
+ wxSocketWriteGuard write(this);
+
+ m_lcount = DoWrite(buffer, nbytes);
+
+ return *this;
+}
+
+// This function is a mirror image of DoRead() except that it doesn't use the
+// push back buffer and doesn't treat 0 return value specially (normally this
+// shouldn't happen at all here), so please see comments there for explanations
+wxUint32 wxSocketBase::DoWrite(const void *buffer_, wxUint32 nbytes)
+{
+ wxCHECK_MSG( m_impl, 0, "socket must be valid" );
+
+ const char *buffer = static_cast<const char *>(buffer_);
+ wxCHECK_MSG( buffer, 0, "NULL buffer" );
+
+ wxUint32 total = 0;
+ while ( nbytes )
+ {
+ if ( m_impl->m_stream && !m_connected )
+ {
+ if ( (m_flags & wxSOCKET_WAITALL) || !total )
+ SetError(wxSOCKET_IOERR);
+ break;
+ }
+
+ const int ret = m_impl->Write(buffer, nbytes);
+ if ( ret == -1 )
+ {
+ if ( m_impl->GetLastError() == wxSOCKET_WOULDBLOCK )
+ {
+ if ( m_flags & wxSOCKET_NOWAIT )
+ break;
+
+ if ( !DoWaitWithTimeout(wxSOCKET_OUTPUT_FLAG) )
+ {
+ SetError(wxSOCKET_TIMEDOUT);
+ break;
+ }
+
+ continue;
+ }
+ else // "real" error
+ {
+ SetError(wxSOCKET_IOERR);
+ break;
+ }
+ }
+
+ total += ret;
+
+ if ( !(m_flags & wxSOCKET_WAITALL) )
+ break;
+
+ nbytes -= ret;
+ buffer += ret;
+ }
+
+ return total;
+}
+
+wxSocketBase& wxSocketBase::WriteMsg(const void *buffer, wxUint32 nbytes)
+{
+ struct
+ {
+ unsigned char sig[4];
+ unsigned char len[4];
+ } msg;
+
+ wxSocketWriteGuard write(this);
+
+ wxSocketWaitModeChanger changeFlags(this, wxSOCKET_WAITALL);
+
+ msg.sig[0] = (unsigned char) 0xad;
+ msg.sig[1] = (unsigned char) 0xde;
+ msg.sig[2] = (unsigned char) 0xed;
+ msg.sig[3] = (unsigned char) 0xfe;
+
+ msg.len[0] = (unsigned char) (nbytes & 0xff);
+ msg.len[1] = (unsigned char) ((nbytes >> 8) & 0xff);
+ msg.len[2] = (unsigned char) ((nbytes >> 16) & 0xff);
+ msg.len[3] = (unsigned char) ((nbytes >> 24) & 0xff);
+
+ bool ok = false;
+ if ( DoWrite(&msg, sizeof(msg)) == sizeof(msg) )
+ {
+ m_lcount = DoWrite(buffer, nbytes);
+ if ( m_lcount == nbytes )
+ {
+ msg.sig[0] = (unsigned char) 0xed;
+ msg.sig[1] = (unsigned char) 0xfe;
+ msg.sig[2] = (unsigned char) 0xad;
+ msg.sig[3] = (unsigned char) 0xde;
+ msg.len[0] =
+ msg.len[1] =
+ msg.len[2] =
+ msg.len[3] = (char) 0;
+
+ if ( DoWrite(&msg, sizeof(msg)) == sizeof(msg))
+ ok = true;
+ }
+ }
+
+ if ( !ok )
+ SetError(wxSOCKET_IOERR);
+
+ return *this;
+}