示例#1
0
文件: pdu.c 项目: sahlberg/libnfs
int rpc_queue_pdu(struct rpc_context *rpc, struct rpc_pdu *pdu)
{
	int size, recordmarker;

	assert(rpc->magic == RPC_CONTEXT_MAGIC);

	if (rpc->timeout > 0) {
		pdu->timeout = rpc_current_time() + rpc->timeout;
#ifndef HAVE_CLOCK_GETTIME
		/* If we do not have GETTIME we fallback to time() which
		 * has 1s granularity for its timestamps.
		 * We thus need to bump the timeout by 1000ms
		 * so that the PDU will timeout within 1.0 - 2.0 seconds.
		 * Otherwise setting a 1s timeout would trigger within
		 * 0.001 - 1.0s.
		 */
		pdu->timeout += 1000;
#endif
	} else {
		pdu->timeout = 0;
	}

	size = zdr_getpos(&pdu->zdr);

	/* for udp we dont queue, we just send it straight away */
	if (rpc->is_udp != 0) {
		unsigned int hash;

// XXX add a rpc->udp_dest_sock_size  and get rid of sys/socket.h and netinet/in.h
		if (sendto(rpc->fd, pdu->zdr.buf, size, MSG_DONTWAIT,
                           (struct sockaddr *)&rpc->udp_dest,
                           sizeof(rpc->udp_dest)) < 0) {
			rpc_set_error(rpc, "Sendto failed with errno %s", strerror(errno));
			rpc_free_pdu(rpc, pdu);
			return -1;
		}

		hash = rpc_hash_xid(pdu->xid);
		rpc_enqueue(&rpc->waitpdu[hash], pdu);
		rpc->waitpdu_len++;
		return 0;
	}

	/* write recordmarker */
	zdr_setpos(&pdu->zdr, 0);
	recordmarker = (size - 4) | 0x80000000;
	zdr_int(&pdu->zdr, &recordmarker);

	pdu->outdata.size = size;
	rpc_enqueue(&rpc->outqueue, pdu);

	return 0;
}
示例#2
0
文件: socket.c 项目: touchft/libnfs
static int rpc_write_to_socket(struct rpc_context *rpc)
{
	int32_t count;
	struct rpc_pdu *pdu;

	assert(rpc->magic == RPC_CONTEXT_MAGIC);

	if (rpc->fd == -1) {
		rpc_set_error(rpc, "trying to write but not connected");
		return -1;
	}

	while ((pdu = rpc->outqueue.head) != NULL) {
		int64_t total;

		total = pdu->outdata.size;

		count = send(rpc->fd, pdu->outdata.data + pdu->written, total - pdu->written, 0);
		if (count == -1) {
			if (errno == EAGAIN || errno == EWOULDBLOCK) {
				return 0;
			}
			rpc_set_error(rpc, "Error when writing to socket :%s(%d)", strerror(errno), errno);
			return -1;
		}

		pdu->written += count;
		if (pdu->written == total) {
			unsigned int hash;

			rpc->outqueue.head = pdu->next;
			if (pdu->next == NULL)
				rpc->outqueue.tail = NULL;

			hash = rpc_hash_xid(pdu->xid);
			rpc_enqueue(&rpc->waitpdu[hash], pdu);
		}
	}
	return 0;
}