AGX Dynamics  2.37.2.0
WebSocketTrack.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #pragma once
18 
19 #include <agx/config/AGX_USE_WEBSOCKETS.h>
20 #include <agx/config.h>
21 #if AGX_USE_WEBSOCKETS()
22 
23 #include <agxNet/SocketTrack.h>
24 #include <agxNet/WebSocket.h>
25 
26 namespace agxNet
27 {
28 
29  AGX_DECLARE_POINTER_TYPES(WebSocketFrameWriter);
30  class AGXCORE_EXPORT WebSocketFrameWriter : public agxData::FrameWriter
31  {
32  public:
33  explicit WebSocketFrameWriter(agx::UInt16 port);
34  explicit WebSocketFrameWriter(agxNet::WebSocket* client);
35 
36  virtual void writeFrame(const agxData::Frame* frame);
37 
38  agx::UInt16 getPort() const;
39 
40  bool service(agx::Real timeoutMs = 0);
41 
42  protected:
43  virtual ~WebSocketFrameWriter() {}
44 
45  void clientConnectedCallback(agxNet::WebSocket* client);
46  void clientDisconnectedCallback(agxNet::WebSocket* client);
47  void messageReceivedCallback(agxNet::WebSocket *socket, agx::UInt8* payload, size_t size);
48 
49  private:
50  agxNet::WebSocketServerRef m_serverSocket;
51  agxNet::WebSocketPtrVector m_clientSockets;
52  agx::Mutex m_mutex;
53  };
54 
55 
56 
57  AGX_DECLARE_POINTER_TYPES(WebSocketFrameReader);
58  class AGXCORE_EXPORT WebSocketFrameReader : public agxData::FrameReader
59  {
60  public:
61  WebSocketFrameReader(agxNet::WebSocket* socket, bool active = false);
62 
63  void messageReceivedCallback(agxNet::WebSocket *socket, agx::UInt8* payload, size_t size);
64  void releaseBlock(agxNet::WebSocket*);
65 
66  virtual agxData::Frame* readFrame();
67 
68  protected:
69  virtual ~WebSocketFrameReader();
70 
71  private:
72  agxNet::WebSocket* m_socket;
73  agx::Block m_frameBlock;
74 
75  // Cannot use reference counting pointers here since 'readFrame' is required to
76  // return Frames by raw pointer, and a vector of reference counted pointers would
77  // then delete the Frames before they are passed to the caller.
78  agx::Vector<agxData::Frame*> m_storedFrames;
79  };
80 
81 
82  /* Implementation */
83  AGX_FORCE_INLINE agx::UInt16 WebSocketFrameWriter::getPort() const { return m_serverSocket->getPort(); }
84 
85 }
86 
87 
88 // AGX_USE_WEBSOCKETS
89 #endif
Containins classes for sending/reading data over sockets as well as compression functionality.
Definition: MacUtil.h:22
uint8_t UInt8
Definition: Integer.h:38
The FrameReader reads frames from an external source.
Definition: FrameIO.h:126
#define AGX_DECLARE_POINTER_TYPES(type)
Definition: Referenced.h:254
#define AGX_FORCE_INLINE
Definition: Object.h:59
Block synchronization primitive.
The FrameWriter writes frames to an external target.
Definition: FrameIO.h:168
Templated vector class.
Definition: Vector.h:52
std::mutex Mutex
Definition: Referenced.h:99
uint16_t UInt16
Definition: Integer.h:39
double Real
Definition: Real.h:42
#define AGXCORE_EXPORT