NAP
websocketinterface.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 
7 // Local Includes
8 #include "websocketevent.h"
9 
10 // External Includes
11 #include <nap/resource.h>
12 #include <queue>
13 #include <mutex>
14 
15 namespace nap
16 {
17  class WebSocketService;
18 
28  class NAPAPI WebSocketInterface : public Resource
29  {
30  friend class WebSocketService;
31  RTTI_ENABLE(Resource)
32  public:
38 
39  // Destructor
40  virtual ~WebSocketInterface();
41 
46  virtual bool init(utility::ErrorState& errorState) override;
47 
54  template<typename T>
55  T& as();
56 
63  template<typename T>
64  const T& as() const;
65 
66  protected:
72  void addEvent(WebSocketEventPtr newEvent);
73 
74  // Queue that holds all the consumed events
75  std::queue<WebSocketEventPtr> mEvents;
76 
77  // Mutex associated with setting / getting events
78  std::mutex mEventMutex;
79 
80  // Handle to the web socket service
81  WebSocketService* mService = nullptr;
82 
83  private:
89  void consumeEvents(std::queue<WebSocketEventPtr>& outEvents);
90 
91  // If the interface registered on init
92  bool mRegistered = false;
93  };
94 
95 
97  // Template definitions
99 
100  template<typename T>
102  {
103  const T* return_p = nullptr;
104  if (this->get_type().is_derived_from<T>())
105  return_p = reinterpret_cast<const T*>(this);
106  assert(return_p != nullptr);
107  return *return_p;
108  }
109 
110 
111  template<typename T>
113  {
114  T* cast_interface = rtti_cast<T>(this);
115  assert(cast_interface != nullptr);
116  return *cast_interface;
117  }
118 
119 }
nap::WebSocketInterface::mEventMutex
std::mutex mEventMutex
Definition: websocketinterface.h:78
nap::WebSocketInterface::mEvents
std::queue< WebSocketEventPtr > mEvents
Definition: websocketinterface.h:75
nap::WebSocketService
Definition: websocketservice.h:23
nap::utility::ErrorState
Definition: errorstate.h:19
nap::WebSocketEventPtr
std::unique_ptr< nap::WebSocketEvent > WebSocketEventPtr
Definition: websocketevent.h:110
nap
Definition: templateapp.h:17
nap::WebSocketInterface
Definition: websocketinterface.h:28
nap::Resource
Definition: resource.h:19
nap::WebSocketInterface::as
T & as()
Definition: websocketinterface.h:112