HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebSocketMonitor.h
Go to the documentation of this file.
1 /*
2  * POPRIETARY 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: NET_WebSocketMonitor.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_WEBSOCKETMONITOR_H__
13 #define __NET_WEBSOCKETMONITOR_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_Time.h"
18 #include "NET_WebSocket.h"
19 
20 #include <UT/UT_Map.h>
21 #include <UT/UT_NetFDSet.h>
22 #include <UT/UT_StringHolder.h>
23 #include <UT/UT_ArrayStringMap.h>
24 #include <UT/UT_Url.h>
25 
26 #include <SYS/SYS_Compiler.h>
27 
28 #include <functional>
29 #include <ostream>
30 
31 /// The WebSocketMonitor is the class that monitors a set of fds.
32 /// When an event occurs the websocket instance defines how the event should be
33 /// handled.
34 class NET_API NET_WebSocketMonitor
35 {
36 public:
37  static NET_WebSocketMonitor& instance();
38 
39 
40  /// @brief Connect to a remote server using url. This will generate a
41  /// CONNECTED or CONNECT_ERROR.
42  ///
43  /// @param id The user provided url
44  /// @param url The url for connecting to the remote host.
45  /// @param timeout The timeout before giving up.
46  ///
47  /// @return True if the connection and upgrade was a success.
48  bool connect(const NET_WebSocketSPtr& websocket);
49 
50  /// @brief Run the dispatcher. This will loop forever until you tell it to
51  /// stop running.
52  void run();
53  /// @brief Run through the event loop exactly once.
54  void poll();
55  /// @brief Tell the dispatcher to stop.
56  void stop();
57 
58  /// @brief Check if the monitor is running. This will only return true if
59  /// the monitor is currently in the event loop from a run() call.
60  ///
61  /// @return True if the monitor is running.
62  SYS_NO_DISCARD_RESULT bool isRunning() const { return myIsRunning; }
63 private:
65  /// @brief Add a websocket to the list of websockets this monitor watches.
66  ///
67  /// @param websocket The websocket to watch.
68  ///
69  /// @return True if the websocket was actually added to the list to watch.
70  bool internalAdd(const NET_WebSocketSPtr& websocket);
71  /// @brief Remove a websocket from the list of sockets to watch.
72  ///
73  /// @param fd The fd used as the id to remove the websocket.
74  ///
75  /// @return True if the websocket was actually removed.
76  bool internalRemove(int fd);
77  /// @brief Clear the monitor of all currently active websockets to watch.
78  void internalClear();
79  /// @brief Check against our fd set for any new events. If an event occurs
80  /// The event callback is fired to allow the child classes to decide
81  /// how to handle the event.
82  ///
83  /// @param timeout The timeout used to check if any new events are present.
84  /// @param clb The callback to handle the event.
85  void checkEvents(int timeout);
86  /// @brief Go through each monitored websocket and determine if its time to
87  /// ping the websocket so that both sides know if they are alive.
88  void ping();
89 
90  /// @brief The amount of time since the previous successful ping that a new
91  /// one is required.
92  int myHeartbeat;
93  /// @brief The set of websockets we are currently watching.
95 
96  UT_Lock myLock;
97  bool myIsRunning;
98  /// @brief The object that monitors the fd for events.
99  UT_NetFDSet myFDs;
100 };
101 
102 #endif // __NET_WEBSOCKETMONITOR_H__
103 
Abstracts the socket file descriptor set for select() or poll() calls.
Definition: UT_NetFDSet.h:34
#define SYS_NO_DISCARD_RESULT
Definition: SYS_Compiler.h:93
SYS_NO_DISCARD_RESULT bool isRunning() const
Check if the monitor is running. This will only return true if the monitor is currently in the event ...
GLbitfield GLuint64 timeout
Definition: glew.h:6605
UT_SharedPtr< NET_WebSocket > NET_WebSocketSPtr