Exemplo n.º 1
0
	int UDPSocketImpl::read(void *data, int size, SocketName &endpoint)
	{
		sockaddr_in addr;
		int addr_len = sizeof(sockaddr_in);

		int result = recvfrom(handle, static_cast<char*>(data), size, 0, (sockaddr *)&addr, &addr_len);
		if (result == SOCKET_ERROR)
		{
			int last_error = WSAGetLastError();
			// To do: maybe return different return values for sending errors
			if (last_error == WSAEWOULDBLOCK || last_error == WSAEMSGSIZE || last_error == WSAECONNRESET || last_error == WSAENETRESET)
			{
				endpoint = SocketName();
				return -1;
			}
			else
			{
				throw Exception("Error reading from udp socket");
			}
		}

		endpoint = SocketName();
		endpoint.from_sockaddr(AF_INET, (sockaddr *)&addr, addr_len);
		return result;
	}
Exemplo n.º 2
0
int Win32Socket::peek_from(void *data, int size, SocketName &out_socketname)
{
	sockaddr_in new_addr;
	memset(&new_addr, 0, sizeof(sockaddr_in));
	int addr_size = sizeof(sockaddr_in);
	int result = ::recvfrom(handle, (char *) data, size, MSG_PEEK, (sockaddr *) &new_addr, &addr_size);
	throw_if_failed(result);
	out_socketname.from_sockaddr(AF_INET, (sockaddr *) &new_addr, size);
	reset_receive();
	return result;
}
Exemplo n.º 3
0
SOCKET Win32Socket::accept(SocketName &out_socketname)
{
	sockaddr_in new_addr;
	memset(&new_addr, 0, sizeof(sockaddr_in));
	int size = sizeof(sockaddr_in);
	SOCKET accepted_socket = ::accept(handle, (sockaddr *) &new_addr, &size);
	throw_if_invalid(accepted_socket);
	out_socketname.from_sockaddr(AF_INET, (sockaddr *) &new_addr, size);
	reset_receive();
	return accepted_socket;
}
Exemplo n.º 4
0
SocketName Win32Socket::get_remote_name() const
{
	sockaddr_in addr;
	memset(&addr, 0, sizeof(sockaddr_in));
	int size = sizeof(sockaddr_in);
	int result = getpeername(handle, (sockaddr *) &addr, &size);
	throw_if_failed(result);

	SocketName name;
	name.from_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	return name;
}
Exemplo n.º 5
0
	SocketName TCPConnectionImpl::remote_name()
	{
		sockaddr_in addr;
		memset(&addr, 0, sizeof(sockaddr_in));
		int size = sizeof(sockaddr_in);
		int result = getpeername(handle, (sockaddr *)&addr, &size);
		if (result == SOCKET_ERROR)
			throw Exception("Error retrieving remote socket name");

		SocketName name;
		name.from_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		return name;
	}
Exemplo n.º 6
0
	SocketName TCPConnectionImpl::local_name()
	{
		sockaddr_in addr;
		memset(&addr, 0, sizeof(sockaddr_in));
		socklen_t size = sizeof(sockaddr_in);
		int result = getsockname(handle, (sockaddr *)&addr, &size);
		if (result == -1)
			throw Exception("Error retrieving local socket name");

		SocketName name;
		name.from_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		return name;
	}
Exemplo n.º 7
0
	TCPConnection TCPListen::accept(SocketName &out_end_point)
	{
		sockaddr_in peer_address = { 0 };
		socklen_t peer_address_length = sizeof(sockaddr_in);

		int result = ::accept(impl->handle, reinterpret_cast<sockaddr*>(&peer_address), &peer_address_length);
		if (result == -1)
		{
			if (errno == EWOULDBLOCK)
				return TCPConnection();
			else
				throw Exception("Error accepting from socket");
		}

		out_end_point.from_sockaddr(AF_INET, reinterpret_cast<sockaddr*>(&peer_address), peer_address_length);
		return TCPConnection(std::make_shared<TCPSocket>(result));
	}
Exemplo n.º 8
0
	TCPConnection TCPListen::accept(SocketName &out_end_point)
	{
		sockaddr_in peer_address = { 0 };
		int peer_address_length = sizeof(sockaddr_in);

		SOCKET result = ::accept(impl->handle, reinterpret_cast<sockaddr*>(&peer_address), &peer_address_length);
		if (result == INVALID_SOCKET)
		{
			if (WSAGetLastError() == WSAEWOULDBLOCK)
				return TCPConnection();
			else
				throw Exception("Error accepting from socket");
		}

		out_end_point = SocketName();
		out_end_point.from_sockaddr(AF_INET, reinterpret_cast<sockaddr*>(&peer_address), peer_address_length);
		return TCPConnection(std::make_shared<TCPSocket>(result));
	}
Exemplo n.º 9
0
	int UDPSocketImpl::read(void *data, int size, SocketName &endpoint)
	{
		sockaddr_in addr;
		socklen_t addr_len = sizeof(sockaddr_in);

		int result = recvfrom(handle, static_cast<char*>(data), size, 0, (sockaddr *)&addr, &addr_len);
		if (result == -1)
		{
			if (errno == EWOULDBLOCK || errno == EMSGSIZE || errno == ECONNRESET || errno == ENETRESET)
			{
				endpoint = SocketName();
				return -1;
			}
			else
			{
				throw Exception("Error reading from udp socket");
			}
		}

		endpoint.from_sockaddr(AF_INET, (sockaddr *)&addr, addr_len);
		return result;
	}