gwnavruntime/visualdebug/visualdebugclient.h Source File

visualdebugclient.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 // primary contact: BRGR - secondary contact: NOBODY
9 #ifndef Navigation_VisualDebugClient_H
10 #define Navigation_VisualDebugClient_H
11 
16 
17 #if defined(KY_BUILD_SHIPPING) || !defined(KY_ENABLE_SOCKETS) || !defined(KY_ENABLE_THREADS)
18 # define KY_DISABLE_VISUALDEBUGCLIENT
19 #endif
20 
21 #ifndef KY_DISABLE_VISUALDEBUGCLIENT
22 
23 
30 
31 
32 namespace Kaim
33 {
34 
35 
36 class FileOpenerBase;
37 class File;
38 
39 class IQuery;
40 class MessageBlob;
41 class MessageAggregatedBlob;
42 class SocketDispatcherFactory;
43 class ClientStatusChangedCallback;
44 class AcknowledgeConnectionMessageReceiver;
45 
46 class VisualDebugClientScopedFileWriter;
47 
48 
49 class MessagePortHandler;
50 
60 class VisualDebugClient
61 {
62  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_VisualDebug)
63 public:
65  enum RunMode
66  {
69  };
70 
71 
72  // ------------------ Main API ------------------
73 
76 
79  static KyUInt32 GetInfiniteWaitDelay() { return KY_WAIT_INFINITE; }
80 
82  KyResult SetupReceiverRegistry(MessageReceiverRegistry& receiverRegistry);
83 
84 
85  // ------------------ Server Connection Mode ------------------
86 
92  KyResult ConnectToServer(const char* ipAddress, KyUInt32 port, RunMode runMode = RunSynchronously);
93 
95  void CloseConnection();
96 
98  bool IsConnected() const;
99 
101  bool IsConnectionEstablished() const;
102 
105  { return (m_socketThreadMgr ? m_socketThreadMgr->IsReceptionPipelineEmpty() : true); }
106 
109  KyResult Update();
110 
111 
112  template <class T>
113  KyResult Send(BaseBlobBuilder<T>& blobBuilder)
114  {
115  if (m_socketThreadMgr)
116  {
117  blobBuilder.m_heap = GetHeap();
118  Ptr<BaseBlobHandler> blobHandler = *KY_HEAP_NEW(blobBuilder.m_heap) BlobHandler<T>;
119  blobBuilder.Build((BlobHandler<T>&)*blobHandler);
120  return this->CreateMessageAndSend(blobHandler);
121  }
122  return KY_ERROR;
123  }
124 
128 
129  KyResult Send(Kaim::BaseBlobHandler& blobHandler);
130 
133  KyResult SendFlatBlob(KyUInt32 blobtypeId, KyUInt32 size, char* blob);
134 
135  // Used to send queries to be exectued remotely
136  KyResult Send(IQuery* query);
137 
138  // Wait until connection is established, or until the specified time interval has passed.
140  bool WaitWhileEstablishingConnection(KyUInt32 maxDelayMilliseconds);
141 
142 
143  // ------------------ Broadcast Mode ------------------
145  void ListenForAvailableServers();
146  void GetAvailableServers(KyArray<Ptr<Net::AMP::MessagePort> >& availableServers);
147  void StopListeningForAvailableServers();
148 
149  // ------------------ File Modes ------------------
150  KyResult ReadFromFile(const char* readFromFile, Kaim::FileOpenerBase* fileOpener, RunMode runMode = RunSynchronously);
151  KyResult WriteToFile(const char* writeToFile, Kaim::FileOpenerBase* fileOpener, VisualDebugClientScopedFileWriter & writer, RunMode runMode = RunSynchronously);
152 
153 private:
154  void SetupMessageRegistryForServerPorts(Net::AMP::MessageTypeRegistry& customMsgTypeRegistry);
155  void SetupMessageRegistryForBlobs(Net::AMP::MessageTypeRegistry& customMsgTypeRegistry);
156  void CreateManager(SocketDispatchMode mode, Net::AMP::MessageTypeRegistry& customMsgTypeRegistry, bool initSocketConnection);
157  void DestroyManager();
158 
159  KyResult OnConnection(); //< called from the socketThread when conneciton is established
160 
161  KyResult CreateMessageAndSend(Ptr<BaseBlobHandler> handlerPtr);
162  KyResult CreateMessageAndSend(KyUInt32 blobtypeId, KyUInt32 shallowBlobSize, KyUInt32 deepBlobSize, char* blob);
163  KyResult SendMessage(MessageBlob* msg);
164 
165  Ptr<Net::AMP::Message> CreateAndReadMessage(File& stream);
166 
167  void HandleAllReceivedMessages();
168  void Handle(Ptr<Net::AMP::Message> msg);
169 
170 public: //internal
171  Net::AMP::ThreadMgr* GetThreadMgr() { return m_socketThreadMgr.GetPtr(); }
172  const Net::AMP::ThreadMgr* GetThreadMgr() const { return m_socketThreadMgr.GetPtr(); }
173  MemoryHeap* GetHeap() { return Memory::GetHeapByAddress(m_socketThreadMgr.GetPtr()); }
174 
175  void WaitForEmptiedSendQueue();
176 
177  RunMode GetRunMode() const { return m_runMode; }
178 
179  File* GetFile() { return m_file; }
180 
181 private:
182  static const KyUInt32 NotConnected = 0;
183  Kaim::AtomicInt<KyUInt32> m_wasConnected; //used to check the connection.
184  bool m_isSynchronized;
185 
186  KyArray<char> m_receiveBuffer;
187 
188  Kaim::Ptr<Net::AMP::ThreadMgr> m_socketThreadMgr;
189  Kaim::Ptr<Net::AMP::DiscardMessageHandler> m_msgDiscarder;
190  Kaim::Ptr<MessagePortHandler> m_receivedMsgPort;
191  Kaim::Ptr<MessageBlobHandler> m_msgBlobHandler;
192  Kaim::Ptr<MessageAggregatedBlobHandler> m_msgAggregatedBlobHandler;
193  Kaim::SocketDispatcherFactory* m_socketFactory;
194  Kaim::Ptr<File> m_file;
195 
196  // Callback for connection status change
197  friend class ClientStatusChangedCallback;
198  Kaim::Ptr<ClientStatusChangedCallback> m_statusCallback;
199  Kaim::Event m_connectedEvent;
200 
201  RunMode m_runMode;
202 };
203 
204 
205 
206 class VisualDebugClientScopedFileWriter
207 {
208  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_VisualDebug)
209 public:
210  VisualDebugClientScopedFileWriter();
211  ~VisualDebugClientScopedFileWriter();
212 
213  KyResult Write(char* blob, KyUInt32 blobTypeId, KyUInt32 deepBlobSize);
214  KyResult WriteFrame(char* blob, KyUInt32 blobTypeId, KyUInt32 deepBlobSize); // call WriteFrame to end the frame previous Write belonged to
215 
216 private:
217  KyResult CreateAggregateMessage(KyUInt32 deepBlobSize);
218  KyResult WriteMessageToFile(Net::AMP::Message*& message);
219  KyResult WriteToFile();
220 
221 public: //internal
222  void Init(VisualDebugClient* visualDebugClient, VisualDebugClient::RunMode runMode);
223 
224 private:
225  VisualDebugClient* m_visualDebugClient;
226  VisualDebugClient::RunMode m_runMode;
227  MessageAggregatedBlob* m_aggregatedBlobMsg;
228 };
229 
230 
231 
232 } // namespace Kaim
233 
234 #else
235 
236 // Only keep symbols that could be used as parameters.
237 // VisualDebugClient is not expected to be used in Shipping.
238 namespace Kaim
239 {
240 
241 class VisualDebugClient
242 {
243  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_VisualDebug)
244 public:
245  enum RunMode
246  {
247  RunSynchronously = 0,
248  RunAsynchronously = 1
249  };
250 
251  static KyUInt32 GetInfiniteWaitDelay() { return KY_WAIT_INFINITE; }
252 };
253 
254 }
255 
256 #endif
257 
258 #endif // Navigation_VisualDebugClient_H
RunMode
Enumerated the running mode of the VisualDebugClient.
Definition: visualdebugclient.h:67
Base interface for a class that opens a file on disk.
Definition: fileopener.h:35
The VisualDebugClient class manages the retrieval of data from a VisualDebugServer running in the gam...
Definition: visualdebugclient.h:61
void CloseConnection()
Closes the current network socket connection, if any.
static KyUInt32 GetInfiniteWaitDelay()
Defines a special value that can be passed to WaitWhileEstablishingConnection to block execution in t...
Definition: visualdebugclient.h:85
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
KyResult ConnectToServer(const char *ipAddress, KyUInt32 port, RunMode runMode=RunSynchronously)
Initiates a new connection to the VisualDebugServer listening on the specified host and port...
Operations are done in the main thread.
Definition: visualdebugclient.h:69
General purpose array for movable objects that require explicit construction/destruction.
Definition: kyarray.h:118
KyResult SendFlatBlob(KyUInt32 blobtypeId, KyUInt32 size, char *blob)
Sends a flat blob (with no Array inside).
The BlobHandler class is a top-level mechanism for serializing blobs between objects in memory and fi...
Definition: blobhandler.h:45
Definition: gamekitcrowddispersion.h:20
#define KY_ERROR
Shorthand for Kaim::Result::Failure.
Definition: types.h:272
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
bool WaitWhileEstablishingConnection(KyUInt32 maxDelayMilliseconds)
For internal use.
void StartListeningForAvailableServers(KyUInt32 port, RunMode runMode=RunAsynchronously)
RunAsynchronously is highly recommended to run this in a seperated thread because listening for Wii U...
BaseBlobBuilder is an abstract base class that builds a blob within a contiguous block of memory...
Definition: baseblobbuilder.h:30
Operations are made in separate threads.
Definition: visualdebugclient.h:70
KyResult SendEndOfSynchronization()
Must be send after having sent or received data for synchronization.
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
KyResult SetupReceiverRegistry(MessageReceiverRegistry &receiverRegistry)
Setup the internal receiver registry.
bool IsReceptionPipelineEmpty()
Check if everything was received.
Definition: visualdebugclient.h:114
bool IsConnected() const
Indicates whether the VisualDebugClient is connected and SendEndOfSynchronization was called...
KyResult Update()
Receives all messages since the last call to Update(), and processes them by calling the receivers se...
Abstract class for all queries.
Definition: iquery.h:133
T * Build(BlobHandler< T > &blobHandler)
This method:Calls DoBuild() in COUNT mode to determine the amount of memory needed for the blob to be...
Definition: baseblobbuilder.h:215
bool IsConnectionEstablished() const
Indicates whether ThreadMgr has established a connection.