Mercurial > code
view C++/modules/Socket/SocketUdp.cpp @ 335:486767e1d165
Socket: fix under Windows and add new style
author | David Demelier <markand@malikania.fr> |
---|---|
date | Wed, 11 Mar 2015 09:41:48 +0100 |
parents | 0b576ee64d45 |
children | 06b0f405c58f |
line wrap: on
line source
/* * SocketUdp.cpp -- portable C++ socket wrappers * * Copyright (c) 2013, 2014 David Demelier <markand@malikania.fr> * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #include "SocketAddress.h" #include "SocketUdp.h" SocketUdp::SocketUdp(int domain, int protocol) : Socket(domain, SOCK_DGRAM, protocol) { } unsigned SocketUdp::recvfrom(void *data, unsigned length, SocketAddress &info) { int nbread; // Store information sockaddr_storage address; socklen_t addrlen; addrlen = sizeof (struct sockaddr_storage); nbread = ::recvfrom(m_handle, (Socket::Arg)data, length, 0, (sockaddr *)&address, &addrlen); info = SocketAddress(address, addrlen); if (nbread == Error) { #if defined(_WIN32) int error = WSAGetLastError(); if (error == WSAEWOULDBLOCK) { throw SocketError(SocketError::WouldBlockRead, "recvfrom", error); } throw SocketError(SocketError::System, "recvfrom", error); #else if (errno == EAGAIN || errno == EWOULDBLOCK) { throw SocketError(SocketError::WouldBlockRead, "recvfrom"); } throw SocketError(SocketError::System, "recvfrom"); #endif } return (unsigned)nbread; } unsigned SocketUdp::sendto(const void *data, unsigned length, const SocketAddress &info) { int nbsent; nbsent = ::sendto(m_handle, (Socket::ConstArg)data, length, 0, (const sockaddr *)&info.address(), info.length()); if (nbsent == Error) { #if defined(_WIN32) int error = WSAGetLastError(); if (error == WSAEWOULDBLOCK) { throw SocketError(SocketError::WouldBlockWrite, "sendto", error); } throw SocketError(SocketError::System, "sendto", error); #else if (errno == EAGAIN || errno == EWOULDBLOCK) { throw SocketError(SocketError::WouldBlockWrite, "sendto"); } throw SocketError(SocketError::System, "sendto"); #endif } return (unsigned)nbsent; }