GNURLMOPT_...ETFUNCTION(3)
β¨ NAME β¨
hey bestie!! π you're on the GNURLMOPT_...ETFUNCTION(3) page, from the curl_multi_setopt options manual, and this one is all about:
CURLMOPT_SOCKETFUNCTIONβ callback informed about what to wait for β¨
π οΈ SYNOPSIS π οΈ
here's the shape of it, no cap:
#include <gnurl/curl.h>
int socket_callback(CURL *easy, /* easy handle */
curl_socket_t s, /* socket */
int what, /* describes the socket */
void *userp, /* private callback pointer */
void *socketp); /* private socket pointer */
CURLMcode curl_multi_setopt(CURLM *handle, CURLMOPT_SOCKETFUNCTION, socket_callback);
π DESCRIPTION π
okay so here's the tea: you pass a pointer to your callback function, and it should match the prototype shown above. π«Ά
when the curl_multi_socket_action(3) function is called, it informs the application about updates in the socket (file descriptor) status by doing none, one, or multiple calls to the socket_callback. the callback function gets status updates with changes since the previous time the callback was called. if the given callback pointer is set to NULL, no callback will be called. periodt. π
π CALLBACK ARGUMENTS π
let's break down the little gang of args, bestie:
easyβ identifies the specific transfer for which this update is related.sβ the specific socket this function invocation concerns. if thewhatargument is notCURL_POLL_REMOVE, then it holds information about what activity on this socket the application is supposed to monitor. subsequent calls to this callback might update thewhatbits for a socket that is already monitored.userpβ set withCURLMOPT_SOCKETDATA(3).socketpβ set withcurl_multi_assign(3)or will beNULL.
the what parameter informs the callback on the status of the given socket. it can hold one of these values:
CURL_POLL_INβ wait for incoming data. for the socket to become readable. πCURL_POLL_OUTβ wait for outgoing data. for the socket to become writable. πCURL_POLL_INOUTβ wait for incoming and outgoing data. for the socket to become readable or writable. π«CURL_POLL_REMOVEβ the specified socket/file descriptor is no longer used by libcurl. bye boo π
πΈ DEFAULT πΈ
NULL (no callback) β literally no callback energy. π«Ά
π PROTOCOLS π
All β every protocol, bestie. π
π₯ EXAMPLE π₯
here's a whole little example, copied exactly because code is sacred:
static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp)
{
GlobalInfo *g = (GlobalInfo*) cbp;
SockInfo *fdp = (SockInfo*) sockp;
if(what == CURL_POLL_REMOVE) {
remsock(fdp);
}
else {
if(!fdp) {
addsock(s, e, what, g);
}
else {
setsock(fdp, s, e, what, g);
}
}
return 0;
}
main()
{
GlobalInfo setup;
/* ... use socket callback and custom pointer */
curl_multi_setopt(multi, CURLMOPT_SOCKETFUNCTION, sock_cb);
curl_multi_setopt(multi, CURLMOPT_SOCKETDATA, &setup);
}
π AVAILABILITY π
added in 7.15.4 β ancient but iconic. β¨
β RETURN VALUE β
returns CURLM_OK. slay. π
π¦ SEE ALSO π¦
go read these next, bestie:
this page is giving libcurl 7.39.0 energy, dated 3 Nov 2016. bye bestie, go make that callback cute. πβ¨
π¦ where this page comes from
- nixos 26.05: libgnurl 7.72.0
- nixpkgs unstable: libgnurl 7.72.0
- Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms