Revision 45a7f54a net/socket.c

b/net/socket.c
32 32
#include "qemu-error.h"
33 33
#include "qemu-option.h"
34 34
#include "qemu_socket.h"
35
#include "iov.h"
35 36

  
36 37
typedef struct NetSocketState {
37 38
    NetClientState nc;
......
40 41
    int state; /* 0 = getting length, 1 = getting data */
41 42
    unsigned int index;
42 43
    unsigned int packet_len;
44
    unsigned int send_index;      /* number of bytes sent (only SOCK_STREAM) */
43 45
    uint8_t buf[4096];
44 46
    struct sockaddr_in dgram_dst; /* contains inet host and port destination iff connectionless (SOCK_DGRAM) */
45 47
    IOHandler *send_fn;           /* differs between SOCK_STREAM/SOCK_DGRAM */
......
88 90
    qemu_flush_queued_packets(&s->nc);
89 91
}
90 92

  
91
/* XXX: we consider we can send the whole packet without blocking */
92 93
static ssize_t net_socket_receive(NetClientState *nc, const uint8_t *buf, size_t size)
93 94
{
94 95
    NetSocketState *s = DO_UPCAST(NetSocketState, nc, nc);
95
    uint32_t len;
96
    len = htonl(size);
96
    uint32_t len = htonl(size);
97
    struct iovec iov[] = {
98
        {
99
            .iov_base = &len,
100
            .iov_len  = sizeof(len),
101
        }, {
102
            .iov_base = (void *)buf,
103
            .iov_len  = size,
104
        },
105
    };
106
    size_t remaining;
107
    ssize_t ret;
108

  
109
    remaining = iov_size(iov, 2) - s->send_index;
110
    ret = iov_send(s->fd, iov, 2, s->send_index, remaining);
97 111

  
98
    send_all(s->fd, (const uint8_t *)&len, sizeof(len));
99
    return send_all(s->fd, buf, size);
112
    if (ret == -1 && errno == EAGAIN) {
113
        ret = 0; /* handled further down */
114
    }
115
    if (ret == -1) {
116
        s->send_index = 0;
117
        return -errno;
118
    }
119
    if (ret < (ssize_t)remaining) {
120
        s->send_index += ret;
121
        net_socket_write_poll(s, true);
122
        return 0;
123
    }
124
    s->send_index = 0;
125
    return size;
100 126
}
101 127

  
102 128
static ssize_t net_socket_receive_dgram(NetClientState *nc, const uint8_t *buf, size_t size)

Also available in: Unified diff