quickjs-tart

quickjs-based runtime for wallet-core logic
Log | Files | Refs | README | LICENSE

select.h (4260B)


      1 #ifndef HEADER_CURL_SELECT_H
      2 #define HEADER_CURL_SELECT_H
      3 /***************************************************************************
      4  *                                  _   _ ____  _
      5  *  Project                     ___| | | |  _ \| |
      6  *                             / __| | | | |_) | |
      7  *                            | (__| |_| |  _ <| |___
      8  *                             \___|\___/|_| \_\_____|
      9  *
     10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
     11  *
     12  * This software is licensed as described in the file COPYING, which
     13  * you should have received as part of this distribution. The terms
     14  * are also available at https://curl.se/docs/copyright.html.
     15  *
     16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
     17  * copies of the Software, and permit persons to whom the Software is
     18  * furnished to do so, under the terms of the COPYING file.
     19  *
     20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
     21  * KIND, either express or implied.
     22  *
     23  * SPDX-License-Identifier: curl
     24  *
     25  ***************************************************************************/
     26 
     27 #include "curl_setup.h"
     28 
     29 #ifdef HAVE_POLL_H
     30 #include <poll.h>
     31 #elif defined(HAVE_SYS_POLL_H)
     32 #include <sys/poll.h>
     33 #endif
     34 
     35 /*
     36  * Definition of pollfd struct and constants for platforms lacking them.
     37  */
     38 
     39 #if !defined(HAVE_SYS_POLL_H) && \
     40     !defined(HAVE_POLL_H) && \
     41     !defined(POLLIN)
     42 
     43 #define POLLIN      0x01
     44 #define POLLPRI     0x02
     45 #define POLLOUT     0x04
     46 #define POLLERR     0x08
     47 #define POLLHUP     0x10
     48 #define POLLNVAL    0x20
     49 
     50 struct pollfd
     51 {
     52     curl_socket_t fd;
     53     short   events;
     54     short   revents;
     55 };
     56 
     57 #endif
     58 
     59 #ifndef POLLRDNORM
     60 #define POLLRDNORM POLLIN
     61 #endif
     62 
     63 #ifndef POLLWRNORM
     64 #define POLLWRNORM POLLOUT
     65 #endif
     66 
     67 #ifndef POLLRDBAND
     68 #define POLLRDBAND POLLPRI
     69 #endif
     70 
     71 /* there are three CSELECT defines that are defined in the public header that
     72    are exposed to users, but this *IN2 bit is only ever used internally and
     73    therefore defined here */
     74 #define CURL_CSELECT_IN2 (CURL_CSELECT_ERR << 1)
     75 
     76 int Curl_socket_check(curl_socket_t readfd, curl_socket_t readfd2,
     77                       curl_socket_t writefd,
     78                       timediff_t timeout_ms);
     79 #define SOCKET_READABLE(x,z) \
     80   Curl_socket_check(x, CURL_SOCKET_BAD, CURL_SOCKET_BAD, z)
     81 #define SOCKET_WRITABLE(x,z) \
     82   Curl_socket_check(CURL_SOCKET_BAD, CURL_SOCKET_BAD, x, z)
     83 
     84 int Curl_poll(struct pollfd ufds[], unsigned int nfds, timediff_t timeout_ms);
     85 
     86 /*
     87    With Winsock the valid range is [0..INVALID_SOCKET-1] according to
     88    https://docs.microsoft.com/en-us/windows/win32/winsock/socket-data-type-2
     89 */
     90 #ifdef USE_WINSOCK
     91 #define VALID_SOCK(s) ((s) < INVALID_SOCKET)
     92 #define FDSET_SOCK(x) 1
     93 #define VERIFY_SOCK(x) do { \
     94   if(!VALID_SOCK(x)) { \
     95     SET_SOCKERRNO(SOCKEINVAL); \
     96     return -1; \
     97   } \
     98 } while(0)
     99 #else
    100 #define VALID_SOCK(s) ((s) >= 0)
    101 
    102 /* If the socket is small enough to get set or read from an fdset */
    103 #define FDSET_SOCK(s) ((s) < FD_SETSIZE)
    104 
    105 #define VERIFY_SOCK(x) do {                     \
    106     if(!VALID_SOCK(x) || !FDSET_SOCK(x)) {      \
    107       SET_SOCKERRNO(SOCKEINVAL);                \
    108       return -1;                                \
    109     }                                           \
    110   } while(0)
    111 #endif
    112 
    113 struct curl_pollfds {
    114   struct pollfd *pfds;
    115   unsigned int n;
    116   unsigned int count;
    117   BIT(allocated_pfds);
    118 };
    119 
    120 void Curl_pollfds_init(struct curl_pollfds *cpfds,
    121                        struct pollfd *static_pfds,
    122                        unsigned int static_count);
    123 
    124 void Curl_pollfds_reset(struct curl_pollfds *cpfds);
    125 
    126 void Curl_pollfds_cleanup(struct curl_pollfds *cpfds);
    127 
    128 CURLcode Curl_pollfds_add_ps(struct curl_pollfds *cpfds,
    129                              struct easy_pollset *ps);
    130 
    131 CURLcode Curl_pollfds_add_sock(struct curl_pollfds *cpfds,
    132                                curl_socket_t sock, short events);
    133 
    134 struct Curl_waitfds {
    135   struct curl_waitfd *wfds;
    136   unsigned int n;
    137   unsigned int count;
    138 };
    139 
    140 void Curl_waitfds_init(struct Curl_waitfds *cwfds,
    141                        struct curl_waitfd *static_wfds,
    142                        unsigned int static_count);
    143 
    144 unsigned int Curl_waitfds_add_ps(struct Curl_waitfds *cwfds,
    145                                  struct easy_pollset *ps);
    146 
    147 #endif /* HEADER_CURL_SELECT_H */