view C++/modules/Socket/SocketTcp.cpp @ 380:06b0f405c58f

Socket: initial support for object-oriented addresses
author David Demelier <markand@malikania.fr>
date Fri, 19 Jun 2015 13:56:12 +0200
parents 92457ea8f7e2
children
line wrap: on
line source

/*
 * SocketTcp.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 "SocketTcp.h"

void SocketTcp::listen(int max)
{
	if (::listen(m_handle, max) == Error) {
		throw SocketError(SocketError::System, "listen");
	}
}

std::unique_ptr<SocketTcp> SocketTcp::accept()
{
	std::unique_ptr<SocketAddress> dummy;

	return accept(dummy);
}

std::unique_ptr<SocketTcp> SocketTcp::accept(std::unique_ptr<SocketAddress> &info)
{
	Socket::Handle handle;

	// Store the information
	sockaddr_storage address;
	socklen_t addrlen;

	addrlen = sizeof (sockaddr_storage);
	handle = ::accept(m_handle, reinterpret_cast<sockaddr *>(&address), &addrlen);

	if (handle == Invalid) {
#if defined(_WIN32)
		int error = WSAGetLastError();

		if (error == WSAEWOULDBLOCK) {
			throw SocketError(SocketError::WouldBlockRead, "accept", error);
		}

		throw SocketError(SocketError::System, "accept", error);
#else
		if (errno == EAGAIN || errno == EWOULDBLOCK) {
			throw SocketError(SocketError::WouldBlockRead, "accept");
		}

		throw SocketError(SocketError::System, "accept");
#endif
	}

	info = SocketAddress::decode(address, addrlen);

	return std::make_unique<SocketTcp>(handle);
}

void SocketTcp::connect(const std::unique_ptr<SocketAddress> &address)
{
	if (m_state == SocketState::Connected) {
		return;
	}

	auto &sa = address->address();
	auto addrlen = address->length();

	if (::connect(m_handle, reinterpret_cast<const sockaddr *>(&sa), addrlen) == Error) {
		/*
		 * Determine if the error comes from a non-blocking connect that cannot be
		 * accomplished yet.
		 */
#if defined(_WIN32)
		int error = WSAGetLastError();

		if (error == WSAEWOULDBLOCK) {
			throw SocketError(SocketError::WouldBlockWrite, "connect", error);
		}

		throw SocketError(SocketError::System, "connect", error);
#else
		if (errno == EINPROGRESS) {
			throw SocketError(SocketError::WouldBlockWrite, "connect");
		}

		throw SocketError(SocketError::System, "connect");
#endif
	}

	m_state = SocketState::Connected;
}

unsigned SocketTcp::recv(void *data, unsigned dataLen)
{
	int nbread;

	nbread = ::recv(m_handle, (Socket::Arg)data, dataLen, 0);
	if (nbread == Error) {
#if defined(_WIN32)
		int error = WSAGetLastError();

		if (error == WSAEWOULDBLOCK) {
			throw SocketError(SocketError::WouldBlockRead, "recv", error);
		}

		throw SocketError(SocketError::System, "recv", error);
#else
		if (errno == EAGAIN || errno == EWOULDBLOCK) {
			throw SocketError(SocketError::WouldBlockRead, "recv");
		}

		throw SocketError(SocketError::System, "recv");
#endif
	} else if (nbread == 0) {
		m_state = SocketState::Closed;
	}

	return (unsigned)nbread;
}

unsigned SocketTcp::send(const void *data, unsigned length)
{
	int nbsent;

	nbsent = ::send(m_handle, (Socket::ConstArg)data, length, 0);
	if (nbsent == Error) {
#if defined(_WIN32)
		int error = WSAGetLastError();

		if (error == WSAEWOULDBLOCK) {
			throw SocketError(SocketError::WouldBlockWrite, "send", error);
		}

		throw SocketError(SocketError::System, "send", error);
#else
		if (errno == EAGAIN || errno == EWOULDBLOCK) {
			throw SocketError(SocketError::WouldBlockWrite, "send");
		}

		throw SocketError(SocketError::System, "send");
#endif
	}

	return (unsigned)nbsent;
}