Ejemplo n.º 1
0
	LWPR::SOCKET_RET_TYPE_E Socket::ReadSocket(SOCKET_FD_T fd, char* buf, int length, int timeout)
	{
		int nDoneLen = 0, nRetNum = 0;
		SOCKET_RET_TYPE_E nRetResult = SOCKET_RET_FAILED;

		if(0 == length) return SOCKET_RET_OK;
		if(length < 0) return SOCKET_RET_FAILED;

		do
		{
			nRetResult = IsSocketReadable(fd, timeout);
			if(nRetResult == SOCKET_RET_OK)
			{
				nRetNum = read(fd, buf + nDoneLen, length - nDoneLen);
				if(nRetNum == -1)
				{
					return ((errno == EINTR) ? SOCKET_RET_TIMEOUT : SOCKET_RET_FAILED);
				}
				else if(nRetNum == 0)
				{
					return SOCKET_RET_FAILED;
				}

				nDoneLen += nRetNum;
			}
		}
		while(nRetResult == SOCKET_RET_OK && (nDoneLen < length));

		return nRetResult;
	}
Ejemplo n.º 2
0
	void Socket::ClearSocket(SOCKET_FD_T fd)
	{
		const int BUF_LEN = 512;
		int nRetNum = 0;
		Buffer buf(BUF_LEN);

		while(IsSocketReadable(fd, 0) == SOCKET_RET_OK && nRetNum >= 0)
		{
			nRetNum = read(fd, buf.Inout(), BUF_LEN);
		}
	}
Ejemplo n.º 3
0
	void Socket::ClearUDPSocket(SOCKET_FD_T fd)
	{
		const int BUF_LEN = 128;
		int nRetNum = 0;
		Buffer buf(BUF_LEN);

		while(IsSocketReadable(fd, 0) == SOCKET_RET_OK && nRetNum >= 0)
		{
			nRetNum = recvfrom(fd, buf.Inout(), BUF_LEN, 0, NULL, NULL);
		}
	}
Ejemplo n.º 4
0
SOCKET CTcpServerSocketHelper::Accept(DWORD dwTimeOut, DWORD &dwRemoteIp, WORD &wRemotePort)
{
	sockaddr_in clientAddr;
	int nSize = sizeof(clientAddr);

	BOOL bError = FALSE;
	if (IsSocketReadable(dwTimeOut, bError, m_Socket))
	{
		SOCKET AcceptSocket = ::accept( m_Socket, (struct sockaddr* )&clientAddr, &nSize );

		if (AcceptSocket !=INVALID_SOCKET)
		{
			dwRemoteIp = clientAddr.sin_addr.s_addr;
			wRemotePort = clientAddr.sin_port;
			return AcceptSocket; 

		}
	}

	return NULL;
}
Ejemplo n.º 5
0
	LWPR::SOCKET_RET_TYPE_E Socket::IsSocketReadable(SOCKET_FD_T fd)
	{
		return IsSocketReadable(fd, INT_MAX);
	}