分类:
2008-11-12 14:03:37
int CSocket::Read(void* buffer, unsigned int size, int& error)
{
int res = recv(m_fd, (char*)buffer, size, 0);
if (res == -1)
{
#ifdef __WXMSW__
error = ConvertMSWErrorCode(WSAGetLastError());
#else
error = errno;
#endif
if (error == EAGAIN)
{
if (m_pSocketThread)
{
m_pSocketThread->m_sync.Lock();
if (!(m_pSocketThread->m_waiting & WAIT_READ))
{
m_pSocketThread->m_waiting |= WAIT_READ;
m_pSocketThread->WakeupThread(true);
}
m_pSocketThread->m_sync.Unlock();
}
}
}
else
error = 0;
return res;
}
int CSocket::Peek(void* buffer, unsigned int size, int& error)
{
int res = recv(m_fd, (char*)buffer, size, MSG_PEEK);
if (res == -1)
{
#ifdef __WXMSW__
error = ConvertMSWErrorCode(WSAGetLastError());
#else
error = errno;
#endif
}
else
error = 0;
return res;
}
int CSocket::Write(const void* buffer, unsigned int size, int& error)
{
#ifdef MSG_NOSIGNAL
const int flags = MSG_NOSIGNAL;
#else
const int flags = 0;
#endif
int res = send(m_fd, (const char*)buffer, size, flags);
if (res == -1)
{
#ifdef __WXMSW__
error = ConvertMSWErrorCode(WSAGetLastError());
#else
error = errno;
#endif
if (error == EAGAIN)
{
if (m_pSocketThread)
{
m_pSocketThread->m_sync.Lock();
if (!(m_pSocketThread->m_waiting & WAIT_WRITE))
{
m_pSocketThread->m_waiting |= WAIT_WRITE;
m_pSocketThread->WakeupThread(true);
}
m_pSocketThread->m_sync.Unlock();
}
}
}
else
error = 0;
return res;
}