AGXDynamics  2.31.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CoSimulationBase.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2021. 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 #ifndef AGXNET_COSIMULATIONBASE_H
18 #define AGXNET_COSIMULATIONBASE_H
19 
20 #ifdef _MSC_VER
21 # pragma warning( disable : 4290)
22 # pragma warning( disable: 4275 ) // warning C4275: non dll-interface class
23 #endif
24 
25 #include <agx/agxPhysics_export.h>
26 #include <agx/Thread.h>
27 #include <agxNet/Compress.h>
28 #include <agxNet/Socket.h>
29 #include <agx/Referenced.h>
30 
34 namespace agxNet
35 {
36 
39  {
40  public:
41 
43  struct Header
44  {
45  Header() : numBytes(0), mode(0), compressed(false) {}
46  enum Mode
47  {
48  NOP = 0,
49 
57 
61 
62  SHUTDOWN
63  };
64 
65  uint32_t numBytes;
66  uint8_t mode;
67  bool compressed;
68  char md5[32];
69  };
70 
71  public:
72 
75 
77  virtual bool isConnected();
78 
79  virtual bool waitForConnection()=0;
80 
84  virtual void dropConnection();
85 
86  protected:
87 
94  size_t sendData( const uint8_t* data, size_t numBytes );
95 
102  size_t receiveData( uint8_t* data, size_t numBytes );
103 
104  virtual ~CoSimulationBase();
105 
106  protected:
107 
108  virtual void handleConnectionDrop();
109  void sendPacket(Header &header);
110  void receivePacket(Header &header);
111 
113 
117 
118 
119  private:
120 
121  };
122 
123 
124  template<typename T>
125  void insertBytes(agx::Vector<char>& container, size_t startIndex, const T& value)
126  {
127  ::memcpy(&container[startIndex], &value, sizeof(value));
128  }
129 
130 
131  template<typename T>
132  T extractBytes(const agx::Vector<char>& container, size_t startIndex)
133  {
134  T value;
135  ::memcpy(&value, &container[startIndex], sizeof(T));
136  return value;
137  }
138 
139 }
140 #endif
#define AGXPHYSICS_EXPORT
Base class for multi threaded transferring simulation between a server and a client using sockets...
agx::Vector< char > m_buffer
T extractBytes(const agx::Vector< char > &container, size_t startIndex)
void insertBytes(agx::Vector< char > &container, size_t startIndex, const T &value)
std::mutex Mutex
Definition: Referenced.h:93
Header used for sending data over sockets.