CTR-Pia  5.4.3
Game Communication Engine
 全て クラス ネームスペース 関数 変数 型定義 列挙型 列挙型の値 ページ
reckoning_ReckoningCloneElementBase.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/reckoning/reckoning_Definitions.h>
17 #include <nn/pia/clone/clone_CloneElementBase.h>
18 #include <nn/pia/common/common_OffsetList.h>
19 
20 
21 namespace nn
22 {
23 namespace pia
24 {
25 namespace clone
26 {
27 class CloneProtocol;
28 class IDataPacker;
29 }
30 }
31 }
32 
33 
34 namespace nn
35 {
36 namespace pia
37 {
38 namespace reckoning
39 {
40 
41 /*!
42  @brief 推測航法を用いながらの値の送受信を管理する基底クラスです。
43  */
45 {
46 public:
47  /*!
48  @brief デストラクタです。
49  */
51 
52 
53  /*!
54  @brief @ref ReckoningCloneElement::GetValue "GetValue()" で有効な値が取得できるかどうかを取得します。
55  @return @ref ReckoningCloneElement::GetValue "GetValue()" で有効な値を取得できる場合は true を返します。
56  */
57  bool IsValidValue();
58 
59 
60  /*!
61  @brief デバッグに有用な情報をプリントします。
62 
63  @param[in] flag トレースフラグの論理和。詳細は @ref TraceFlag 型を参照してください。
64  */
65  void Trace(uint64_t flag) const;
66 
67 
68  //! @cond PRIVATE
69 
70 public:
71  virtual Type GetType() const
72  {
73  return GetTypeStatic();
74  }
75  virtual bool IsCloneSupported(int cloneType) const;
76 
77  static Type GetTypeStatic()
78  {
79  return Type_Reckoning;
80  }
81  static bool Receive(CloneElementBase* pElement, clone::IDataPacker* pAckPacker, const void* cpChunk, uint16_t chunkSize, Id elementId, StationIndex src, StationIndex myStationIndex, uint32_t dispatchCount);
82  static uint32_t GetDataChunkHeaderSize();
83 
84 private:
85  void ReceiveUnreliable(const void* cpData, uint32_t size, StationIndex src, StationIndex setter, clone::ClockValue clock);
86  void ReceiveReliable(const void* cpData, uint32_t size, StationIndex src, StationIndex setter, clone::ClockValue clock, uint32_t destBmp);
87  void ReceiveAck(StationIndex src, StationIndex setter, clone::ClockValue clock);
88 
89 public:
90  virtual void ClearData();
91 
92  virtual void AddDest(StationIndex stationId);
93  virtual void RemoveDest(StationIndex stationId);
94  virtual void Adjust(StationIndex stationId);
95 
96  virtual void RequestInitialData();
97 
98 public:
99  class ReckoningSendToken : public ConversibleResendableSendToken
100  {
101  public:
102  ReckoningSendToken()
103  : ConversibleResendableSendToken()
104  {
105  }
106  void Init(ReckoningCloneElementBase* pElement)
107  {
108  m_pElement = pElement;
109  }
110 
111  virtual Type GetElementType() const
112  {
113  return Type_Reckoning;
114  }
115  virtual uint32_t GetDestBitmap() const
116  {
117  return m_DestBmp;
118  }
119  virtual uint32_t GetChunkSize() const;
120  virtual void WriteChunk(void* pChunkBuffer) const;
121  virtual CloneElementBase* GetElement()
122  {
123  return m_pElement;
124  }
125 
126  Id GetId() const
127  {
128  return m_pElement->GetId();
129  }
130  uint32_t GetSize() const
131  {
132  return m_pElement->GetSize();
133  }
134  clone::ClockValue GetClock() const
135  {
136  return m_Clock;
137  }
138  void Serialize(void* pBuffer) const
139  {
140  m_pElement->Serialize(pBuffer, this);
141  }
142 
143  StationIndex GetSetterStationIndex() const
144  {
145  return m_SetterStationIndex;
146  }
147 
148  void SetDestBitmap(uint32_t destBmp)
149  {
150  m_DestBmp = destBmp;
151  }
152  void SetClock(clone::ClockValue clock)
153  {
154  m_Clock = clock;
155  }
156  void SetSetterStationIndex(StationIndex stationId)
157  {
158  m_SetterStationIndex = stationId;
159  }
160 
161  void AddSendData()
162  {
163  m_pElement->AddSendData(this);
164  }
165  void CancelSendData()
166  {
167  m_pElement->CancelSendData(this);
168  }
169 
170  private:
171  ReckoningCloneElementBase* m_pElement;
172  uint32_t m_DestBmp;
173  clone::ClockValue m_Clock;
174  StationIndex m_SetterStationIndex;
175  };
176 
177 protected:
178  template <typename Sample>
179  class SampleReckoningSendToken : public ReckoningSendToken
180  {
181  public:
182  SampleReckoningSendToken()
183  : ReckoningSendToken()
184  {
185  }
186  Sample* GetSamplePtr()
187  {
188  return &m_Sample;
189  }
190  const Sample& GetSample() const
191  {
192  return m_Sample;
193  }
194 
195  private:
196  Sample m_Sample;
197  };
198 
199 protected:
200  ReckoningCloneElementBase(ReckoningSendToken** papBuffer, uint32_t bufferSize);
201 
202 protected:
203  void UpdateValue();
204  Result SetValueCore(bool* pIsSend);
205  ReckoningSendToken* AssignTokenForSend(int* pIdx, bool isResendable);
206 
207  virtual void Serialize(void* pBuffer, const ReckoningSendToken* cpToken) = 0;
208  virtual void ClearValue() = 0;
209  virtual bool EstimateValue(clone::ClockValue clock) = 0;
210 
211  virtual void Deserialize(ReckoningSendToken* pToken, const void* cpData) = 0;
212  virtual void OnUpdateSample(int idx) = 0;
213 
214 protected:
215  class SampleBuffer
216  {
217  public:
218  SampleBuffer(ReckoningSendToken** apBuffer, int size);
219 
220  void Clear();
221  ReckoningSendToken* Get(int idx);
222  const ReckoningSendToken* Get(int idx) const;
223  bool IsHead(ReckoningSendToken* pToken) const
224  {
225  return (pToken == m_apBuffer[m_HeadIdx]);
226  }
227  ReckoningSendToken* Search(clone::ClockValue clock);
228  ReckoningSendToken* Assign(int* pIdx, clone::ClockValue clock);
229  int GetNum() const
230  {
231  return m_Num;
232  }
233 
234  void AddDest(StationIndex stationId, bool IsResponsible);
235  void RemoveDest(StationIndex stationId);
236  void Adjust();
237 
238  private:
239  ReckoningSendToken** m_apBuffer;
240  int m_Size;
241  int m_HeadIdx;
242  int m_Num;
243  };
244 
245  const SampleBuffer& GetSampleBuffer() const
246  {
247  return m_SampleBuffer;
248  }
249 
250  //! @endcond
251 
252 public:
253  /*!
254  @brief 標本値バッファの値を参照するためのクラスです。
255  @tparam Sample 標本値の型です。
256  */
257  template <typename Sample>
259  {
260  public:
261  //! @cond PRIVATE
262  explicit SampleAccessor(const SampleBuffer& cBuffer)
263  : m_cBuffer(cBuffer)
264  {
265  }
266  //! @endcond
267 
268 
269  /*!
270  @brief 保存されている標本値の数を取得します。
271  */
272  int GetNum() const
273  {
274  return m_cBuffer.GetNum();
275  }
276 
277 
278  /*!
279  @brief 標本値が設定された時刻を取得します。
280  @param[in] index 標本値のインデックスです。値が小さい方が新しい標本値を表します。
281  @return 標本値が設定された時刻です。
282  */
283  clone::ClockValue GetClock(int index) const
284  {
285  PIA_ASSERT(index >= 0 && index < GetNum());
286  return m_cBuffer.Get(index)->GetClock();
287  }
288 
289 
290  /*!
291  @brief 標本値を取得します。
292  @param[in] index 標本値のインデックスです。値が小さい方が新しい標本値を表します。
293  @return 標本値です。
294  */
295  const Sample& GetSample(int index) const
296  {
297  PIA_ASSERT(index >= 0 && index < GetNum());
298  return static_cast<const SampleReckoningSendToken<Sample>*>(m_cBuffer.Get(index))->GetSample();
299  }
300 
301  private:
302  const SampleBuffer& m_cBuffer;
303  };
304 
305 
306  //! @cond PRIVATE
307 
308 private:
309  SampleBuffer m_SampleBuffer;
310  clone::ClockValue m_Clock;
311  bool m_IsValidValue;
312  StationIndex m_ResponsibleStationIndex;
313 
314  static Definition<ReckoningCloneElementBase> s_Definition;
315 
316 public:
317 #if 0
318  void Test(const CloneProtocol* cpProtocol);
319 #endif
320 
321 public:
322  template <int TEST_ID, typename Arg>
323  static void Test(const ReckoningCloneElementBase& obj, Arg* pArg = NULL);
324 
325  NN_PIA_DISALLOW_COPY(ReckoningCloneElementBase);
326  //! @endcond
327 };
328 }
329 }
330 } // end of namespace nn::pia::reckoning