mirror of
https://github.com/Motorhead1991/qemu.git
synced 2025-08-02 15:23:53 -06:00
cleanup qemu_co_sendv(), qemu_co_recvv() and friends
The same as for non-coroutine versions in previous patches: rename arguments to be more obvious, change type of arguments from int to size_t where appropriate, and use common code for send and receive paths (with one extra argument) since these are exactly the same. Use common iov_send_recv() directly. qemu_co_sendv(), qemu_co_recvv(), and qemu_co_recv() are now trivial #define's merely adding one extra arg. qemu_co_sendv() and qemu_co_recvv() callers are converted to different argument order and extra `iov_cnt' argument. Signed-off-by: Michael Tokarev <mjt@tls.msk.ru>
This commit is contained in:
parent
e3e87df4c9
commit
2fc8ae1dd7
4 changed files with 55 additions and 88 deletions
|
@ -27,71 +27,39 @@
|
|||
#include "qemu-coroutine.h"
|
||||
#include "iov.h"
|
||||
|
||||
int coroutine_fn qemu_co_recvv(int sockfd, struct iovec *iov,
|
||||
int len, int iov_offset)
|
||||
ssize_t coroutine_fn
|
||||
qemu_co_sendv_recvv(int sockfd, struct iovec *iov, unsigned iov_cnt,
|
||||
size_t offset, size_t bytes, bool do_send)
|
||||
{
|
||||
int total = 0;
|
||||
int ret;
|
||||
while (len) {
|
||||
ret = iov_recv(sockfd, iov, iov_offset + total, len);
|
||||
if (ret < 0) {
|
||||
size_t done = 0;
|
||||
ssize_t ret;
|
||||
while (done < bytes) {
|
||||
ret = iov_send_recv(sockfd, iov,
|
||||
offset + done, bytes - done, do_send);
|
||||
if (ret > 0) {
|
||||
done += ret;
|
||||
} else if (ret < 0) {
|
||||
if (errno == EAGAIN) {
|
||||
qemu_coroutine_yield();
|
||||
continue;
|
||||
}
|
||||
if (total == 0) {
|
||||
total = -1;
|
||||
} else if (done == 0) {
|
||||
return -1;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
} else if (ret == 0 && !do_send) {
|
||||
/* write (send) should never return 0.
|
||||
* read (recv) returns 0 for end-of-file (-data).
|
||||
* In both cases there's little point retrying,
|
||||
* but we do for write anyway, just in case */
|
||||
break;
|
||||
}
|
||||
if (ret == 0) {
|
||||
break;
|
||||
}
|
||||
total += ret, len -= ret;
|
||||
}
|
||||
|
||||
return total;
|
||||
return done;
|
||||
}
|
||||
|
||||
int coroutine_fn qemu_co_sendv(int sockfd, struct iovec *iov,
|
||||
int len, int iov_offset)
|
||||
ssize_t coroutine_fn
|
||||
qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send)
|
||||
{
|
||||
int total = 0;
|
||||
int ret;
|
||||
while (len) {
|
||||
ret = iov_send(sockfd, iov, iov_offset + total, len);
|
||||
if (ret < 0) {
|
||||
if (errno == EAGAIN) {
|
||||
qemu_coroutine_yield();
|
||||
continue;
|
||||
}
|
||||
if (total == 0) {
|
||||
total = -1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
total += ret, len -= ret;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
int coroutine_fn qemu_co_recv(int sockfd, void *buf, int len)
|
||||
{
|
||||
struct iovec iov;
|
||||
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = len;
|
||||
|
||||
return qemu_co_recvv(sockfd, &iov, len, 0);
|
||||
}
|
||||
|
||||
int coroutine_fn qemu_co_send(int sockfd, void *buf, int len)
|
||||
{
|
||||
struct iovec iov;
|
||||
|
||||
iov.iov_base = buf;
|
||||
iov.iov_len = len;
|
||||
|
||||
return qemu_co_sendv(sockfd, &iov, len, 0);
|
||||
struct iovec iov = { .iov_base = buf, .iov_len = bytes };
|
||||
return qemu_co_sendv_recvv(sockfd, &iov, 1, 0, bytes, do_send);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue