# HG changeset patch # User Teemu Piippo # Date 1418270383 -7200 # Node ID c709bb1a08e4ad1a9fb0cc9c5ce9668f30b9ce27 # Parent 146825d63b9af3498a8126b3884ffcd6ca0a651b - udp -> udpsocket diff -r 146825d63b9a -r c709bb1a08e4 CMakeLists.txt --- a/CMakeLists.txt Thu Dec 11 05:58:55 2014 +0200 +++ b/CMakeLists.txt Thu Dec 11 05:59:43 2014 +0200 @@ -9,7 +9,7 @@ sources/version.cpp sources/network/bytestream.cpp sources/network/ipaddress.cpp - sources/network/udp.cpp + sources/network/udpsocket.cpp ) set (CURSES_NEED_NCURSES, True) diff -r 146825d63b9a -r c709bb1a08e4 sources/network/udp.cpp --- a/sources/network/udp.cpp Thu Dec 11 05:58:55 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "udp.h" - -// ----------------------------------------------------------------------------- -// -UDPSocket::UDPSocket() : - m_socket (socket (AF_INET, SOCK_DGRAM, 0)) {} - -// ----------------------------------------------------------------------------- -// -UDPSocket::~UDPSocket() {} - -// ------------------------------------------------------------------------------------------------- -// -METHOD -UDPSocket::set_blocking (bool a) -> bool -{ - 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; -} - -// ------------------------------------------------------------------------------------------------- -// -METHOD -UDPSocket::bind (unsigned short port) -> bool -{ - struct 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 (&svaddr), sizeof svaddr) == -1) - { - m_error = String ("Couldn't bind to port ") + String::from_number (port); - return false; - } - - return true; -} - -// ------------------------------------------------------------------------------------------------- -// -METHOD -UDPSocket::read (Datagram& datagram) -> bool -{ - sockaddr_in claddr; - socklen_t socklen = sizeof claddr; - static unsigned char packet[MAX_DATAGRAM_LENGTH]; - int length = ::recvfrom (m_socket, packet, sizeof packet, 0, - reinterpret_cast (&claddr), &socklen); - - if (length == -1) - { - // We got an error, though EWOULDBLOCK is silent as it means no packets recieved. - if (errno != EWOULDBLOCK) - m_error.sprintf ("recvfrom error: %s", strerror (errno));; - - return false; - } - - datagram.from.host = ntohl (claddr.sin_addr.s_addr); - datagram.from.port = ntohs (claddr.sin_port); - datagram.data = Bytestream (packet, length); - return true; -} - -// ------------------------------------------------------------------------------------------------- -// -METHOD -UDPSocket::send (const Bytestream& data, const IPAddress& addr) -> bool -{ - struct sockaddr_in claddr = addr.to_sockaddr_in(); - - int res = ::sendto (m_socket, data.data(), data.written_length(), 0, - reinterpret_cast (&claddr), sizeof claddr); - - if (res == -1) - { - m_error = String ("Unable to launch packet: ") + strerror (errno); - return false; - } - - return true; -} diff -r 146825d63b9a -r c709bb1a08e4 sources/network/udp.h --- a/sources/network/udp.h Thu Dec 11 05:58:55 2014 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,31 +0,0 @@ -#pragma once -#include "../main.h" -#include "ipaddress.h" -#include "bytestream.h" - -enum { MAX_DATAGRAM_LENGTH = 5120 }; - -struct Datagram -{ - Bytestream data; - IPAddress from; -}; - -// ------------------------------------------------------------------------------------------------- -// -class UDPSocket -{ -public: - UDPSocket(); - virtual ~UDPSocket(); - - METHOD bind (unsigned short port) -> bool; - METHOD read (Datagram& datagram) -> bool; - METHOD send (const Bytestream& data, const IPAddress& addr) -> bool; - METHOD set_blocking (bool a) -> bool; - -private: - IPAddress m_addr; - String m_error; - int m_socket; -}; diff -r 146825d63b9a -r c709bb1a08e4 sources/network/udpsocket.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sources/network/udpsocket.cpp Thu Dec 11 05:59:43 2014 +0200 @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include "udpsocket.h" + +// ----------------------------------------------------------------------------- +// +UDPSocket::UDPSocket() : + m_socket (socket (AF_INET, SOCK_DGRAM, 0)) {} + +// ----------------------------------------------------------------------------- +// +UDPSocket::~UDPSocket() {} + +// ------------------------------------------------------------------------------------------------- +// +METHOD +UDPSocket::set_blocking (bool a) -> bool +{ + 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; +} + +// ------------------------------------------------------------------------------------------------- +// +METHOD +UDPSocket::bind (unsigned short port) -> bool +{ + struct 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 (&svaddr), sizeof svaddr) == -1) + { + m_error = String ("Couldn't bind to port ") + String::from_number (port); + return false; + } + + return true; +} + +// ------------------------------------------------------------------------------------------------- +// +METHOD +UDPSocket::read (Datagram& datagram) -> bool +{ + sockaddr_in claddr; + socklen_t socklen = sizeof claddr; + static unsigned char packet[MAX_DATAGRAM_LENGTH]; + int length = ::recvfrom (m_socket, packet, sizeof packet, 0, + reinterpret_cast (&claddr), &socklen); + + if (length == -1) + { + // We got an error, though EWOULDBLOCK is silent as it means no packets recieved. + if (errno != EWOULDBLOCK) + m_error.sprintf ("recvfrom error: %s", strerror (errno));; + + return false; + } + + datagram.from.host = ntohl (claddr.sin_addr.s_addr); + datagram.from.port = ntohs (claddr.sin_port); + datagram.data = Bytestream (packet, length); + return true; +} + +// ------------------------------------------------------------------------------------------------- +// +METHOD +UDPSocket::send (const Bytestream& data, const IPAddress& addr) -> bool +{ + struct sockaddr_in claddr = addr.to_sockaddr_in(); + + int res = ::sendto (m_socket, data.data(), data.written_length(), 0, + reinterpret_cast (&claddr), sizeof claddr); + + if (res == -1) + { + m_error = String ("Unable to launch packet: ") + strerror (errno); + return false; + } + + return true; +} diff -r 146825d63b9a -r c709bb1a08e4 sources/network/udpsocket.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sources/network/udpsocket.h Thu Dec 11 05:59:43 2014 +0200 @@ -0,0 +1,31 @@ +#pragma once +#include "../main.h" +#include "ipaddress.h" +#include "bytestream.h" + +enum { MAX_DATAGRAM_LENGTH = 5120 }; + +struct Datagram +{ + Bytestream data; + IPAddress from; +}; + +// ------------------------------------------------------------------------------------------------- +// +class UDPSocket +{ +public: + UDPSocket(); + virtual ~UDPSocket(); + + METHOD bind (unsigned short port) -> bool; + METHOD read (Datagram& datagram) -> bool; + METHOD send (const Bytestream& data, const IPAddress& addr) -> bool; + METHOD set_blocking (bool a) -> bool; + +private: + IPAddress m_addr; + String m_error; + int m_socket; +};