1--- 2c: Copyright (C) Daniel Stenberg, <daniel.se>, et al. 3SPDX-License-Identifier: curl 4Title: curl_multi_assign 5Section: 3 6Source: libcurl 7See-also: 8 - curl_multi_setopt (3) 9 - curl_multi_socket_action (3) 10--- 11 12# NAME 13 14curl_multi_assign - set data to associate with an internal socket 15 16# SYNOPSIS 17 18~~~c 19#include <curl/curl.h> 20 21CURLMcode curl_multi_assign(CURLM *multi_handle, curl_socket_t sockfd, 22 void *sockptr); 23~~~ 24 25# DESCRIPTION 26 27This function creates an association in the multi handle between the given 28socket and a private pointer of the application. This is designed for 29curl_multi_socket_action(3) uses. 30 31When set, the *sockptr* pointer is passed to all future socket callbacks 32for the specific *sockfd* socket. 33 34If the given *sockfd* is not already in use by libcurl, this function 35returns an error. 36 37libcurl only keeps one single pointer associated with a socket, so calling 38this function several times for the same socket makes the last set pointer get 39used. 40 41The idea here being that this association (socket to private pointer) is 42something that just about every application that uses this API needs and then 43libcurl can just as well do it since it already has the necessary 44functionality. 45 46It is acceptable to call this function from your multi callback functions. 47 48# EXAMPLE 49 50~~~c 51int main(void) 52{ 53 CURLM *multi = curl_multi_init(); 54 void *ourstructp; /* pointer to our data */ 55 curl_socket_t fd; /* file descriptor to associate our data with */ 56 57 /* make our struct pointer associated with socket fd */ 58 CURLMcode mc = curl_multi_assign(multi, fd, ourstructp); 59 if(mc) 60 printf("error: %s\n", curl_multi_strerror(mc)); 61} 62~~~ 63 64# AVAILABILITY 65 66Added in 7.15.5 67 68# RETURN VALUE 69 70The standard CURLMcode for multi interface error codes. 71 72# TYPICAL USAGE 73 74In a typical application you allocate a struct or at least use some kind of 75semi-dynamic data for each socket that we must wait for action on when using 76the curl_multi_socket_action(3) approach. 77 78When our socket-callback gets called by libcurl and we get to know about yet 79another socket to wait for, we can use curl_multi_assign(3) to point out 80the particular data so that when we get updates about this same socket again, 81we do not have to find the struct associated with this socket by ourselves. 82