diff -r 060a13878ca0 -r be953e1621d9 sources/network/udpsocket.cpp --- a/sources/network/udpsocket.cpp Wed Jan 27 12:41:50 2021 +0200 +++ b/sources/network/udpsocket.cpp Wed Jan 27 19:48:41 2021 +0200 @@ -1,5 +1,5 @@ /* - Copyright 2014 - 2016 Teemu Piippo + Copyright 2014 - 2021 Teemu Piippo All rights reserved. Redistribution and use in source and binary forms, with or without @@ -47,115 +47,119 @@ BEGIN_ZFC_NAMESPACE -char UDPSocket::HuffmanBuffer[131072]; +static char HuffmanBuffer[131072]; -// ----------------------------------------------------------------------------- -// -UDPSocket::UDPSocket() : - m_socket (socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) {} +net::UDPSocket::UDPSocket() : + file_descriptor{::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)} {} -// ----------------------------------------------------------------------------- -// -UDPSocket::~UDPSocket() +net::UDPSocket::~UDPSocket() { #ifdef _WIN32 - closesocket (m_socket); + ::closesocket(m_socket); #else - close (m_socket); + ::close(this->file_descriptor); #endif } -// ------------------------------------------------------------------------------------------------- -// -bool UDPSocket::set_blocking (bool a) +bool net::UDPSocket::set_blocking(bool a, std::ostream& errors) { #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) + 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) { - m_error = "Unable to set socket as non-blocking"; + errors << "Unable to set the UDP socket as non-blocking"; return false; } - - return true; + else + { + return true; + } #else unsigned long mode = a ? 0 : 1; - if (ioctlsocket (m_socket, FIONBIO, &mode) != 0) + if (::ioctlsocket(m_socket, FIONBIO, &mode) != 0) { - m_error = strerror (errno); + errors << strerror(errno); return false; } - - return true; + else + { + return true; + } #endif } -// ------------------------------------------------------------------------------------------------- -// -bool UDPSocket::bind (unsigned short port) +bool net::UDPSocket::bind(const net::port_t port, std::ostream& errors) { sockaddr_in svaddr; - memset (&svaddr, 0, sizeof 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 (m_socket, reinterpret_cast (&svaddr), sizeof svaddr) == -1) + svaddr.sin_port = htons(port); + svaddr.sin_addr.s_addr = htonl(INADDR_ANY); + if (::bind(this->file_descriptor, reinterpret_cast(&svaddr), sizeof svaddr) == -1) { - m_error = String ("Couldn't bind to port ") + String::fromNumber (port); + errors << "Couldn't bind to port "s + std::to_string(port); return false; } - - return true; + else + { + return true; + } } -// ------------------------------------------------------------------------------------------------- -// -bool UDPSocket::read (Datagram& datagram) +bool net::UDPSocket::read(Datagram& datagram, std::ostream& errors) { sockaddr_in claddr; socklen_t socklen = sizeof claddr; - int length = ::recvfrom (m_socket, HuffmanBuffer, sizeof HuffmanBuffer, 0, - reinterpret_cast (&claddr), &socklen); - + const int length = ::recvfrom( + this->file_descriptor, + zfc::HuffmanBuffer, + sizeof zfc::HuffmanBuffer, + 0, + reinterpret_cast(&claddr), + &socklen + ); if (length == -1) { if (errno != EWOULDBLOCK) - m_error = String ("recvfrom error: ") + strerror (errno); - + { + errors << std::string("recvfrom error: ") + std::strerror(errno); + } return false; } - unsigned char decodedPacket[MAX_DATAGRAM_LENGTH]; int decodedLength = sizeof decodedPacket; - HUFFMAN_Decode (reinterpret_cast (HuffmanBuffer), + ::HUFFMAN_Decode(reinterpret_cast(HuffmanBuffer), decodedPacket, length, &decodedLength); - datagram.address.host = ntohl (claddr.sin_addr.s_addr); - datagram.address.port = ntohs (claddr.sin_port); - datagram.message = ByteArray(decodedPacket, decodedLength); + datagram.address.host = ntohl(claddr.sin_addr.s_addr); + datagram.address.port = ntohs(claddr.sin_port); + datagram.message = std::vector{&decodedPacket[0], &decodedPacket[decodedLength]}; return true; } -// ------------------------------------------------------------------------------------------------- -// -bool UDPSocket::send (const IPAddress& address, const ByteArray& data) +bool net::UDPSocket::send(const net::ip_address& address, const std::vector& data, std::ostream& errors) { int encodedlength = sizeof HuffmanBuffer; - HUFFMAN_Encode (data.data(), reinterpret_cast (HuffmanBuffer), data.size(), &encodedlength); - sockaddr_in claddr = address.to_sockaddr_in(); - int res = ::sendto (m_socket, HuffmanBuffer, encodedlength, 0, - reinterpret_cast (&claddr), sizeof claddr); - - if (res == -1) + ::HUFFMAN_Encode(data.data(), reinterpret_cast(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(&claddr), + sizeof claddr + ); + if (send_result == -1) { - m_error = String ("Unable to launch packet: ") + strerror (errno); + errors << "Unable to launch packet: "s + std::strerror(errno); return false; } - - return true; + else + { + return true; + } } END_ZFC_NAMESPACE \ No newline at end of file