gwnavruntime/visualdebug/amp/Amp_Message.h Source File

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