gwnavruntime/visualdebug/visualdebugclient.h Source File

visualdebugclient.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 #pragma once
8 
13 
14 #if defined(KY_CONFIG_RELEASE) || !defined(KY_ENABLE_SOCKETS)
15 # define KY_DISABLE_VISUALDEBUGCLIENT
16 #endif
17 
18 #ifndef KY_DISABLE_VISUALDEBUGCLIENT
19 
27 
28 namespace Kaim
29 {
30 
31 class FileOpenerBase;
32 class File;
33 
34 class IQuery;
35 class MessageBlob;
36 class MessageAggregatedBlob;
37 class SocketDispatcherFactory;
38 class ClientStatusChangedCallback;
39 class AcknowledgeConnectionMessageReceiver;
40 
41 class VisualDebugClientScopedFileWriter;
42 
43 
44 class MessagePortHandler;
45 
55 class VisualDebugClient
56 {
57  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_VisualDebug)
58 public:
60  enum RunMode
61  {
62  RunSynchronously = 0,
63  RunAsynchronously = 1
64  };
65 
66 
67  // ------------------ Main API ------------------
68 
69  VisualDebugClient();
70  ~VisualDebugClient();
71 
74  static KyUInt32 GetInfiniteWaitDelay() { return KY_WAIT_INFINITE; }
75 
77  KyResult SetupReceiverRegistry(MessageReceiverRegistry& receiverRegistry, VisualDebugUnknownBlobTypeHandling unknownBlobTypeHandling = VisualDebugUnknownBlobTypeHandling::Log);
78 
79 
80  // ------------------ Server Connection Mode ------------------
81 
87  KyResult ConnectToServer(const char* ipAddress, KyUInt32 port, RunMode runMode = RunSynchronously);
88 
90  void CloseConnection();
91 
93  bool IsConnected() const;
94 
96  bool IsConnectionEstablished() const;
97 
99  bool IsReceptionPipelineEmpty()
100  { return (m_socketThreadMgr ? m_socketThreadMgr->IsReceptionPipelineEmpty() : true); }
101 
104  KyResult Update();
105 
106 
107  template <class T>
108  KyResult Send(BaseBlobBuilder<T>& blobBuilder)
109  {
110  if (m_socketThreadMgr)
111  {
112  blobBuilder.m_heap = GetHeap();
113  Ptr<BaseBlobHandler> blobHandler = *KY_HEAP_NEW(blobBuilder.m_heap) BlobHandler<T>;
114  blobBuilder.Build((BlobHandler<T>&)*blobHandler);
115  return this->CreateMessageAndSend(blobHandler);
116  }
117  return KY_ERROR;
118  }
119 
122  KyResult SendEndOfSynchronization();
123 
124  KyResult Send(Kaim::BaseBlobHandler& blobHandler);
125 
128  KyResult SendFlatBlob(KyUInt32 blobtypeId, KyUInt32 size, char* blob);
129 
130  // Used to send queries to be exectued remotely
131  KyResult Send(IQuery* query);
132 
133  // Wait until connection is established, or until the specified time interval has passed.
135  bool WaitWhileEstablishingConnection(KyUInt32 maxDelayMilliseconds);
136 
137 
138  // ------------------ Broadcast Mode ------------------
139  void StartListeningForAvailableServers(KyUInt32 port, RunMode runMode = RunAsynchronously);
140  void ListenForAvailableServers();
141  void GetAvailableServers(KyArray<Ptr<Net::AMP::MessagePort> >& availableServers);
142  void StopListeningForAvailableServers();
143 
144  // ------------------ File Modes ------------------
145  KyResult ReadFromFile(const char* readFromFile, Kaim::FileOpenerBase* fileOpener, RunMode runMode = RunSynchronously);
146  KyResult WriteToFile(const char* writeToFile, Kaim::FileOpenerBase* fileOpener, VisualDebugClientScopedFileWriter & writer, RunMode runMode = RunSynchronously);
147 
148 private:
149  void SetupMessageRegistryForServerPorts(Net::AMP::MessageTypeRegistry& customMsgTypeRegistry);
150  void SetupMessageRegistryForBlobs(Net::AMP::MessageTypeRegistry& customMsgTypeRegistry);
151  void CreateManager(SocketDispatchMode mode, Net::AMP::MessageTypeRegistry& customMsgTypeRegistry, bool initSocketConnection);
152  void DestroyManager();
153 
154  KyResult OnConnection(); //< called from the socketThread when conneciton is established
155 
156  KyResult CreateMessageAndSend(Ptr<BaseBlobHandler> handlerPtr);
157  KyResult CreateMessageAndSend(KyUInt32 blobtypeId, KyUInt32 shallowBlobSize, KyUInt32 deepBlobSize, char* blob);
158  KyResult SendMessage(MessageBlob* msg);
159 
160  Ptr<Net::AMP::Message> CreateAndReadMessage(File& stream);
161 
162  void HandleAllReceivedMessages();
163  void Handle(Ptr<Net::AMP::Message> msg);
164 
165 public: //internal
166  Net::AMP::ThreadMgr* GetThreadMgr() { return m_socketThreadMgr.GetPtr(); }
167  const Net::AMP::ThreadMgr* GetThreadMgr() const { return m_socketThreadMgr.GetPtr(); }
168  MemoryHeap* GetHeap() { return Memory::GetHeapByAddress(m_socketThreadMgr.GetPtr()); }
169 
170  void WaitForEmptiedSendQueue();
171 
172  RunMode GetRunMode() const { return m_runMode; }
173 
174  File* GetFile() { return m_file; }
175 
176 private:
177  static const KyUInt32 NotConnected = 0;
178  std::atomic<KyUInt32> m_wasConnected; //used to check the connection.
179  bool m_isSynchronized;
180 
181  KyArray<char> m_receiveBuffer;
182 
183  Kaim::Ptr<Net::AMP::ThreadMgr> m_socketThreadMgr;
184  Kaim::Ptr<Net::AMP::DiscardMessageHandler> m_msgDiscarder;
185  Kaim::Ptr<MessagePortHandler> m_receivedMsgPort;
186  Kaim::Ptr<MessageBlobHandler> m_msgBlobHandler;
187  Kaim::Ptr<MessageAggregatedBlobHandler> m_msgAggregatedBlobHandler;
188  Kaim::SocketDispatcherFactory* m_socketFactory;
189  Kaim::Ptr<File> m_file;
190 
191  // Callback for connection status change
192  friend class ClientStatusChangedCallback;
193  Kaim::Ptr<ClientStatusChangedCallback> m_statusCallback;
194  Kaim::Event m_connectedEvent;
195 
196  RunMode m_runMode;
197 };
198 
199 class VisualDebugClientScopedFileWriter
200 {
201  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_VisualDebug)
202 public:
203  VisualDebugClientScopedFileWriter();
204  ~VisualDebugClientScopedFileWriter();
205 
206  KyResult Write(char* blob, KyUInt32 blobTypeId, KyUInt32 deepBlobSize);
207  KyResult WriteFrame(char* blob, KyUInt32 blobTypeId, KyUInt32 deepBlobSize); // call WriteFrame to end the frame previous Write belonged to
208 
209 private:
210  KyResult CreateAggregateMessage(KyUInt32 deepBlobSize);
211  KyResult WriteMessageToFile(Net::AMP::Message*& message);
212  KyResult WriteToFile();
213 
214 public: //internal
215  void Init(VisualDebugClient* visualDebugClient, VisualDebugClient::RunMode runMode);
216 
217 private:
218  VisualDebugClient* m_visualDebugClient;
219  VisualDebugClient::RunMode m_runMode;
220  MessageAggregatedBlob* m_aggregatedBlobMsg;
221 };
222 
223 
224 
225 } // namespace Kaim
226 
227 #else
228 
229 // Only keep symbols that could be used as parameters.
230 // VisualDebugClient is not expected to be used in Release.
231 namespace Kaim
232 {
233 
234 class VisualDebugClient
235 {
236  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_VisualDebug)
237 public:
238  enum RunMode
239  {
240  RunSynchronously = 0,
241  RunAsynchronously = 1
242  };
243 
244  static KyUInt32 GetInfiniteWaitDelay() { return KY_WAIT_INFINITE; }
245 };
246 
247 }
248 
249 #endif
250 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KY_ERROR
use result == KY_ERROR to test for error
Definition: types.h:132