Netpoll

Overview

The netpoll module provides the mechanism to interact with network poller(currently only epoll supported).

Example

csp_netpoll_register(sockfd);

while (true) {
  int r = csp_netpoll_wait_read(sockfd, 0);
  if (r != csp_netpoll_avail) {
    break;
  }

  int conn = accept(sockfd, &addr, &len);
  if (conn != -1) {
    async(handle_conn(conn));
  }
}

csp_netpoll_unregister(sockfd);

Index

bool csp_netpoll_register(int fd)


csp_netpoll_register registers fd to the netpoll so that the netpoll can monitor io events releated to the fd.

  • fd: The file descriptor to register.

int csp_netpoll_wait_read(int fd, int timeout)


csp_netpoll_wait_read blocks until we can read from the fd.

  • fd: The file descriptor registered.
  • timeout: The duration we wait in millseconds. If it’s 0 or negative, this argument will be ignored and it will block until we got related io events.

It returns,

  • csp_netpoll_avail when read events are available.
  • csp_netpoll_timeout when timeout.

int csp_netpoll_wait_write(int fd, int timeout)


csp_netpoll_wait_write blocks until we can write to the fd.

  • fd: The file descriptor registered.
  • timeout: The duration we wait in millseconds. If it’s 0 or negative, this argument will be ignored and it will block until we got related io events.

It returns,

  • csp_netpoll_avail when write events are available.
  • csp_netpoll_timeout when timeout.

csp_netpoll_unregister(int fd)


csp_netpoll_unregister unregisters fd from the netpoll.

  • fd: The file descriptor to unregister.