client: Split ring_to_iovec() off ring_to_file()
Signed-off-by: Markus Armbruster <armbru@pond.sub.org>
This commit is contained in:
parent
8301e0f144
commit
8fe2b949e6
1 changed files with 25 additions and 13 deletions
|
@ -27,7 +27,7 @@
|
||||||
* ringbuf.c: Simple ring buffer
|
* ringbuf.c: Simple ring buffer
|
||||||
*
|
*
|
||||||
* Known contributors to this file:
|
* Known contributors to this file:
|
||||||
* Markus Armbruster, 2007-2015
|
* Markus Armbruster, 2007-2017
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
@ -215,19 +215,15 @@ ring_from_file(struct ring *r, int fd)
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Drain ring buffer to file referred by file descriptor @fd.
|
* Set up @iov[] to describe complete contents of ring buffer.
|
||||||
* If ring buffer is already empty, do nothing and return 0.
|
* @iov[] must have at least two elements.
|
||||||
* Else attempt to write complete contents with writev(), and return
|
* Return number of elements used (zero for an empty ring buffer).
|
||||||
* its value.
|
|
||||||
*/
|
*/
|
||||||
int
|
static int
|
||||||
ring_to_file(struct ring *r, int fd)
|
ring_to_iovec(struct ring *r, struct iovec iov[])
|
||||||
{
|
{
|
||||||
unsigned cons = r->cons % RING_SIZE;
|
unsigned cons = r->cons % RING_SIZE;
|
||||||
unsigned prod = r->prod % RING_SIZE;
|
unsigned prod = r->prod % RING_SIZE;
|
||||||
struct iovec iov[2];
|
|
||||||
int cnt;
|
|
||||||
ssize_t res;
|
|
||||||
|
|
||||||
if (r->cons == r->prod)
|
if (r->cons == r->prod)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -239,15 +235,31 @@ ring_to_file(struct ring *r, int fd)
|
||||||
/* r->buf[..prod-1] */
|
/* r->buf[..prod-1] */
|
||||||
iov[1].iov_base = r->buf;
|
iov[1].iov_base = r->buf;
|
||||||
iov[1].iov_len = prod;
|
iov[1].iov_len = prod;
|
||||||
cnt = 2;
|
return 2;
|
||||||
} else {
|
} else {
|
||||||
/* r->buf[cons..prod-1] */
|
/* r->buf[cons..prod-1] */
|
||||||
iov[0].iov_len = prod - cons;
|
iov[0].iov_len = prod - cons;
|
||||||
cnt = 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Drain ring buffer to file referred by file descriptor @fd.
|
||||||
|
* If ring buffer is already empty, do nothing and return 0.
|
||||||
|
* Else attempt to write complete contents with writev(), and return
|
||||||
|
* its value.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
ring_to_file(struct ring *r, int fd)
|
||||||
|
{
|
||||||
|
struct iovec iov[2];
|
||||||
|
int cnt;
|
||||||
|
ssize_t res;
|
||||||
|
|
||||||
|
cnt = ring_to_iovec(r, iov);
|
||||||
res = writev(fd, iov, cnt);
|
res = writev(fd, iov, cnt);
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
return res;
|
return res;
|
||||||
r->cons += res;
|
ring_discard(r, res);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue