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