Callback on sockets in linux
c, linux, networking, openssl, sockets
Solution
Some event libraries implement such callback hooks:
- libev
- libevent
and all graphical or HTTP server libraries (e.g. Gtk/Glib, Qt, LibOnion, ...) provide (or use) such event libraries (around a multiplexing syscall like poll(2) etc).
Both Glib (from GTK) and QtCore (from Qt) are event libraries usable without any GUI
Read also about the C10K problem
Problem
Is there any way to associate callback functions with sockets in Linux? For example,when the function ``` connect(sock, (struct sockaddr *)peeraddr, sizeof(*peeraddr)) ``` has connected the client with the server, is there a way to associate a function "`afterRecv`" with the socket of name `sock` such that after function `recv` has read some data from `sock`, `afterRecv` gets called? That `sock` is of blocking type. This is due to the many internal `send/recv` calls on `sock` during the handshake period of `OpenSSL`. If I modify `OpenSSL`, it would be cumbersome to modify every `recv`. So I wondered, if I can add a callback instead. The flow should be: `connect` to socket `recv` function When `recv` has read data, `afterRecv` gets called as a callback on `recv`