static GError *
_send_and_read_reply (int fd, struct iovec *iov, unsigned int iovcount)
{
	if (!_send(fd, iov, iovcount))
		return NEWERROR(CODE_NETWORK_ERROR,
				"send error: (%d) %s",
				errno, strerror(errno));

	GError *err = NULL;
	guint8 buf[256];
	int r = sock_to_read (fd, 1000, buf, sizeof(buf)-1, &err);
	if (r < 0)
		return NEWERROR(CODE_NETWORK_ERROR,
				"read error: (%d) %s", err->code, err->message);
	if (r == 0)
		return NEWERROR(CODE_NETWORK_ERROR,
				"read error: closed by peer: (%d) %s",
				errno, strerror(errno));

	buf[r+1] = 0;

	if (!_is_success((gchar*) buf))
		return NEWERROR(CODE_BAD_REQUEST, "reply error: unexpected");
	return NULL;
}
Exemplo n.º 2
0
gint
sock_to_read_size(int fd, gint ms, void *buf, gsize bufSize, GError ** err)
{
	if (VTABLE.to_read_size)
		return VTABLE.to_read_size(fd, ms, buf, bufSize, err);

	gsize nbRead = 0;

	while (nbRead < bufSize) {
		int n = sock_to_read(fd, ms, ((guint8 *) buf) + nbRead, bufSize - nbRead, err);
		if (n < 0) {
			GSETERROR(err, "Read failed after %"G_GSIZE_FORMAT" bytes", nbRead);
			return n;
		}
		else if (n == 0) {
			GSETERROR(err, "Socket closed after %"G_GSIZE_FORMAT" bytes read", nbRead);
			return n;
		}
		else
			nbRead += n;
	}
	return nbRead;
}