示例#1
0
Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer, int p_buffer_size) {

	ERR_FAIL_COND_V(!peer_addr.is_valid(), ERR_UNCONFIGURED);

	if (sock_type == IP::TYPE_NONE)
		sock_type = peer_addr.is_ipv4() ? IP::TYPE_IPV4 : IP::TYPE_IPV6;

	int sock = _get_socket();
	ERR_FAIL_COND_V(sock == -1, FAILED);
	struct sockaddr_storage addr;
	size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, sock_type);

	_set_blocking(true);

	errno = 0;
	int err;
	while ((err = sendto(sock, (const char *)p_buffer, p_buffer_size, 0, (struct sockaddr *)&addr, addr_size)) != p_buffer_size) {

		if (WSAGetLastError() != WSAEWOULDBLOCK) {
			return FAILED;
		};
	}

	return OK;
}
Error PacketPeerUDPWinsock::put_packet(const uint8_t *p_buffer,int p_buffer_size){

	int sock = _get_socket();
	ERR_FAIL_COND_V( sock == -1, FAILED );
	struct sockaddr_storage addr;
	size_t addr_size = _set_sockaddr(&addr, peer_addr, peer_port, ip_type);

	_set_blocking(true);

	errno = 0;
	int err;
	while ( (err = sendto(sock, (const char*)p_buffer, p_buffer_size, 0, (struct sockaddr*)&addr, addr_size)) != p_buffer_size) {

		if (WSAGetLastError() != WSAEWOULDBLOCK) {
			return FAILED;
		};
	}

	return OK;
}
示例#3
0
Error PacketPeerUDPWinsock::_poll(bool p_wait) {

	if (sockfd == -1) {
		return FAILED;
	}

	_set_blocking(p_wait);

	struct sockaddr_storage from = { 0 };
	int len = sizeof(struct sockaddr_storage);
	int ret;
	while ((ret = recvfrom(sockfd, (char *)recv_buffer, MIN((int)sizeof(recv_buffer), MAX(rb.space_left() - 24, 0)), 0, (struct sockaddr *)&from, &len)) > 0) {

		uint32_t port = 0;

		if (from.ss_family == AF_INET) {
			uint8_t type = (uint8_t)IP::TYPE_IPV4;
			rb.write(&type, 1);
			struct sockaddr_in *sin_from = (struct sockaddr_in *)&from;
			rb.write((uint8_t *)&sin_from->sin_addr, 4);
			port = ntohs(sin_from->sin_port);

		} else if (from.ss_family == AF_INET6) {

			uint8_t type = (uint8_t)IP::TYPE_IPV6;
			rb.write(&type, 1);

			struct sockaddr_in6 *s6_from = (struct sockaddr_in6 *)&from;
			rb.write((uint8_t *)&s6_from->sin6_addr, 16);

			port = ntohs(s6_from->sin6_port);

		} else {
			// WARN_PRINT("Ignoring packet with unknown address family");
			uint8_t type = (uint8_t)IP::TYPE_NONE;
			rb.write(&type, 1);
		};

		rb.write((uint8_t *)&port, 4);
		rb.write((uint8_t *)&ret, 4);
		rb.write(recv_buffer, ret);

		len = sizeof(struct sockaddr_storage);
		++queue_count;
	};

	if (ret == SOCKET_ERROR) {
		int error = WSAGetLastError();

		if (error == WSAEWOULDBLOCK) {
			// Expected when doing non-blocking sockets, retry later.
		} else if (error == WSAECONNRESET) {
			// If the remote target does not accept messages, this error may occur, but is harmless.
			// Once the remote target gets available, this message will disappear for new messages.
		} else {
			close();
			return FAILED;
		}
	}

	if (ret == 0) {
		close();
		return FAILED;
	};

	return OK;
}