NAP
portalwebsocketserver.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 "portalevent.h"
9 
10 // External Includes
11 #include <websocketserver.h>
12 #include <queue>
13 #include <mutex>
14 
15 namespace nap
16 {
17  // Forward Declares
18  class PortalService;
19 
25  {
26  friend class PortalService;
27  RTTI_ENABLE(IWebSocketServer)
28  public:
34 
40  virtual bool init(utility::ErrorState& error) override;
41 
45  virtual void onDestroy() override;
46 
54  bool send(PortalEventPtr event, const WebSocketConnection& connection, utility::ErrorState& error);
55 
62  bool broadcast(PortalEventPtr event, utility::ErrorState& error);
63 
64  bool mSendWebSocketEvents = true;
65  bool mVerbose = true;
66 
67  private:
68 
69  // Called by web-socket server endpoint when a client connection opened
70  virtual void onConnectionOpened(const WebSocketConnection& connection) override;
71 
72  // Called by web-socket server endpoint when a client connection closed
73  virtual void onConnectionClosed(const WebSocketConnection& connection, int code, const std::string& reason) override;
74 
75  // Called by web-socket server endpoint when a client connection failed to extablish
76  virtual void onConnectionFailed(const WebSocketConnection& connection, int code, const std::string& reason) override;
77 
78  // Called by web-socket server endpoint when a new message is received
79  virtual void onMessageReceived(const WebSocketConnection& connection, const WebSocketMessage& message) override;
80 
85  void addPortalEvent(PortalEventPtr event);
86 
92  void consumePortalEvents(std::queue<PortalEventPtr>& outEvents);
93 
94  // Queue that holds all the received portal events
95  std::queue<PortalEventPtr> mPortalEvents;
96 
97  // Mutex associated with setting / getting portal events
98  std::mutex mPortalEventMutex;
99 
100  // Handle to the portal service
101  PortalService* mService;
102  };
103 
104  // Object creator used for constructing the portal WebSocket server
106 }
nap::WebSocketConnection
Definition: websocketconnection.h:27
nap::PortalEventPtr
std::unique_ptr< PortalEvent > PortalEventPtr
Definition: portalevent.h:128
nap::PortalService
Definition: portalservice.h:25
nap::rtti::ObjectCreator
Definition: factory.h:49
nap::utility::ErrorState
Definition: errorstate.h:19
nap::WebSocketMessage
Definition: websocketmessage.h:29
nap::PortalWebSocketServer
Definition: portalwebsocketserver.h:24
nap
Definition: templateapp.h:17
nap::IWebSocketServer
Definition: websocketserver.h:22