Chinaunix首页 | 论坛 | 博客
  • 博客访问: 993161
  • 博文数量: 200
  • 博客积分: 5011
  • 博客等级: 大校
  • 技术积分: 2479
  • 用 户 组: 普通用户
  • 注册时间: 2008-06-27 15:07
文章分类

全部博文(200)

文章存档

2009年(12)

2008年(190)

我的朋友

分类:

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;
}
阅读(914) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~