Ejemplo n.º 1
0
//------------------------------------------------------------------------------
//
// BindInterface()
//
//------------------------------------------------------------------------------
bool TDSocket::BindInterface(u8 *pInterface)
{
	bool           bRetVal = false;
	struct in_addr stInterfaceAddr;

	if (GetMulticast() == true)
	{
		if (pInterface)
		{
			stInterfaceAddr.s_addr = inet_addr((const char *)pInterface);
			if (SETSOCKOPT(m_socket, IPPROTO_IP, IP_MULTICAST_IF, &stInterfaceAddr, sizeof(stInterfaceAddr)) == SocketSuccess)
			{
				bRetVal = true;
			}
		}
	}
	else
	{
		SetSocketError(TDSocket::SocketProtocolError);
	}

	return bRetVal;
}
Ejemplo n.º 2
0
//------------------------------------------------------------------------------
//
// Receive() - Attempts to receive a block of data on an established		
//			   connection.	Data is received in an internal buffer managed	
//			   by the class.  This buffer is only valid until the next call	
//			   to Receive(), a call to Close(), or until the object goes out
//			   of scope.													
//																			
//------------------------------------------------------------------------------
i32 TDSocket::Receive(i32 nMaxBytes)
{
	m_nBytesReceived = 0;

	//--------------------------------------------------------------------------
	// If the socket is invalid then return false.
	//--------------------------------------------------------------------------
	if (IsSocketValid() == false)
	{
		return m_nBytesReceived;
	}

	//--------------------------------------------------------------------------
	// Free existing buffer and allocate a new buffer the size of
	// nMaxBytes.
	//--------------------------------------------------------------------------
	if (nMaxBytes != m_nBufferSize)
	{
		m_nBufferSize = nMaxBytes;
		m_pBuffer.clear();
		m_pBuffer.resize(m_nBufferSize);
	}

	SetSocketError(SocketSuccess);

	switch (m_nSocketType)
	{
		//----------------------------------------------------------------------
		// If zero bytes are received, then return.  If SocketERROR is 
		// received, free buffer and return CSocket::SocketError (-1) to caller.	
		//----------------------------------------------------------------------
	case TDSocket::SocketTypeTcp:
	{
		do
		{
			m_nBytesReceived = RECV(m_socket, &m_pBuffer[m_nBytesReceived],
				nMaxBytes - m_nBytesReceived, m_nFlags);
			TranslateSocketError();
		} while (IsSocketValid() && (GetSocketError() == TDSocket::SocketInterrupted));

		break;
	}
	case TDSocket::SocketTypeUdp:
	{
		socklen_t srcSize;

		srcSize = sizeof(struct sockaddr_in);

		if (GetMulticast() == true)
		{
			do
			{
				m_nBytesReceived = RECVFROM(m_socket, &m_pBuffer[0], nMaxBytes, 0,
					&m_stMulticastGroup, &srcSize);
				TranslateSocketError();
			} while (IsSocketValid() && GetSocketError() == TDSocket::SocketInterrupted);
		}
		else
		{
			do
			{
				m_nBytesReceived = RECVFROM(m_socket, &m_pBuffer[0], nMaxBytes, 0,
					&m_stClientSockaddr, &srcSize);
				TranslateSocketError();
			} while (GetSocketError() == TDSocket::SocketInterrupted);
		}

		break;
	}
	default:
		break;
	}

	TranslateSocketError();
	return m_nBytesReceived;
}
Ejemplo n.º 3
0
//------------------------------------------------------------------------------
//
// Receive() - Attempts to receive a block of data on an established		
//			   connection.	Data is received in an internal buffer managed	
//			   by the class.  This buffer is only valid until the next call	
//			   to Receive(), a call to Close(), or until the object goes out
//			   of scope.													
//																			
//------------------------------------------------------------------------------
int32 CSimpleSocket::Receive(int32 nMaxBytes)
{
    m_nBytesReceived = 0;

    //--------------------------------------------------------------------------
    // If the socket is invalid then return false.
    //--------------------------------------------------------------------------
    if (IsSocketValid() == false)
    {
        return m_nBytesReceived;
    }

    //--------------------------------------------------------------------------
    // Free existing buffer and allocate a new buffer the size of
    // nMaxBytes.
    //--------------------------------------------------------------------------
    if ((m_pBuffer != NULL) && (nMaxBytes != m_nBufferSize))
    {
        delete [] m_pBuffer;
        m_pBuffer = NULL;
    }

    //--------------------------------------------------------------------------
    // Allocate a new internal buffer to receive data.
    //--------------------------------------------------------------------------
	if (m_pBuffer == NULL)
	{
		m_nBufferSize = nMaxBytes;
		m_pBuffer = new uint8[nMaxBytes]; 
	}

    SetSocketError(SocketSuccess);

    switch (m_nSocketType)
    {
        m_timer.Initialize();
        m_timer.SetStartTime();

        //----------------------------------------------------------------------
        // If zero bytes are received, then return.  If SocketERROR is 
        // received, free buffer and return CSocket::SocketError (-1) to caller.	
        //----------------------------------------------------------------------
        case CSimpleSocket::SocketTypeTcp:
        {
            do 
            {
                m_nBytesReceived = RECV(m_socket, (m_pBuffer + m_nBytesReceived), 
                                        nMaxBytes, m_nFlags);
                TranslateSocketError();
            } while ((GetSocketError() == CSimpleSocket::SocketInterrupted));

            break;
        }
        case CSimpleSocket::SocketTypeUdp:
        {
            uint32 srcSize;
                
            srcSize = sizeof(struct sockaddr_in);

            if (GetMulticast() == true)
            {
                do 
                {
                    m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0, 
                                                &m_stMulticastGroup, &srcSize);
                    TranslateSocketError();
                } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
            }
            else
            {
                do 
                {
                    m_nBytesReceived = RECVFROM(m_socket, m_pBuffer, nMaxBytes, 0, 
                                                &m_stClientSockaddr, &srcSize);
                    TranslateSocketError();
                } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
            }

            break;
        }
        default:
            break;
    }
    
    m_timer.SetEndTime();
    TranslateSocketError();

    //--------------------------------------------------------------------------
    // If we encounter an error translate the error code and return.  One 
    // possible error code could be EAGAIN (EWOULDBLOCK) if the socket is
    // non-blocking.  This does not mean there is an error, but no data is
    // yet available on the socket.
    //--------------------------------------------------------------------------
    if (m_nBytesReceived == CSimpleSocket::SocketError)
    {
        if (m_pBuffer != NULL)
        {
            delete [] m_pBuffer;
            m_pBuffer = NULL;
        }
    }

    return m_nBytesReceived;
}