sources/network/udpsocket.cpp

Tue, 26 May 2015 18:46:24 +0300

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Tue, 26 May 2015 18:46:24 +0300
changeset 83
08bfc3d9d2ae
parent 81
a18aaf460648
child 88
08ccaf26cffd
permissions
-rw-r--r--

Removed format.cpp and format.h, use built-in formatting instead

/*
	Copyright 2014, 2015 Teemu Piippo
	All rights reserved.

	Redistribution and use in source and binary forms, with or without
	modification, are permitted provided that the following conditions
	are met:

	1. Redistributions of source code must retain the above copyright
	   notice, this list of conditions and the following disclaimer.
	2. Redistributions in binary form must reproduce the above copyright
	   notice, this list of conditions and the following disclaimer in the
	   documentation and/or other materials provided with the distribution.
	3. Neither the name of the copyright holder nor the names of its
	   contributors may be used to endorse or promote products derived from
	   this software without specific prior written permission.

	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
	TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
	PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
	OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
	PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
	PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
	LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
	NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
	SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "udpsocket.h"

#ifndef _WIN32
# include <sys/socket.h>
# include <netinet/in.h>
#else
# include <winsock2.h>
# include <ws2tcpip.h>
#endif

#include <sys/types.h>
#include <sys/time.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include "../huffman/huffman.h"

static char g_huffmanBuffer[131072];

// -----------------------------------------------------------------------------
//
UDPSocket::UDPSocket() :
	m_socket (socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) {}

// -----------------------------------------------------------------------------
//
UDPSocket::~UDPSocket()
{
	close (m_socket);
}

// -------------------------------------------------------------------------------------------------
//
bool UDPSocket::set_blocking (bool a)
{
#ifndef _WIN32
	int flags = fcntl (m_socket, F_GETFL, 0);
	int newflags = a ? (flags & ~O_NONBLOCK) : (flags | O_NONBLOCK);

	if (flags < 0 || fcntl (m_socket, F_SETFL, newflags) != 0)
	{
		m_error = "Unable to set socket as non-blocking";
		return false;
	}

	return true;
#else
	unsigned long mode = a ? 0 : 1;

	if (ioctlsocket (m_socket, FIONBIO, &mode) != 0)
	{
		m_error = strerror (errno);
		return false;
	}

	return true;
#endif
}

// -------------------------------------------------------------------------------------------------
//
METHOD
UDPSocket::bind (unsigned short port) -> bool
{
	sockaddr_in svaddr;
	memset (&svaddr, 0, sizeof svaddr);
	svaddr.sin_family = AF_INET;
	svaddr.sin_port = htons (port);
	svaddr.sin_addr.s_addr = htonl (INADDR_ANY);

	if (::bind (m_socket, reinterpret_cast<sockaddr*> (&svaddr), sizeof svaddr) == -1)
	{
		m_error = String ("Couldn't bind to port ") + String::from_number (port);
		return false;
	}

	return true;
}

// -------------------------------------------------------------------------------------------------
//
bool UDPSocket::read (Datagram& datagram)
{
	sockaddr_in claddr;
	socklen_t socklen = sizeof claddr;
	int length = ::recvfrom (m_socket, g_huffmanBuffer, sizeof g_huffmanBuffer, 0,
		reinterpret_cast<sockaddr*> (&claddr), &socklen);

	if (length == -1)
	{
		if (errno != EWOULDBLOCK)
			m_error = String ("recvfrom error: ") + strerror (errno);

		return false;
	}

	unsigned char decodedPacket[MAX_DATAGRAM_LENGTH];
	int decodedLength = sizeof decodedPacket;
	HUFFMAN_Decode (reinterpret_cast<unsigned char*> (g_huffmanBuffer),
		decodedPacket, length, &decodedLength);
	datagram.from.host = ntohl (claddr.sin_addr.s_addr);
	datagram.from.port = ntohs (claddr.sin_port);
	datagram.data = Bytestream (decodedPacket, decodedLength);
	return true;
}

// -------------------------------------------------------------------------------------------------
//
METHOD
UDPSocket::send (const IPAddress& address, const Bytestream& data) -> bool
{
	int encodedlength = sizeof g_huffmanBuffer;
	HUFFMAN_Encode (data.data(), reinterpret_cast<unsigned char*> (g_huffmanBuffer),
		data.written_length(), &encodedlength);
	sockaddr_in claddr = address.to_sockaddr_in();
	int res = ::sendto (m_socket, g_huffmanBuffer, encodedlength, 0,
		reinterpret_cast<sockaddr*> (&claddr), sizeof claddr);

	if (res == -1)
	{
		m_error = String ("Unable to launch packet: ") + strerror (errno);
		return false;
	}

	return true;
}

mercurial