gwnavruntime/visualdebug/amp/Amp_Message.h Source File

Amp_Message.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 /**************************************************************************
8 
9 PublicHeader: AMP
10 Filename : Amp_Message.h
11 Content : Messages sent back and forth to AMP
12 Created : December 2009
13 Authors : Alex Mantzaris
14 
15 **************************************************************************/
16 
17 // This file includes a number of different classes
18 // All of them derive from GFxMessage and represent the types of messages
19 // sent back and forth between AMP and the GFx application
20 
21 #pragma once
22 
28 
29 
30 namespace Kaim {
31  class File;
32 namespace Net {
33 namespace AMP {
34 
35 
36 class RawData
37 {
38 public:
39  void Read(Kaim::File& str);
40  void Write(Kaim::File& str) const;
41 
42  Kaim::ArrayLH_POD<Kaim::UByte, MemStat_VisualDebugMessage> Data;
43 };
44 
45 // Generic message that includes only the type
46 // Although not abstract, this class is intended as a base class for concrete messages
47 class Message : public RefCountBase<Message, MemStat_VisualDebugMessage>, public ListNode<Message>
48 {
49 public:
50  static const UInt32 InvalidVersion = KY_MAX_UINT32;
51  static const UInt32 InvalidMsgType = KY_MAX_UINT32;
52 
53  virtual ~Message() { }
54 
55  // Message serialization methods
56  virtual void Read(File& str);
57  virtual void Write(File& str) const;
58 
59  // Message printing for debugging
60  virtual String ToString() const
61  {
62  char str[32];
63  Kaim::SFsprintf(str, 32, "%s-%u", GetMessageTypeName(), GetMessageType());
64  return str;
65  }
66 
67  // Type comparison for queue filtering
68  bool IsSameType(const Message& msg) const { return MsgType == msg.MsgType; }
69  UInt32 GetMessageType() const { return MsgType; }
70  void SetMessageType(UInt32 msgType)
71  {
72  KY_DEBUG_ASSERTN(msgType!=Message::InvalidMsgType, ("setting an invalid message type, check the registry"));
73  MsgType = msgType;
74  } //< should be set accordingly to a MessageTypeRegistry (done automatically by the AmpThreadMgr when sending and receiving)
75  virtual const char* GetMessageTypeName() const { return "Message Base"; }
76 
77  // version for backwards-compatibility
78  void SetVersion(UInt32 version) { Version = version; } //< When sending a message, the version is already set. This is useful when receiving it
79  UInt32 GetVersion() const { return Version; }
80 
81 
82  // Compression
83  virtual Message* Compress() const;
84  virtual Message* Uncompress() { return NULL; }
85 
86  // IP address information
87  virtual void SetPeerName(const char* name) { KY_UNUSED(name); }
88  virtual void SetPeerAddress(UInt32 address) { KY_UNUSED(address); }
89 
90 
91  // Version for backwards compatibility
92  static UInt32 GetLatestVersion() { return Version_Latest; }
93 
94  // Helper read/write methods
95  static void ReadString(File& inFile, String* str);
96  static void WriteString(File& outFile, const String& str);
97 
98 protected:
99 
100  UInt32 MsgType; // for serialization
101  UInt32 Version;
102 
103  // Protected constructor because Message is intended to be used as a base class only
104  Message(): MsgType(Message::InvalidMsgType), Version(InvalidVersion) {}
105 
106  enum VersionType
107  {
108  Version_Latest = 0,
109  };
110 };
111 
112 // MessageSetup
113 // Both peers send this message on connection
114 class MessageSetup : public Message
115 {
116 public:
117  static const char* GetTypeName() { return "MessageSetup"; }
118  static UInt32 GetLatestVersion() { return Message::GetLatestVersion(); }
119 
120  MessageSetup();
121  virtual ~MessageSetup() { }
122 
123  virtual const char* GetMessageTypeName() const { return MessageSetup::GetTypeName(); }
124 
125  // Message serialization methods
126  virtual void Read(File& str);
127  virtual void Write(File& str) const;
128 
129  // Message printing for debugging
130  virtual String ToString() const
131  {
132  String str = Kaim::Net::AMP::Message::ToString();
133  char cstr[32];
134  Kaim::SFsprintf(cstr, 32, "(heartbeatInterval:%u)", HeartbeatInterval);
135  str += cstr;
136  return str;
137  }
138 
139  virtual Message* Compress() const { return NULL; } //no need to compress such a message
140  virtual Message* Uncompress() { return NULL; }
141 
142  // HeartbeatInterval is compared to the local value in the ThreadMgr, and on reception the biggest value is kept, ensuring that the connection is maintained the same way on both ends.
143  UInt32 HeartbeatInterval;
144 };
145 
146 // Heartbeat message
147 // Used for connection verification
148 // A lack of received messages for a long time period signifies lost connection
149 class MessageHeartbeat : public Message
150 {
151 public:
152  static const char* GetTypeName() { return "MessageHeartbeat"; }
153  static UInt32 GetLatestVersion() { return Message::GetLatestVersion(); }
154 
155  MessageHeartbeat();
156  virtual ~MessageHeartbeat() { }
157 
158  virtual const char* GetMessageTypeName() const { return MessageHeartbeat::GetTypeName(); }
159 
160  virtual Message* Compress() const { return NULL; } //no need to compress such a message
161  virtual Message* Uncompress() { return NULL; }
162 };
163 
164 // Used to signify that the connection is explicitly ended by the remote peer
165 class MessageDisconnection : public Message
166 {
167 public:
168  static const char* GetTypeName() { return "MessageDisconnection"; }
169  static UInt32 GetLatestVersion() { return Message::GetLatestVersion(); }
170 
171  MessageDisconnection();
172  virtual ~MessageDisconnection() { }
173 
174  virtual const char* GetMessageTypeName() const { return MessageDisconnection::GetTypeName(); }
175 
176  virtual Message* Compress() const { return NULL; } //no need to compress such a message
177  virtual Message* Uncompress() { return NULL; }
178 };
179 
180 
181 // Log message
182 // Sent from server to client
183 class MessageLog : public Message
184 {
185 public:
186  static const char* GetTypeName() { return "MessageLog"; }
187  static UInt32 GetLatestVersion() { return Message::GetLatestVersion(); }
188 
189  MessageLog(const String& logText = "", UInt32 logCategory = 0, const UInt64 timeStamp = 0);
190  virtual ~MessageLog() { }
191  void SetLog(const String& logText = "", UInt32 logCategory = 0, const UInt64 timeStamp = 0);
192 
193  // Message overrides
194  virtual void Read(File& str);
195  virtual void Write(File& str) const;
196  virtual const char* GetMessageTypeName() const { return MessageLog::GetTypeName(); }
197 
198  // Accessors
199  UInt32 GetLogCategory() const;
200  const StringLH& GetTimeStamp() const;
201  const StringLH& GetLogText() const { return LogText; }
202 
203 
204 private:
205  void SetTimeStamp(const UInt64 timeStamp);
206 
207  StringLH LogText;
208  UInt32 LogCategory;
209  StringLH TimeStamp;
210 };
211 
212 
213 // Message containing the server listening port
214 // Broadcast via UDP
215 // The server IP address is known from the origin of the UDP packet
216 class MessagePort : public Message
217 {
218 public:
219  static const char* GetTypeName() { return "MessagePort"; }
220  static UInt32 GetLatestVersion() { return Message::GetLatestVersion(); }
221 
222  enum PlatformType
223  {
224  PlatformOther = 0,
225 
226  PlatformWindows,
227  PlatformMac,
228  PlatformLinux,
229 
230  PlatformXbox360,
231  PlatformPs3,
232  PlatformWii,
233  Platform3DS,
234 
235  PlatformAndroid,
236  PlatformIphone,
237 
238  PlatformNgp,
239  PlatformWiiU
240  };
241 
242  MessagePort();
243  virtual ~MessagePort() { }
244 
245  // Message overrides
246  virtual void Read(File& str);
247  virtual void Write(File& str) const;
248  virtual const char* GetMessageTypeName() const { return MessagePort::GetTypeName(); }
249 
250  // Accessors
251  UInt32 GetPort() const;
252  UInt32 GetPeerAddress() const;
253  const StringLH& GetPeerName() const;
254  PlatformType GetPlatform() const;
255  const char* GetPlatformName() const;
256  const StringLH& GetAppName() const;
257  const StringLH& GetFileName() const;
258  virtual void SetPeerName(const char* name);
259  virtual void SetPeerAddress(UInt32 address);
260  void SetPlatform(PlatformType platform);
261  void SetPort(UInt32 port);
262  void SetAppName(const char* appName);
263  void SetFileName(const char* fileName);
264 
265 protected:
266  UInt32 Port;
267  UInt32 Address;
268  StringLH PeerName;
269  PlatformType Platform;
270  StringLH AppName;
271  StringLH FileName;
272 };
273 
274 // Compressed Message
275 // Can be any other message type once uncompressed
276 class MessageCompressed : public Message
277 {
278 public:
279  static const char* GetTypeName() { return "MessageCompressed"; }
280  static UInt32 GetLatestVersion() { return Message::GetLatestVersion(); }
281  MessageCompressed();
282  ~MessageCompressed();
283 
284  // Message overrides
285  virtual const char* GetMessageTypeName() const { return MessageCompressed::GetTypeName(); }
286 
287  virtual void Read(File& str);
288  virtual void Write(File& str) const;
289 
290  virtual Message* Uncompress();
291 
292  // Accessors
293  void AddCompressedData(UByte* data, UPInt dataSize);
294  const RawData& GetCompressedData() const;
295 
296 private:
297  RawData CompressedData;
298 };
299 
300 
301 } // namespace AMP
302 } // namespace Net
303 } // namespace Kaim
304 
305 
std::uint8_t UByte
uint8_t
Definition: SF_Types.h:134
std::uint32_t UInt32
uint32_t
Definition: SF_Types.h:137
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
std::uint64_t UInt64
uint64_t
Definition: SF_Types.h:138