HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_NetFDSet.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_NetFDSet.h ( UT Library, C++)
7  *
8  * COMMENTS:
9  */
10 
11 #ifndef __UT_NetFDSet__
12 #define __UT_NetFDSet__
13 
14 #include "UT_API.h"
15 #include "UT_UniquePtr.h"
16 #include "UT_Array.h"
17 
18 #include <SYS/SYS_Types.h>
19 
20 #include <stddef.h>
21 
22 // ============================================================================
23 /// Types of IO events that UT_NetFDSet can monitor a file descriptor for.
24 enum class UT_NetFDEvent
25 {
26  NETFD_READ = 0x1,
27  NETFD_WRITE = 0x2,
28  NETFD_ERROR = 0x4,
29  NETFD_HANGUP = 0x8
30 };
31 
32 // ============================================================================
33 /// Abstracts the socket file descriptor set for select() or poll() calls.
35 {
36 public:
37  /// Constructor and desctructor.
38  /// @param fast This informs the fd set to use the performant variant for
39  /// the platform. This is currently only support on some
40  /// platforms.
41  UT_NetFDSet(bool fast = false);
42  ~UT_NetFDSet();
43 
44  // The copy constructor and copy assignment should only be used on windows
45  // when you want to put an fdset in a loop (windows modifies the fdset when
46  // you check for events). There is no reason to copy an fd set on linux.
47  UT_NetFDSet(const UT_NetFDSet& net_fdset);
48  UT_NetFDSet& operator=(const UT_NetFDSet& net_fdset);
49 
51  {
52  public:
53  EventInfo();
54 
55  uint8 myReadActivity:1, myWriteActivity: 1,
56  myErrorActivity: 1, myHangupActivity: 1;
57  int myFD;
58  };
59 
60  /// Adds a file descriptor to the set with the intent of monitoring
61  /// the given IO event.
62  void add(int fd, UT_NetFDEvent event);
63  /// Adds a file descriptor to the set with the intent of monitoring a set
64  /// of given IO events.
65  void add(int fd, const std::initializer_list<UT_NetFDEvent>& events);
66 
67  /// Remove a file descriptor from the set.
68  void remove(int fd);
69 
70  /// Test whether a file descriptor is in the set
71  bool contains(int fd) const;
72 
73  /// Removes all added file descriptors for all events (resets the set).
74  void removeAll();
75 
76  /// Checks or waits for the IO events on the file descriptors in the set.
77  /// Essentially it's a wrapper for select() or poll().
78  /// @param timeout_ms The timeout to wait for connection (in miliseconds).
79  /// If zero, the call does not wait for the events, but instead
80  /// checks for the current state of the FDs in the set.
81  /// If negative, the call blocks indefinitely, waiting for the
82  /// first event. This is equivalent to passing NULL to select() or
83  /// -1 to poll() as time value.
84  ///
85  /// @return The total number of file descriptors on which there was
86  /// activitiy (events of the registered type), or -1 if error
87  /// occured. If time limit expires, returns 0.
88  int checkEvents(int timeout_ms) const;
89 
90  /// Constants denoting special wait times for checkEvents()
91  static const int INDEFINITE_WAIT; // blocking indefinitely for events
92  static const int NO_WAIT; // returning immediately
93 
94  /// Returns true if the set contains no file descriptors.
95  bool isEmpty() const;
96 
97  /// Returns true if there was any IO activity of the given type
98  /// on the given file descriptor.
99  bool isActive(int fd, UT_NetFDEvent event) const;
100 
101  /// Get all IO activity from an fd. Returns false if something has gone
102  /// wrong.
103  ///
104  /// @param fd This is the file descriptor to get the io information for.
105  /// @param info This is the information about a IO activity from the
106  /// file descriptor provided.
107  bool getInfoWithFD(int fd, EventInfo& info) const;
108 
109  /// Get all events current events in the set. This will wait up to
110  /// timeout_ms before returning.
111  ///
112  /// @param The list of all current events in the set. The size of the array
113  /// is 0..number of fd in set.
114  /// @param The maximum amount of time to wait before givin up. The timeout
115  /// is in ms.
116  /// @return True if `events` has any events in the list.
117  bool getAnyEvents(UT_Array<EventInfo>& events, int timeout_ms) const;
118 private:
119  /// Used to tell what impl its using.
120  bool myUsingFast;
121  /// Delegate that implements actual IO check (eg, select() or poll()).
122  static const size_t SIZEOF_IMPL = 1576;
123  char myImpl[SIZEOF_IMPL];
124 };
125 
126 
127 #endif
128 
UT_NetFDEvent
Types of IO events that UT_NetFDSet can monitor a file descriptor for.
Definition: UT_NetFDSet.h:24
Abstracts the socket file descriptor set for select() or poll() calls.
Definition: UT_NetFDSet.h:34
#define UT_API
Definition: UT_API.h:13
unsigned char uint8
Definition: SYS_Types.h:36
bool OIIO_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?
cl_event event
Definition: glew.h:3695
static const int NO_WAIT
Definition: UT_NetFDSet.h:92
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
static const int INDEFINITE_WAIT
Constants denoting special wait times for checkEvents()
Definition: UT_NetFDSet.h:91