CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
clone_EventCloneElementBase.h
1 /*--------------------------------------------------------------------------------*
2  Copyright (C)Nintendo All rights reserved.
3 
4  These coded instructions, statements, and computer programs contain proprietary
5  information of Nintendo and/or its licensed developers and are protected by
6  national and international copyright laws. They may not be disclosed to third
7  parties or copied or duplicated in any form, in whole or in part, without the
8  prior written consent of Nintendo.
9 
10  The content herein is highly confidential and should be handled accordingly.
11  *--------------------------------------------------------------------------------*/
12 
13 
14 #pragma once
15 
16 #include <nn/pia/clone/clone_Definitions.h>
17 #include <nn/pia/clone/clone_CloneElementBase.h>
18 #include <nn/pia/common/common_OffsetList.h>
19 
20 namespace nn
21 {
22 namespace pia
23 {
24 namespace clone
25 {
26 
27 class IDataPacker;
28 
29 /*!
30  @brief イベントの送受信を管理する基底クラスです。
31  */
33 {
34 public:
35  /*!
36  @brief デストラクタです。
37  */
38  virtual ~EventCloneElementBase();
39 
40 
41  /*!
42  @brief デバッグに有用な情報をプリントします。
43 
44  @param[in] flag トレースフラグの論理和。詳細は @ref TraceFlag 型を参照してください。
45  */
46  void Trace(uint64_t flag) const;
47 
48 
49  /*!
50  @brief イベントの取りこぼしエラーが発生したかどうかを取得します。
51  @details AtomicSharingClone で使用しているときにステーションの異常切断が発生すると、
52  受信するはずだったイベントを受信し損ねる場合があります。
53  いずれかの EventCloneElement でこのエラーが発生した場合、
54  CloneProtocol::GetError() が CloneProtocol::ErrorType_EventDropped を返すようになります。
55  @return イベントの取りこぼしエラーが発生していたら true を返します。
56  @see CloneProtocol::GetError
57  */
58  bool IsEventDropped() const
59  {
60  return m_IsEventDropped;
61  }
62 
63  /*!
64  @brief 現在のイベントバッファの使用数を取得します。
65 
66  @details ReceiveClone、AtomicSharingClone、SequentialSharingClone に登録されている場合、
67  返り値が 0 にならない場合があります。
68  次のデータを受信時に、消してもよいデータがバッファから削除されます。
69 
70  @return イベントバッファの使用数を返します。
71  */
72  uint32_t GetUsingBufferNum() const;
73 
74 
75  /*!
76  @cond PRIVATE
77  @brief イベントバッファ中の指定したイベントが、どのステーションからの Ack を待っているかを取得します。
78 
79  @param[in] index イベントバッファ中の一番古いイベントを 0 番としたときの、取得対象のイベントのインデックスです。
80  0 以上、現在のイベントバッファの使用数未満の値を設定してください。
81 
82  @return 指定したイベントが Ack を待っているステーションのステーションインデックスを表すビットマップを返します。
83  @see GetUsingBufferNum
84  */
85  uint32_t GetBufferEventWaitingStationIndexBitmap(int index) const;
86  //! @endcond
87 
88  /*!
89  @cond PRIVATE
90  @brief イベントバッファ中の指定したイベントの発行時刻を取得します。
91 
92  @param[in] index イベントバッファ中の一番古いイベントを 0 番としたときの、取得対象のイベントのインデックスです。
93  0 以上、現在のイベントバッファの使用数未満の値を設定してください。
94 
95  @return 指定したイベントの発行時刻を返します。取得対象のイベントが無い場合は 0 を返します。
96  @see GetUsingBufferNum
97  */
98  ClockValue GetBufferEventClock(int index) const;
99  //! @endcond
100 
101 
102  //! @cond PRIVATE
103 
104 public:
105  virtual Type GetType() const
106  {
107  return GetTypeStatic();
108  }
109  virtual bool IsCloneSupported(int cloneType) const;
110 
111  static Type GetTypeStatic()
112  {
113  return Type_Event;
114  }
115 
116  static bool Receive(
117  CloneElementBase* pElement,
118  IDataPacker* pAckPacker,
119  const void* cpChunk,
120  uint16_t chunkSize,
121  Id elementId,
122  StationIndex src,
123  StationIndex localStationIndex,
124  uint32_t dispatchCount);
125 
126  static uint32_t GetDataChunkHeaderSize();
127 
128 public:
129  typedef uint16_t Index;
130 
131 private:
132  enum ReceiveDataResponse
133  {
134  ReceiveDataResponse_Nothing,
135  ReceiveDataResponse_Ack,
136  ReceiveDataResponse_PreserverMigration
137  };
138  ReceiveDataResponse ReceiveData(const void* cpData, uint32_t size, StationIndex src, StationIndex setter, ClockValue clock, uint64_t registerCountAll, uint32_t destBitmap, Index index, Index eraseIndex);
139  void ReceiveAck(StationIndex src, Index index);
140  void ReceivePreserverMigration(StationIndex src, Index index);
141 
142  void ReceiveInitial(StationIndex src, Index index);
143  void ReceiveInitialAck(StationIndex src);
144 
145 public:
146  virtual void ClearData();
147 
148  virtual void RemoveDest(StationIndex stationIndex);
149  virtual void AddParticipant(StationIndex stationIndex);
150  virtual void Complement(StationIndex stationIndex);
151  virtual void Lock();
152 
153 protected:
154  static const uint32_t BufferSizeMin = 2;
155  static const uint32_t BufferSizeMax = 0x3fff;
156 
157 public:
158  class EventSendToken : public CloneElementBase::ResendableSendToken
159  {
160  public:
161  enum TokenType
162  {
163  TokenType_Initial,
164  TokenType_Event
165  };
166  virtual TokenType GetTokenType() const = 0;
167  };
168 
169 
170  class EventInitialToken : public EventSendToken
171  {
172  public:
173  explicit EventInitialToken(EventCloneElementBase* pElement)
174  : EventSendToken(), m_pElement(pElement)
175  {
176  }
177 
178  virtual Type GetElementType() const
179  {
180  return Type_Event;
181  }
182  virtual uint32_t GetDestBitmap() const
183  {
184  return m_DestBitmap;
185  }
186  virtual TokenType GetTokenType() const
187  {
188  return TokenType_Initial;
189  }
190  virtual uint32_t GetChunkSize() const;
191  virtual void WriteChunk(void* pChunkBuffer) const;
192  virtual CloneElementBase* GetElement()
193  {
194  return m_pElement;
195  }
196 
197  Id GetId() const
198  {
199  return m_pElement->GetId();
200  }
201 
202  Index GetInitialIndex() const
203  {
204  return m_pElement->m_pEventTokenBuffer->GetNextIndex();
205  }
206  void SetDestBitmap(uint32_t destBitmap)
207  {
208  m_DestBitmap = destBitmap;
209  }
210  virtual void TraceTokenType()
211  {
212  PIA_TRACE_EX(common::TraceFlagClone, "TokenType: EventInitialToken");
213  };
214 
215  private:
216  EventCloneElementBase* m_pElement;
217  uint32_t m_DestBitmap;
218  };
219 
220 
221  class EventTokenBase : public EventSendToken
222  {
223  public:
224  EventTokenBase()
225  : EventSendToken()
226  {
227  }
228  void Init(EventCloneElementBase* pElement)
229  {
230  m_pElement = pElement;
231  }
232 
233  virtual Type GetElementType() const
234  {
235  return Type_Event;
236  }
237  virtual uint32_t GetDestBitmap() const
238  {
239  return m_DestBitmap;
240  }
241  virtual TokenType GetTokenType() const
242  {
243  return TokenType_Event;
244  }
245  virtual uint32_t GetChunkSize() const;
246  virtual void WriteChunk(void* pChunkBuffer) const;
247  virtual CloneElementBase* GetElement()
248  {
249  return m_pElement;
250  }
251  virtual bool IsUnicast() const
252  {
253 #if NN_PIA_ENABLE_CLONE_BROADCAST_RESEND
254  return false;
255 #else
256  return true;
257 #endif
258  }
259 
260  Index GetIndex() const
261  {
262  return m_Index;
263  }
264  StationIndex GetSetterStationIndex() const
265  {
266  return m_SetterStationIndex;
267  }
268  StationIndex GetPreserverStationIndex() const
269  {
270  return m_PreserverStationIndex;
271  }
272  const ClockValue& GetClock() const
273  {
274  return m_Clock;
275  }
276  uint64_t GetRegisterCountAll() const
277  {
278  return m_RegisterCountAll;
279  }
280 
281  void SetDestBitmap(uint32_t destBitmap)
282  {
283  m_DestBitmap = destBitmap;
284  }
285  void RemoveDestBmp(uint32_t bmp)
286  {
287  m_DestBitmap &= ~bmp;
288  }
289  void RemoveDestAll()
290  {
291  m_DestBitmap = 0;
292  }
293  void SetIndex(Index index)
294  {
295  m_Index = index;
296  }
297  void SetSetterStationIndex(StationIndex stationIndex)
298  {
299  m_SetterStationIndex = stationIndex;
300  }
301  void SetPreserverStationIndex(StationIndex stationIndex)
302  {
303  m_PreserverStationIndex = stationIndex;
304  }
305  void SetClock(ClockValue clock)
306  {
307  m_Clock = clock;
308  }
309  void SetRegisterCountAll(uint64_t count)
310  {
311  m_RegisterCountAll = count;
312  }
313 
314  enum State
315  {
316  State_EventTokenEmpty,
317  State_EventTokenSet,
318  State_EventTokenReceived,
319  State_EventTokenHandled
320  };
321 
322  State GetState() const
323  {
324  return m_State;
325  }
326  void SetState(State state)
327  {
328  m_State = state;
329  }
330 
331  Id GetId() const
332  {
333  return m_pElement->GetId();
334  }
335  virtual uint32_t GetSize() const
336  {
337  return m_pElement->GetSize();
338  }
339  virtual void Serialize(void* pBuffer) const = 0;
340  virtual void Deserialize(const void* cpBuffer) = 0;
341  Index GetEraseIndex() const
342  {
343  return m_pElement->m_pEventTokenBuffer->GetEraseIndex();
344  }
345 
346  void CancelSendData()
347  {
348  m_pElement->CancelSendData(this);
349  }
350  void AddSendData()
351  {
352  m_pElement->AddSendData(this);
353  }
354  virtual void TraceTokenType()
355  {
356  PIA_TRACE_EX(common::TraceFlagClone, "TokenType: EventTokenBase");
357  };
358 
359  private:
360  EventCloneElementBase* m_pElement;
361  uint32_t m_DestBitmap;
362  Index m_Index;
363  StationIndex m_SetterStationIndex;
364  StationIndex m_PreserverStationIndex;
365  ClockValue m_Clock;
366  uint64_t m_RegisterCountAll;
367  State m_State;
368  };
369 
370 
371  class EventTokenBufferBase
372  {
373  protected:
374  explicit EventTokenBufferBase(uint32_t size);
375 
376  public:
377  void Clear();
378  void InitIndex(Index index);
379  EventTokenBase* Get(Index index);
380  EventTokenBase* AssignNext();
381  EventTokenBase* Assign(Index index);
382  void Recycle();
383  bool ShiftHead();
384  Index GetHeadIndex() const
385  {
386  return At(m_HeadIdx)->GetIndex();
387  }
388  bool IsEmpty() const
389  {
390  return (m_Num == 0);
391  }
392  Index GetEraseIndex() const
393  {
394  return m_EraseIndex;
395  }
396  Index GetNextIndex() const;
397 
398  void RemoveDest(StationIndex stationIndex);
399  void Complement(StationIndex stationIndex, uint32_t destBitmap);
400  void Lock();
401 
402  uint32_t GetUsingBufferNum()
403  {
404  return m_Num;
405  }
406 
407  uint32_t GetBufferEventWaitingStationIndexBitmap(int index) const;
408  ClockValue GetBufferEventClock(int index) const;
409 
410  protected:
411  virtual EventTokenBase* At(int idx) = 0;
412  virtual const EventTokenBase* At(int idx) const = 0;
413 
414  private:
415  int m_BufferSize;
416  int m_HeadIdx;
417  int m_Num;
418 
419  Index m_EraseIndex;
420  };
421 
422 protected:
423  explicit EventCloneElementBase(EventTokenBufferBase* pEventTokenBuffer);
424 
425 protected:
426  Result SetValueCore(EventTokenBase** ppTokenBase);
427  void HandleNextCore();
428  const EventTokenBase* GetHandlingEventToken() const
429  {
430  return m_pHandlingEventToken;
431  }
432 
433 
434 private:
435  enum IndexState
436  {
437  IndexState_EventInvalid,
438  IndexState_EventInit,
439  IndexState_EventAdjust,
440  IndexState_EventValid
441  };
442 
443  IndexState m_IndexState;
444 
445  Index m_HandledIndex;
446  EventTokenBase* m_pHandlingEventToken;
447  Index m_PreIndex;
448 
449  EventInitialToken m_InitialToken;
450  EventTokenBufferBase* m_pEventTokenBuffer;
451 
452  bool m_IsEventDropped;
453 
454  static Definition<EventCloneElementBase> s_Definition;
455 
456 public:
457  template <int TEST_ID, typename Arg>
458  static void Test(const EventCloneElementBase& obj, Arg* pArg = NULL);
459 
460  NN_PIA_DISALLOW_COPY(EventCloneElementBase);
461  //! @endcond
462 };
463 }
464 }
465 } // end of namespace nn::pia::clone