Zero-copy read

master
Vitaliy Filippov 1 year ago
parent 22e71c2c12
commit bceb7d5328
  1. 26
      pingpong.c

@ -72,9 +72,21 @@ int loop(void *arg)
}
else if (events[i].events & EPOLLIN)
{
size_t readlen = vppcom_session_read(events[i].data.fd, buf, message_size);
vppcom_data_segment_t seg[8];
size_t readlen = vppcom_session_read_segments(
events[i].data.fd, seg, sizeof(seg)/sizeof(seg[0]), message_size
);
if (readlen > 0)
vppcom_session_write(events[i].data.fd, buf, readlen);
{
size_t left = readlen;
for (int i = 0; left > 0; i++)
{
vppcom_session_write(events[i].data.fd, seg[i].data, seg[i].len);
left -= seg[i].len;
}
// FIXME: I'm not sure if I can do that. Maybe the buffer can't be freed until it's really sent
vppcom_session_free_segments(events[i].data.fd, readlen);
}
else
{
vppcom_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
@ -129,6 +141,7 @@ int loop_client(void *arg)
if (events[i].events & (EPOLLHUP|EPOLLERR))
{
/* Simply close socket */
printf("Error event\n");
vppcom_epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, NULL);
vppcom_session_close(events[i].data.fd);
sigint_handler(SIGINT);
@ -139,9 +152,14 @@ int loop_client(void *arg)
/* Read everything back */
if (to_read > 0)
{
len = vppcom_session_read(events[i].data.fd, buf, message_size);
vppcom_data_segment_t seg[8];
len = vppcom_session_read_segments(
events[i].data.fd, seg, sizeof(seg)/sizeof(seg[0]), to_read
);
if (len > 0)
to_read = to_read > len ? to_read-len : 0;
// FIXME: I'm not sure if I can do that. Maybe the buffer can't be freed until it's really sent
vppcom_session_free_segments(events[i].data.fd, len);
if (to_read == 0)
{
/* Calculate latency */
@ -297,7 +315,7 @@ int main(int argc, char *argv[])
assert((epfd = vppcom_epoll_create()) > 0);
ev.data.fd = sockfd;
ev.events = (server ? EPOLLOUT : 0) | EPOLLIN|EPOLLHUP;
ev.events = (server ? EPOLLOUT : 0) | EPOLLIN|EPOLLHUP|EPOLLET;
rv = vppcom_epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);
if (rv < 0)
{

Loading…
Cancel
Save