sources/network/udpsocket.cpp

Thu, 28 Jan 2021 11:39:30 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 28 Jan 2021 11:39:30 +0200
changeset 198
54d64e5a4204
parent 191
2e6cbacafdc7
child 195
be953e1621d9
permissions
-rw-r--r--

reduce delta with protocol5

/*
	Copyright 2014 - 2021 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>
# include <sys/time.h>
# include <unistd.h>
#else
# include <winsock2.h>
# include <ws2tcpip.h>
#endif

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

BEGIN_ZFC_NAMESPACE

static char HuffmanBuffer[131072];

net::UDPSocket::UDPSocket() :
	file_descriptor{::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)} {}

net::UDPSocket::~UDPSocket()
{
#ifdef _WIN32
	::closesocket(m_socket);
#else
	::close(this->file_descriptor);
#endif
}

bool net::UDPSocket::set_blocking(bool a, std::ostream& errors)
{
#ifndef _WIN32
	int flags = ::fcntl(this->file_descriptor, F_GETFL, 0);
	int newflags = a ?(flags & ~O_NONBLOCK) :(flags | O_NONBLOCK);
	if (flags < 0 || ::fcntl(this->file_descriptor, F_SETFL, newflags) != 0)
	{
		errors << "Unable to set the UDP socket as non-blocking";
		return false;
	}
	else
	{
		return true;
	}
#else
	unsigned long mode = a ? 0 : 1;

	if (::ioctlsocket(m_socket, FIONBIO, &mode) != 0)
	{
		errors << strerror(errno);
		return false;
	}
	else
	{
		return true;
	}
#endif
}

bool net::UDPSocket::bind(const net::port_t port, std::ostream& errors)
{
	sockaddr_in svaddr;
	std::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(this->file_descriptor, reinterpret_cast<sockaddr*>(&svaddr), sizeof svaddr) == -1)
	{
		errors << "Couldn't bind to port "s + std::to_string(port);
		return false;
	}
	else
	{
		return true;
	}
}

bool net::UDPSocket::read(Datagram& datagram, std::ostream& errors)
{
	sockaddr_in claddr;
	socklen_t socklen = sizeof claddr;
	const int length = ::recvfrom(
		this->file_descriptor,
		zfc::HuffmanBuffer,
		sizeof zfc::HuffmanBuffer,
		0,
		reinterpret_cast<sockaddr*>(&claddr),
		&socklen
	);
	if (length == -1)
	{
		if (errno != EWOULDBLOCK)
		{
			errors << std::string("recvfrom error: ") + std::strerror(errno);
		}
		return false;
	}
	unsigned char decodedPacket[MAX_DATAGRAM_LENGTH];
	int decodedLength = sizeof decodedPacket;
	::HUFFMAN_Decode(reinterpret_cast<unsigned char*>(HuffmanBuffer),
		decodedPacket, length, &decodedLength);
	datagram.address.host = ntohl(claddr.sin_addr.s_addr);
	datagram.address.port = ntohs(claddr.sin_port);
	datagram.message = std::vector<unsigned char>{&decodedPacket[0], &decodedPacket[decodedLength]};
	return true;
}

bool net::UDPSocket::send(const net::ip_address& address, const std::vector<unsigned char>& data, std::ostream& errors)
{
	int encodedlength = sizeof HuffmanBuffer;
	::HUFFMAN_Encode(data.data(), reinterpret_cast<unsigned char*>(HuffmanBuffer), data.size(), &encodedlength);
	sockaddr_in claddr = net::ip_address_to_sockaddr_in(address);
	const int send_result = ::sendto(
		this->file_descriptor,
		HuffmanBuffer,
		encodedlength,
		0,
		reinterpret_cast<sockaddr*>(&claddr),
		sizeof claddr
	);
	if (send_result == -1)
	{
		errors << "Unable to launch packet: "s + std::strerror(errno);
		return false;
	}
	else
	{
		return true;
	}
}

END_ZFC_NAMESPACE

mercurial