Пример #1
0
DTerr DeviceNetworkSockets::udp_send (const NetworkPacket &packet, const NetworkSocket &socket)
{
	if (socket.is_empty())
		return DT3_ERR_NONE;

	DTint socket_raw = *reinterpret_cast<DTint*>(socket.network_socket_data());
	
	struct sockaddr_storage sast = string_to_address(packet.network_address().network_address());
    struct sockaddr *sa = (struct sockaddr *) &sast;

#if DT3_OS == DT3_ANDROID
    ::sendto(   socket_raw, 
                packet.data(),
                packet.data_size(),
                0, 
                sa,
                sizeof(sockaddr));
#else
    ::sendto(   socket_raw, 
                packet.data(),
                (DTuint) packet.data_size(),
                0, 
                sa,
                sa->sa_len);
                
#endif

	return DT3_ERR_NONE;
}
Пример #2
0
DTerr DeviceNetworkSockets::tcp_recv (NetworkPacket &packet, const NetworkSocket &socket)
{
	if (socket.is_empty())
		return DT3_ERR_NONE;

	DTint socket_raw = *reinterpret_cast<DTint*>(socket.network_socket_data());

    ssize_t size = ::recv(  socket_raw,
                            packet.data(),
                            (DTuint) packet.data_size(),
                            0
                        );
                            

    if (size < 0) {
        packet.set_data_size(0);

        switch (errno) {
            //case EAGAIN:  // Same as EWOULDBLOCK
			case EWOULDBLOCK:	
                return DT3_ERR_NET_WOULD_BLOCK;
			default:			
                LOG_MESSAGE << "TCPRecv: recv: " << strerror(errno) << " (" << (DTuint) errno << ")";
                return DT3_ERR_NET_UNKNOWN;
		};
	} else {
        packet.set_data_size(size);
    }

	return DT3_ERR_NONE;
}
Пример #3
0
DTerr DeviceNetworkSockets::udp_recv (NetworkPacket &packet, const NetworkSocket &socket)
{    
	if (socket.is_empty())
		return DT3_ERR_NONE;

	DTint socket_raw = *reinterpret_cast<DTint*>(socket.network_socket_data());

    struct sockaddr from;
    socklen_t       from_size = sizeof(from);

    DTsize size = ::recvfrom(   socket_raw,
                                packet.data(),
                                (DTuint) packet.data_size(),
                                0, 
                                &from,
                                &from_size
                            );
                            
    if (size > 0) {
        packet.set_network_address( address_to_string(&from) );
        packet.set_data_size(size);
    
	} else {	
		switch (errno) {
			case EWOULDBLOCK:	return DT3_ERR_NET_WOULD_BLOCK;
			default:			return DT3_ERR_NET_UNKNOWN;
		};
	}

	return DT3_ERR_NONE;

}
Пример #4
0
DTerr DeviceNetworkSockets::tcp_send (const NetworkPacket &packet, const NetworkSocket &socket)
{
	if (socket.is_empty())
		return DT3_ERR_NONE;

	DTint socket_raw = *reinterpret_cast<DTint*>(socket.network_socket_data());
	
    ::send(   socket_raw, 
                packet.data(),
                (DTuint) packet.data_size(),
                0
            );

	return DT3_ERR_NONE;
}