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_Array.h"
16 #include "UT_NonCopyable.h"
17 #include "UT_SmallArray.h"
18 #include "UT_UniquePtr.h"
19 
20 #include <SYS/SYS_Types.h>
21 #include <SYS/SYS_Compiler.h>
22 
23 #include <stddef.h>
24 
25 // ============================================================================
26 /// Types of IO events that UT_NetFDSet can monitor a file descriptor for.
27 enum class UT_NetFDEvent
28 {
29  NETFD_READ = 0x1,
30  NETFD_WRITE = 0x2,
31  NETFD_ERROR = 0x4,
32  NETFD_HANGUP = 0x8
33 };
34 
35 // ============================================================================
36 // The different modes that UT_NetFDSet can monitor file descriptors in. The
37 // default mode is the easiest mode to run the monitor in and is the recommended
38 // mode to use for regular monitoring needs. In some cases its more performant
39 // to use NETFD_ET_MODE which isnt supported on all platforms (default back to
40 // the default mode if not support on the platform). The ET mode typically needs
41 // a lot of care to get correct otherwise the program could hault waiting
42 // forever. If using ET mode please take extreme care before using!!!
43 enum class UT_NetFDMode
44 {
46  NETFD_ET_MODE = 1
47 };
48 
49 // ============================================================================
50 /// Abstracts the socket file descriptor set for select() or poll() calls.
52 {
53 public:
54  /// Constructor and desctructor.
55  /// @param fast This informs the fd set to use the performant variant for
56  /// the platform. This is currently only support on some
57  /// platforms.
58  /// @param mode The mode to run the fd set in.
60  bool fast = false,
62  ~UT_NetFDSet();
63 
65 
67  {
68  public:
69  EventInfo();
70 
71  uint8 myReadActivity:1, myWriteActivity: 1,
72  myErrorActivity: 1, myHangupActivity: 1;
73  int myFD;
74  };
75 
76  /// Adds a file descriptor to the set with the intent of monitoring
77  /// the given IO event.
78  bool add(int fd, UT_NetFDEvent event);
79  /// Adds a file descriptor to the set with the intent of monitoring a set
80  /// of given IO events.
81  bool add(int fd, const UT_SmallArray<UT_NetFDEvent>& events);
82 
83  /// Modify a file descriptor flags.
84  bool modify(int fd, const UT_SmallArray<UT_NetFDEvent>& events);
85 
86  /// Remove a file descriptor from the set.
87  void remove(int fd);
88 
89  /// Test whether a file descriptor is in the set
90  bool contains(int fd) const;
91 
92  /// Removes all added file descriptors for all events (resets the set).
93  void removeAll();
94 
95  /// Checks or waits for the IO events on the file descriptors in the set.
96  /// Essentially it's a wrapper for select() or poll().
97  /// @param timeout_ms The timeout to wait for connection (in miliseconds).
98  /// If zero, the call does not wait for the events, but instead
99  /// checks for the current state of the FDs in the set.
100  /// If negative, the call blocks indefinitely, waiting for the
101  /// first event. This is equivalent to passing NULL to select() or
102  /// -1 to poll() as time value.
103  ///
104  /// @return The total number of file descriptors on which there was
105  /// activitiy (events of the registered type), or -1 if error
106  /// occured. If time limit expires, returns 0.
107  int checkEvents(int timeout_ms) const;
108 
109  /// Constants denoting special wait times for checkEvents()
110  static const int INDEFINITE_WAIT; // blocking indefinitely for events
111  static const int NO_WAIT; // returning immediately
112 
113  /// Returns true if the set contains no file descriptors.
114  bool isEmpty() const;
115 
116  /// Returns true if there was any IO activity of the given type
117  /// on the given file descriptor.
118  bool isActive(int fd, UT_NetFDEvent event) const;
119 
120  /// Get all IO activity from an fd. Returns false if something has gone
121  /// wrong.
122  ///
123  /// @param fd This is the file descriptor to get the io information for.
124  /// @param info This is the information about a IO activity from the
125  /// file descriptor provided.
126  bool getInfoWithFD(int fd, EventInfo& info) const;
127 
128  /// Get all events current events in the set. This will wait up to
129  /// timeout_ms before returning.
130  ///
131  /// @param events The list of all current events in the set. The size of
132  /// the array is 0..number of fd in set.
133  /// @param timeout_ms The maximum amount of time to wait before givin
134  /// up. The timeout is in ms.
135  /// @return True if `events` has any events in the list.
136  bool getAnyEvents(UT_Array<EventInfo>& events, int timeout_ms) const;
137 
138  /// In some cases the mode wont be decided until after the fd set is created.
139  /// The mode CANNOT be changed while the fdset has fd in it!
140  /// @param mode The mode to run the fd set in.
141  void setMode(UT_NetFDMode mode) { myMode = mode; }
142  SYS_NO_DISCARD_RESULT UT_NetFDMode mode() const { return myMode; }
143 private:
144  /// Used to tell what impl its using.
145  bool myUsingFast;
146  /// The mode to run the FD set in.
147  UT_NetFDMode myMode;
148  /// Delegate that implements actual IO check (eg, select() or poll()).
149  static const size_t SIZEOF_IMPL = 1576;
150  char myImpl[SIZEOF_IMPL];
151 };
152 
153 
154 #endif
155 
UT_NetFDEvent
Types of IO events that UT_NetFDSet can monitor a file descriptor for.
Definition: UT_NetFDSet.h:27
Abstracts the socket file descriptor set for select() or poll() calls.
Definition: UT_NetFDSet.h:51
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:623
#define UT_API
Definition: UT_API.h:14
SYS_NO_DISCARD_RESULT UT_NetFDMode mode() const
Definition: UT_NetFDSet.h:142
struct _cl_event * event
Definition: glcorearb.h:2961
unsigned char uint8
Definition: SYS_Types.h:36
void setMode(UT_NetFDMode mode)
Definition: UT_NetFDSet.h:141
#define UT_NON_COPYABLE(CLASS)
Define deleted copy constructor and assignment operator inside a class.
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
GLenum mode
Definition: glcorearb.h:99
UT_NetFDMode
Definition: UT_NetFDSet.h:43
static const int NO_WAIT
Definition: UT_NetFDSet.h:111
ImageBuf OIIO_API add(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
bool OIIO_UTIL_API contains(string_view a, string_view b)
Does 'a' contain the string 'b' within it?
GLuint64 GLenum GLint fd
Definition: RE_OGL.h:262
static const int INDEFINITE_WAIT
Constants denoting special wait times for checkEvents()
Definition: UT_NetFDSet.h:110