curl / Mailing Lists / curl-library / Single Mail
Buy commercial curl support from WolfSSL. We help you work out your issues, debug your libcurl applications, use the API, port to new platforms, add new features and more. With a team lead by the curl founder himself.

Re: Question/proposal about function returning pollfds from multi handle

From: Dmitry Karpov via curl-library <curl-library_at_lists.haxx.se>
Date: Wed, 20 Dec 2023 23:03:17 +0000

> Do you iterate over the sockets in the socket hash to avoid adding the same socket multiple times?

I use iteration technique similar to the one used in the multi_wait().

The function looks like:

CURLMcode curl_multi_pollfds(struct Curl_multi* multi,
                             struct curl_waitfd* ufds,
                             unsigned int size,
                             unsigned int* fd_count)
{
  struct Curl_easy* data;
  unsigned int nfds = 0;
  struct easy_pollset ps;
  unsigned int i;
  CURLMcode result = CURLM_OK;

  if(!GOOD_MULTI_HANDLE(multi))
    return CURLM_BAD_HANDLE;

  if(multi->in_callback)
    return CURLM_RECURSIVE_API_CALL;

  memset(&ps, 0, sizeof(ps));
  for(data = multi->easyp; data; data = data->next) {
    multi_getsock(data, &ps);

    for(i = 0; i < ps.num; i++) {
      if(nfds < size) {
        struct curl_waitfd* ufd = &ufds[nfds];
        ufd->fd = ps.sockets[i];
        ufd->events = 0;
        if(ps.actions[i] & CURL_POLL_IN)
            ufd->events |= CURL_WAIT_POLLIN;
        if(ps.actions[i] & CURL_POLL_OUT)
            ufd->events |= CURL_WAIT_POLLOUT;
      }
      else if(size > 0)
        result = CURLM_OUT_OF_MEMORY;
        if(!fd_count)
          return result;
      nfds++;
    }
  }

  if(fd_count)
   *fd_count = nfds;
  return result;
}

I guess the multi_getsock(data, &ps) should take care of avoiding adding sockets multiple times.

Thanks,
Dmitry Karpov

-----Original Message-----
From: Daniel Stenberg <daniel_at_haxx.se>
Sent: Wednesday, December 20, 2023 2:54 PM
To: Dmitry Karpov via curl-library <curl-library_at_lists.haxx.se>
Cc: Dmitry Karpov <dkarpov_at_roku.com>
Subject: [EXTERNAL] Re: Question/proposal about function returning pollfds from multi handle

On Wed, 20 Dec 2023, Dmitry Karpov via curl-library wrote:

> The function implementation is relatively simple and small. I can
> create a PR for it if there are no objections.

Please do, it will give us something to discuss around.

Do you iterate over the sockets in the socket hash to avoid adding the same socket multiple times?

-- 
  / daniel.haxx.se
  | Commercial curl support up to 24x7 is available!
  | Private help, bug fixes, support, ports, new features
  | https://curl.se/support.html
-- 
Unsubscribe: https://lists.haxx.se/mailman/listinfo/curl-library
Etiquette:   https://curl.se/mail/etiquette.html
Received on 2023-12-21