CTR Pia  4.11.3
Game Communication Engine
clone_ReliableCloneElementBase.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: clone_ReliableCloneElementBase.h
4 
5  Copyright Nintendo. All rights reserved.
6 
7  These coded instructions, statements, and computer programs contain
8  proprietary information of Nintendo of America Inc. and/or Nintendo
9  Company Ltd., and are protected by Federal copyright law. They may
10  not be disclosed to third parties or copied or duplicated in any form,
11  in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 
15 #pragma once
16 
17 #include <pia/clone/clone_definitions.h>
18 #include <pia/clone/clone_CloneElementBase.h>
19 
20 
21 namespace nn
22 {
23 namespace pia
24 {
25 namespace clone
26 {
27 
28 
29 class IDataPacker;
30 
31 /*!
32 @brief This is the base class for managing the sending and receiving of reliable data.
33 
34 @date 2013-07-18 Initial version.
35 */
37 {
38 protected:
39 /*!
40 @brief Instantiates the object with default parameters (default constructor).
41 */
43 
44 public:
45 /*!
46 @brief Destroys the object (destructor).
47 */
48  virtual ~ReliableCloneElementBase();
49 
50 
51 /*!
52 @brief Gets whether a valid value can be obtained using <tt>@ref ReliableCloneElement::GetValue "GetValue"</tt>.
53 @return Returns <tt>true</tt> if a valid value can be obtained using <tt>@ref ReliableCloneElement::GetValue "GetValue"</tt>.
54 */
55  bool IsValidValue() const
56  {
57  return m_SetterStationIndex != STATION_INDEX_INVALID;
58  }
59 
60 
61 /*!
62 @brief Gets the clock for when the value was set.
63 @return Returns the clock for when the value was set. This value is undefined when <tt>IsValidValue</tt> returns <tt>false</tt>.
64 */
66  {
67  return m_Clock;
68  }
69 
70 
71 /*!
72 @brief Prints information that is useful for debugging.
73 
74 @param[in] flag Specifies the bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
75 */
76  void Trace(u64 flag) const;
77 
78 
79 public:
80 /*!
81 @cond PRIVATE
82 @brief Gets the <tt>StationIndex</tt> that set the current value. (For debugging.)
83 */
84  StationIndex GetSetter() const
85  {
86  return m_SetterStationIndex;
87  }
88  //! @endcond
89 
90 /*!
91 @cond PRIVATE
92 @brief Gets the <tt>StationIndex</tt> responsible for sending the current value. (For debugging.)
93 */
94  StationIndex GetPreserver() const
95  {
96  return m_PreserverStationIndex;
97  }
98  //! @endcond
99 
100 /*!
101 @cond PRIVATE
102 @brief Gets the destination bitmap for the current value. (For debugging.)
103 */
104  u32 GetDestBitmap() const
105  {
106  return m_DestBitmap;
107  }
108  //! @endcond
109 
110  //! @cond PRIVATE
111 
112 public:
113  virtual u16 GetType() const
114  {
115  return GetTypeStatic();
116  }
117 
118  static u16 GetTypeStatic()
119  {
120  return TYPE_RELIABLE;
121  }
122  static bool Receive(CloneElementBase* pElement, IDataPacker* pAckPacker, const void* cpChunk, u16 chunkSize, Id elementId, StationIndex src, StationIndex localStationIndex, u32 dispatchCount);
123  static size_t GetDataChunkHeaderSize();
124 
125 private:
126  bool ReceiveRequest(StationIndex src);
127  bool ReceiveData(const void* cpData, size_t size, StationIndex src, StationIndex setter, ClockValue clock, u32 destBitmap);
128  void ReceiveNoData(StationIndex src);
129  void ReceivePreserverMigration(StationIndex src, StationIndex setter, ClockValue clock);
130  void ReceiveAck(StationIndex src, StationIndex setter, ClockValue clock);
131 
132 public:
133  virtual void ClearData();
134 
135  virtual void AddDest(StationIndex stationId);
136  virtual void RemoveDest(StationIndex stationId);
137  virtual void Complement(StationIndex stationId);
138 
139  virtual void RequestInitialData();
140 
141 private:
142  class ReliableSendToken : public CloneElementBase::ResendableSendToken
143  {
144  public:
145  enum TokenType
146  {
147  TOKEN_REQUEST,
148  TOKEN_DATA
149  };
150  virtual TokenType GetTokenType() const = 0;
151  };
152 
153 
154  class ReliableRequestToken : public ReliableSendToken
155  {
156  public:
157  explicit ReliableRequestToken(ReliableCloneElementBase* pElement)
158  : ReliableSendToken(), m_pElement(pElement)
159  {
160  }
161  virtual Type GetElementType() const
162  {
163  return TYPE_RELIABLE;
164  }
165  virtual u32 GetDestBitmap() const
166  {
167  return m_pElement->m_DestBitmap;
168  }
169  virtual TokenType GetTokenType() const
170  {
171  return TOKEN_REQUEST;
172  }
173  virtual size_t GetChunkSize() const;
174  virtual void WriteChunk(void* pChunkBuffer) const;
175  virtual CloneElementBase* GetElement()
176  {
177  return m_pElement;
178  }
179 
180  private:
181  Id GetId() const
182  {
183  return m_pElement->GetId();
184  }
185 
186  private:
187  ReliableCloneElementBase* m_pElement;
188  };
189 
190 
191  class ReliableDataToken : public ReliableSendToken
192  {
193  public:
194  explicit ReliableDataToken(ReliableCloneElementBase* pElement)
195  : ReliableSendToken(), m_pElement(pElement)
196  {
197  }
198  virtual Type GetElementType() const
199  {
200  return TYPE_RELIABLE;
201  }
202  virtual u32 GetDestBitmap() const
203  {
204  return m_pElement->m_DestBitmap;
205  }
206  virtual TokenType GetTokenType() const
207  {
208  return TOKEN_DATA;
209  }
210  virtual size_t GetChunkSize() const;
211  virtual void WriteChunk(void* pChunkBuffer) const;
212  virtual CloneElementBase* GetElement()
213  {
214  return m_pElement;
215  }
216  virtual bool IsUnicast() const
217  {
218  return true;
219  }
220 
221  private:
222  size_t GetSize() const
223  {
224  return m_pElement->GetSize();
225  }
226  Id GetId() const
227  {
228  return m_pElement->GetId();
229  }
230  ClockValue GetClock() const
231  {
232  return m_pElement->m_Clock;
233  }
234  StationIndex GetSetterStationIndex() const
235  {
236  return m_pElement->m_SetterStationIndex;
237  }
238  void Serialize(void* pBuffer) const
239  {
240  m_pElement->Serialize(pBuffer);
241  }
242 
243  private:
244  ReliableCloneElementBase* m_pElement;
245  };
246 
247 
248  class ReliableInitialToken : public CloneElementBase::ISendToken
249  {
250  public:
251  explicit ReliableInitialToken(ReliableCloneElementBase* pElement)
252  : ISendToken(), m_pElement(pElement)
253  {
254  }
255  virtual Type GetElementType() const
256  {
257  return TYPE_RELIABLE;
258  }
259  virtual u32 GetDestBitmap() const
260  {
261  return m_DestBitmap;
262  }
263  virtual size_t GetChunkSize() const;
264  virtual void WriteChunk(void* pChunkBuffer) const;
265  virtual CloneElementBase* GetElement()
266  {
267  return m_pElement;
268  }
269 
270  private:
271  size_t GetSize() const
272  {
273  return m_pElement->GetSize();
274  }
275  bool IsValidValue() const
276  {
277  PIA_ASSERT(m_pElement->IsValidValue());
278  return true;
279  }
280  Id GetId() const
281  {
282  return m_pElement->GetId();
283  }
284  ClockValue GetClock() const
285  {
286  return m_pElement->m_Clock;
287  }
288  StationIndex GetSetterStationIndex() const
289  {
290  return m_pElement->m_SetterStationIndex;
291  }
292  void Serialize(void* pBuffer) const
293  {
294  m_pElement->Serialize(pBuffer);
295  }
296 
297  public:
298  void AddDest(StationIndex stationId)
299  {
300  m_DestBitmap |= (0x1 << stationId);
301  }
302  void RemoveDest(StationIndex stationId)
303  {
304  m_DestBitmap &= (0x1 << stationId);
305  }
306  void ClearDest()
307  {
308  m_DestBitmap = 0;
309  }
310 
311  private:
312  ReliableCloneElementBase* m_pElement;
313  u32 m_DestBitmap;
314  };
315 
316 
317 protected:
318  nn::Result SetValueCore();
319 
320  virtual void Serialize(void* pBuffer) const = 0;
321  virtual void Deserialize(const void* cpData) = 0;
322  virtual void ClearValue() = 0;
323 
324 private:
325  StationIndex m_SetterStationIndex;
326  StationIndex m_PreserverStationIndex;
327  ClockValue m_Clock;
328  u32 m_DestBitmap;
329  ReliableRequestToken m_RequestToken;
330  ReliableDataToken m_DataToken;
331  ReliableInitialToken m_InitialToken;
332 
333  static Definition<ReliableCloneElementBase> s_Definition;
334 
335 public:
336  template <int TEST_ID, typename Arg>
337  static void Test(const ReliableCloneElementBase& obj, Arg* pArg = NULL);
338 
339  //! @endcond
340 };
341 }
342 }
343 } // end of namespace nn::pia::clone
This is the base class for managing data that is sent and received. .
Definition: clone_CloneElementBase.h:40
virtual ~ReliableCloneElementBase()
Destroys the object (destructor).
u32 ClockValue
Defines a type that holds a clock value.
Definition: clone_definitions.h:44
StationIndex
Enumerates StationIndex values.
Definition: platformCtr.h:44
Definition: assert.h:115
ID indicating a station that is not present in the session.
Definition: platformCtr.h:59
void Trace(u64 flag) const
Prints information that is useful for debugging.
bool IsValidValue() const
Gets whether a valid value can be obtained using GetValue.
Definition: clone_ReliableCloneElementBase.h:55
This is the base class for managing the sending and receiving of reliable data.
Definition: clone_ReliableCloneElementBase.h:36
ClockValue GetClock() const
Gets the clock for when the value was set.
Definition: clone_ReliableCloneElementBase.h:65
u16 Id
Specifies the type of the ID for identifying clone elements.
Definition: clone_CloneElementBase.h:59
Id GetId() const
Gets the ID.
Definition: clone_CloneElementBase.h:67