My application is going to send a huge amount of data over the network, so I decided (because I use Linux) to use epoll and splice. This is how I see it (pseudo-code):
epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event while(1) { epoll_wait (tmp_structure); if (tmp_structure->fd == file_descriptor) { epoll_ctl (file_fd, EPOLL_CTL_DEL); epoll_ctl (tcp_socket_fd, EPOLL_CTL_ADD); // wait for EPOLLOUT event } if (tmp_structure->fd == tcp_socket_descriptor) { splice (file_fd, tcp_socket_fd); epoll_ctl (tcp_socket_fd, EPOLL_CTL_DEL); epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event } }
I assume that my application will open up to 2000 TCP sockets. I want to ask you about two things:
- There will be many calls to epoll_ctl, wouldn't it be slow if I have so many sockets?
- First, the file descriptor must be readable, and before the socket is writable, there will be some interval. Can I be sure that the moment the socket becomes writable, the file descriptor is still readable (to avoid blocking the call)?
Goofy source share