CTR Pia  4.11.3
Game Communication Engine
clone_ReliableLargeCloneElementBase.h
1 /*---------------------------------------------------------------------------*
2  Project: Pia
3  File: clone_ReliableLargeCloneElementBase.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 enum ReliableLargeChunkType;
31 
32 
33 /*!
34  @brief The base class for managing the sending and receiving of reliable large-sized data.
35 
36  @date 2014-10-08 Initial version.
37 */
39 {
40 public:
41 /*!
42  @brief Destructor.
43 */
45 
46 
47 /*!
48  @brief Gets whether a valid value can be obtained using <tt>@ref ReliableCloneElement::GetValue "GetValue"</tt>.
49  @return Returns <tt>true</tt> if a valid value can be obtained using <tt>@ref ReliableCloneElement::GetValue "GetValue"</tt>.
50 */
51  bool IsValidValue() const
52  {
53  return m_ValueSetterStationIndex != STATION_INDEX_INVALID;
54  }
55 
56 
57 /*!
58  @brief Gets the clock for when the value was set.
59  @return Returns the clock for when the value was set. This value is undefined when <tt>IsValidValue</tt> returns <tt>false</tt>.
60 */
62  {
63  return m_ValueClock;
64  }
65 
66 
67 /*!
68  @brief Prints information that is useful for debugging.
69 
70  @param[in] flag Specifies the bitwise OR of trace flags. For more information, see the <tt>@ref TraceFlag</tt> type.
71 */
72  void Trace(u64 flag) const;
73 
74 
75 public:
76 /*!
77  @cond PRIVATE
78  @brief Gets the <tt>StationIndex</tt> that set the current value. (For debugging.)
79 */
80  StationIndex GetSetter() const
81  {
82  return m_ValueSetterStationIndex;
83  }
84  //! @endcond
85 
86 /*!
87  @cond PRIVATE
88  @brief Gets the <tt>StationIndex</tt> responsible for sending the current value. (For debugging.)
89 */
90  StationIndex GetPreserver() const
91  {
92  return m_ValuePreserverStationIndex;
93  }
94  //! @endcond
95 
96 /*!
97  @cond PRIVATE
98  @brief Gets the destination bitmap for the current value. (For debugging.)
99 */
100  u32 GetDestBitmap(u32 divideIndex) const
101  {
102  return (divideIndex < m_DivideNum) ? m_aDivideInfo[divideIndex].m_ValueDestBitmap : 0;
103  }
104  //! @endcond
105 
106  //! @cond PRIVATE
107 
108 public:
109  virtual u16 GetType() const
110  {
111  return GetTypeStatic();
112  }
113 
114  static u16 GetTypeStatic()
115  {
116  return TYPE_RELIABLE_LARGE;
117  }
118  static bool Receive(CloneElementBase* pElement, IDataPacker* pAckPacker, const void* cpChunk, u16 chunkSize, Id elementId, StationIndex src, StationIndex localStationIndex, u32 dispatchCount);
119  static size_t GetDataChunkHeaderSize();
120 
121 private:
122  bool ReceiveRequest(StationIndex src, u16 divideIndexOffset, u32 divideIndexFlag);
123  ReliableLargeChunkType ReceiveData(const void* cpData, size_t size, StationIndex src, StationIndex setter, ClockValue clock, u16 divideIndex, u32 destBitmap);
124  void ReceiveNoData(StationIndex src);
125  void ReceivePreserverMigration(StationIndex src, StationIndex setter, ClockValue clock);
126  void ReceiveAck(StationIndex src, StationIndex setter, ClockValue clock, u16 divideIndex);
127 
128 public:
129  virtual void ClearData();
130 
131  virtual void AddDest(StationIndex stationIndex);
132  virtual void RemoveDest(StationIndex stationIndex);
133  virtual void Complement(StationIndex stationIndex);
134 
135  virtual void RequestInitialData();
136 
137 private:
138  class ReliableLargeSendToken : public CloneElementBase::ResendableSendToken
139  {
140  public:
141  enum TokenType
142  {
143  TOKEN_REQUEST,
144  TOKEN_DATA
145  };
146  virtual TokenType GetTokenType() const = 0;
147  };
148 
149  class ReliableLargeRequestToken : public ReliableLargeSendToken
150  {
151  public:
152  explicit ReliableLargeRequestToken(ReliableLargeCloneElementBase* pElement)
153  : ReliableLargeSendToken(), m_pElement(pElement), m_DestBitmap(0)
154  {
155  }
156  virtual Type GetElementType() const
157  {
158  return TYPE_RELIABLE_LARGE;
159  }
160  virtual u32 GetDestBitmap() const
161  {
162  return m_DestBitmap;
163  }
164  virtual TokenType GetTokenType() const
165  {
166  return TOKEN_REQUEST;
167  }
168  virtual size_t GetChunkSize() const;
169  virtual void WriteChunk(void* pChunkBuffer) const;
170  virtual CloneElementBase* GetElement()
171  {
172  return m_pElement;
173  }
174 
175  public:
176  void SetDest(u32 destBitmap) {m_DestBitmap = destBitmap;}
177 
178  private:
179  Id GetId() const
180  {
181  return m_pElement->GetId();
182  }
183 
184  private:
185  ReliableLargeCloneElementBase* m_pElement;
186  u32 m_DestBitmap;
187  };
188 
189 protected:
190  class ReliableLargeDataToken : public ReliableLargeSendToken
191  {
192  public:
193  explicit ReliableLargeDataToken() : ReliableLargeSendToken() {}
194  void Init(ReliableLargeCloneElementBase* pElement, u32 divideIndex)
195  {
196  m_pElement = pElement;
197  m_DivideIndex = divideIndex;
198  }
199  virtual Type GetElementType() const
200  {
201  return TYPE_RELIABLE_LARGE;
202  }
203  virtual u32 GetDestBitmap() const
204  {
205  return m_pElement->m_aDivideInfo[m_DivideIndex].m_SerializedDestBitmap;
206  }
207  virtual TokenType GetTokenType() const
208  {
209  return TOKEN_DATA;
210  }
211  virtual size_t GetChunkSize() const;
212  virtual void WriteChunk(void* pChunkBuffer) const;
213  virtual CloneElementBase* GetElement()
214  {
215  return m_pElement;
216  }
217  virtual bool IsUnicast() const
218  {
219  return true;
220  }
221 
222  private:
223  size_t GetSize() const
224  {
225  return (m_DivideIndex < m_pElement->m_DivideNum - 1) ? m_pElement->m_DivideSize : m_pElement->m_DivideLastSize;
226  }
227  Id GetId() const
228  {
229  return m_pElement->GetId();
230  }
231  ClockValue GetClock() const
232  {
233  return m_pElement->m_SerializedClock;
234  }
235  StationIndex GetSetterStationIndex() const
236  {
237  return m_pElement->m_SerializedSetterStationIndex;
238  }
239 
240  private:
241  ReliableLargeCloneElementBase* m_pElement;
242  u32 m_DivideIndex;
243  };
244 
245 
246  class ReliableLargeInitialToken : public CloneElementBase::ISendToken
247  {
248  public:
249  explicit ReliableLargeInitialToken() : ISendToken() {}
250  void Init(ReliableLargeCloneElementBase* pElement, u32 divideIndex)
251  {
252  m_pElement = pElement;
253  m_DivideIndex = divideIndex;
254  }
255  virtual Type GetElementType() const
256  {
257  return TYPE_RELIABLE_LARGE;
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_DivideIndex < m_pElement->m_DivideNum - 1) ? m_pElement->m_DivideSize : m_pElement->m_DivideLastSize;
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_SerializedClock;
287  }
288  StationIndex GetSetterStationIndex() const
289  {
290  return m_pElement->m_SerializedSetterStationIndex;
291  }
292 
293  public:
294  void AddDest(StationIndex stationId)
295  {
296  m_DestBitmap |= (0x1 << stationId);
297  }
298  void RemoveDest(StationIndex stationId)
299  {
300  m_DestBitmap &= (0x1 << stationId);
301  }
302  void ClearDest()
303  {
304  m_DestBitmap = 0;
305  }
306 
307  private:
308  ReliableLargeCloneElementBase* m_pElement;
309  u32 m_DivideIndex;
310  u32 m_DestBitmap;
311  };
312 
313 protected:
314  struct DivideInfo
315  {
316  public:
317  DivideInfo() : m_ValueDestBitmap(0), m_SerializedDestBitmap(0), m_SerializedIsValid(false), m_DataToken(), m_InitialToken() {}
318 
319  u32 m_ValueDestBitmap;
320  u32 m_SerializedDestBitmap;
321  bool m_SerializedIsValid;
322  ReliableLargeDataToken m_DataToken;
323  ReliableLargeInitialToken m_InitialToken;
324  };
325 
326 
327 protected:
328 
329  ReliableLargeCloneElementBase(u8* pBuffer, DivideInfo* aDivideInfo, size_t serializedSize, size_t divideSize, u32 divideNum);
330 
331  virtual size_t GetSize() const {return m_DivideSize;}
332  bool IsEqualValueAndSerialized() const
333  {
334  return m_ValueClock == m_SerializedClock && m_ValueSetterStationIndex == m_SerializedSetterStationIndex;
335  }
336  void GetRequestDivideIndex(u16* pOffset, u32* pFlag);
337 
338 protected:
339  nn::Result SetValueCore();
340  void PostSetValue();
341  virtual void Serialize() = 0;
342  virtual void Deserialize() = 0;
343  virtual void ClearValue() = 0;
344 
345 
346 private:
347 
348  StationIndex m_ValueSetterStationIndex;
349  StationIndex m_ValuePreserverStationIndex;
350  ClockValue m_ValueClock;
351 
352  StationIndex m_SerializedSetterStationIndex;
353  StationIndex m_SerializedPreserverStationIndex;
354  ClockValue m_SerializedClock;
355 
356  u8* m_SerializedBuffer;
357  DivideInfo* m_aDivideInfo;
358 
359  size_t m_SerializedSize;
360  size_t m_DivideSize;
361  size_t m_DivideLastSize;
362  u32 m_DivideNum;
363 
364  ReliableLargeRequestToken m_RequestToken;
365 
366 #if 0
367  StationIndex m_SetterStationIndex;
368  StationIndex m_PreserverStationIndex;
369  ClockValue m_Clock;
370  u32 m_DestBitmap;
371  ReliableRequestToken m_RequestToken;
372  ReliableDataToken m_DataToken;
373  ReliableInitialToken m_InitialToken;
374 #endif
375 
376  static Definition<ReliableLargeCloneElementBase> s_Definition;
377 
378 public:
379  template <int TEST_ID, typename Arg>
380  static void Test(const ReliableLargeCloneElementBase& obj, Arg* pArg = NULL);
381 
382  //! @endcond
383 };
384 }
385 }
386 } // end of namespace nn::pia::clone
This is the base class for managing data that is sent and received. .
Definition: clone_CloneElementBase.h:40
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
virtual ~ReliableLargeCloneElementBase()
Destructor.
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_ReliableLargeCloneElementBase.h:51
The base class for managing the sending and receiving of reliable large-sized data.
Definition: clone_ReliableLargeCloneElementBase.h:38
ClockValue GetClock() const
Gets the clock for when the value was set.
Definition: clone_ReliableLargeCloneElementBase.h:61
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