Example #1
0
	void UDPSocketImpl::send(const void *data, int size, const SocketName &endpoint)
	{
		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));

		sendto(handle, static_cast<const char*>(data), size, 0, (const sockaddr *)&addr, sizeof(sockaddr_in));
	}
Example #2
0
	TCPConnectionImpl::TCPConnectionImpl(const SocketName &endpoint)
	{
		int receive_buffer_size = 600 * 1024;
		int send_buffer_size = 600 * 1024;
		//int result = setsockopt(handle, SOL_SOCKET, SO_RCVBUF, (const char *) &receive_buffer_size, sizeof(int));
		int result = setsockopt(handle, SOL_SOCKET, SO_SNDBUF, (const char *)&send_buffer_size, sizeof(int));

		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		result = ::connect(handle, (const sockaddr *)&addr, sizeof(sockaddr_in));
		if (result == SOCKET_ERROR)
			throw Exception("Connect to server failed");

		int value = 1;
		result = setsockopt(handle, IPPROTO_TCP, TCP_NODELAY, (const char *)&value, sizeof(int));

		result = WSAEventSelect(handle, wait_handle, FD_READ | FD_WRITE | FD_CLOSE);
		if (result == SOCKET_ERROR)
			throw Exception("WSAEventSelect failed");

		//int receive_buffer_size = 0;
		//int send_buffer_size = 0;
		//int len = sizeof(int);
		//getsockopt(handle, SOL_SOCKET, SO_RCVBUF, (char *) &receive_buffer_size, &len);
		//getsockopt(handle, SOL_SOCKET, SO_SNDBUF, (char *) &send_buffer_size, &len);
	}
Example #3
0
	TCPConnectionImpl::TCPConnectionImpl(const SocketName &endpoint)
	{
		//int receive_buffer_size = 600*1024;
		int send_buffer_size = 600*1024;
		//int result = setsockopt(handle, SOL_SOCKET, SO_RCVBUF, (const char *) &receive_buffer_size, sizeof(int));
		int result = setsockopt(handle, SOL_SOCKET, SO_SNDBUF, (const char *) &send_buffer_size, sizeof(int));

		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
		result = ::connect(handle, (const sockaddr *) &addr, sizeof(sockaddr_in));
		if (result == -1)
			throw Exception("Connect to server failed");

		int value = 1;
		result = setsockopt(handle, IPPROTO_TCP, TCP_NODELAY, (const char *) &value, sizeof(int));

		//int receive_buffer_size = 0;
		//int send_buffer_size = 0;
		//int len = sizeof(int);
		//getsockopt(handle, SOL_SOCKET, SO_RCVBUF, (char *) &receive_buffer_size, &len);
		//getsockopt(handle, SOL_SOCKET, SO_SNDBUF, (char *) &send_buffer_size, &len);

		int nonblocking = 1;
		ioctl(handle, FIONBIO, &nonblocking);
	}
Example #4
0
	void UDPSocketImpl::bind(const SocketName &endpoint)
	{
		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		int result = ::bind(handle, (const sockaddr *)&addr, sizeof(sockaddr_in));
		if (result == SOCKET_ERROR)
			throw Exception("Could not bind socket to end point");
	}
Example #5
0
void Win32Socket::connect(const SocketName &socketname)
{
	sockaddr_in addr;
	socketname.to_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	int result = ::connect(handle, (const sockaddr *) &addr, sizeof(sockaddr_in));
	if (result == SOCKET_ERROR)
	{
		int errorcode = WSAGetLastError();
		if ( ! ( (errorcode ==  WSAEWOULDBLOCK) || (errorcode ==  WSAEINPROGRESS) ) )
			throw Exception(error_to_string(errorcode));
	}
}
Example #6
0
void Win32Socket::bind(const SocketName &socketname, bool reuse_address)
{
	if (reuse_address)
	{
		int value = 1;
		int result = setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, (const char *) &value, sizeof(int));
		throw_if_failed(result);
	}

	sockaddr_in addr;
	socketname.to_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	int result = ::bind(handle, (const sockaddr *) &addr, sizeof(sockaddr_in));
	throw_if_failed(result);
}
Example #7
0
	void UDPSocketImpl::send(const void *data, int size, const SocketName &endpoint)
	{
		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));

		int result = sendto(handle, static_cast<const char*>(data), size, 0, (const sockaddr *)&addr, sizeof(sockaddr_in));
		if (result == SOCKET_ERROR)
		{
			int last_error = WSAGetLastError();
			if (last_error != WSAENOBUFS)
			{
				throw Exception("Error writing to udp socket");
			}
		}
	}
Example #8
0
	TCPListen::TCPListen(const SocketName &endpoint, int backlog, bool reuse_address)
		: impl(new TCPSocket())
	{
		if (reuse_address)
		{
			int value = 1;
			int result = setsockopt(impl->handle, SOL_SOCKET, SO_REUSEADDR, (const char *)&value, sizeof(int));
			if (result == SOCKET_ERROR)
				throw Exception("Could not set reuse address socket option");
		}

		int receive_buffer_size = 600 * 1024;
		int send_buffer_size = 600 * 1024;

		//int result = setsockopt(impl->handle, SOL_SOCKET, SO_RCVBUF, (const char *) &receive_buffer_size, sizeof(int));
		//if (result == SOCKET_ERROR)
		//	throw Exception("Could not set socket receive buffer size");

		int result = setsockopt(impl->handle, SOL_SOCKET, SO_SNDBUF, (const char *)&send_buffer_size, sizeof(int));
		if (result == SOCKET_ERROR)
			throw Exception("Could not set socket send buffer size");

		int value = 1;
		result = setsockopt(impl->handle, IPPROTO_TCP, TCP_NODELAY, (const char *)&value, sizeof(int));

		sockaddr_in addr;
		endpoint.to_sockaddr(AF_INET, (sockaddr *)&addr, sizeof(sockaddr_in));
		result = ::bind(impl->handle, (const sockaddr *)&addr, sizeof(sockaddr_in));
		if (result == SOCKET_ERROR)
			throw Exception("Could not bind socket to end point");

		result = ::listen(impl->handle, backlog);
		if (result == SOCKET_ERROR)
			throw Exception("Unable to set socket into listen state");

		result = WSAEventSelect(impl->handle, impl->wait_handle, FD_ACCEPT);
		if (result == SOCKET_ERROR)
			throw Exception("WSAEventSelect failed");
	}
Example #9
0
int Win32Socket::send_to(const void *data, int size, const SocketName &socketname)
{
	sockaddr_in addr;
	socketname.to_sockaddr(AF_INET, (sockaddr *) &addr, sizeof(sockaddr_in));
	int result = ::sendto(handle, (const char *) data, size, 0, (const sockaddr *) &addr, sizeof(sockaddr_in));
	if (result == SOCKET_ERROR)
	{
		int errorcode = WSAGetLastError();
		if (errorcode == WSAEWOULDBLOCK)
		{
			reset_send();
			return 0;
		}
		else
		{
			throw Exception(error_to_string(errorcode));
		}
	}
	else
	{
		return result;
	}
}