TNB Library
TnbTemporaryCommunicator.h
[詳解]
1#pragma once
11#include "TnbCommunication.h"
12#include "TnbVector.h"
13
14
15
16//TNB Library
17namespace TNB
18{
19
20
21
51{
52 DEFSUPER(ICommunication);
53public:
54
56 CTemporaryCommunicator(void) : m_property(_T("TemporaryCommunicator"), TNBVIW_KIND_TEMP), m_isOpened(false), m_isSendError(false)
57 {
58 }
59
64 virtual const TProperty& GetProperty(void) const
65 {
66 return m_property;
67 }
68
75 void SetPacketSize(DWORD receivePacketSize, DWORD sendPacketSize)
76 {
77 m_property.receivePacketSize = receivePacketSize;
78 m_property.sendPacketSize = sendPacketSize;
79 }
80
88 virtual bool SetReceiveTimeout(DWORD to)
89 {
90 m_property.receiveTimeout = to;
91 return true;
92 }
93
98 virtual bool Open(void)
99 {
100 Purge();
101 m_isOpened = true;
102 return true;
103 }
104
108 virtual void Close(void)
109 {
110 Purge();
111 m_isOpened = false;
112 }
113
118 virtual bool IsOpened(void) const
119 {
120 return m_isOpened;
121 }
122
130 virtual size_t Send(size_t size, LPCVOID P)
131 {
132 if ( ! m_isOpened || m_isSendError )
133 {
134 return INVALID_SIZE;
135 }
136 CByteVector vb;
137 vb.SetElements(size, static_cast<const BYTE*>(P));
138 EXCLUSIVE( &m_sentData );
139 m_sentRowData.Add(vb);
140 m_sentData.Append(vb);
141 return size;
142 }
143
150 virtual size_t Receive(size_t size, LPVOID _P)
151 {
152 EXCLUSIVE( &m_receiveReserves );
153 if ( m_isOpened )
154 {
155 INDEX idx = m_Find();
156 if ( idx != INVALID_INDEX )
157 {
158 TParam& p = m_receiveReserves[idx];
159 BYTE* B = static_cast<BYTE*>(_P);
160 size_t l = min(p.data.GetSize(), size);
161 MemCopy(B, p.data.ReferBuffer(), l);
162 if ( l >= p.data.GetSize() )
163 {
164 m_receiveReserves.Remove(idx);
165 }
166 else
167 {
168 p.data.RemoveElements(0, l);
169 }
170 return l;
171 }
172 }
173 return 0;
174 }
175
180 virtual size_t GetReceivingDataSize(void) const
181 {
182 EXCLUSIVE( &m_receiveReserves );
183 if ( m_isOpened )
184 {
185 INDEX idx = m_Find();
186 if ( idx != INVALID_INDEX )
187 {
188 return m_receiveReserves[idx].data.GetSize();
189 }
190 }
191 return 0;
192 }
193
200 virtual EState Check(void) const
201 {
202 return m_isOpened ? CONNECTING : CLOSED;
203 }
204
208 virtual void Purge(void)
209 {
210 EXCLUSIVE2( &m_receiveReserves, &m_sentData );
211 m_receiveReserves.RemoveAll();
212 m_sentData.RemoveAll();
213 m_sentRowData.RemoveAll();
214 }
215
223 void AddReceivingData(size_t size, LPCVOID pData, DWORD delay = 0)
224 {
225 EXCLUSIVE( &m_receiveReserves );
226 if ( size > 0 )
227 {
228 TParam p;
229 p.tick = ::GetTickCount();
230 p.delay = delay;
231 p.data.SetElements(size, static_cast<const BYTE*>(pData));
232 m_receiveReserves.Add(p);
233 }
234 }
235
242 void AddReceivingData(const IConstCollectionT<BYTE>& data, DWORD delay = 0)
243 {
244 EXCLUSIVE( &m_receiveReserves );
245 if ( data.GetSize() > 0 )
246 {
247 TParam p;
248 p.tick = ::GetTickCount();
249 p.delay = delay;
250 p.data.Copy(data);
251 m_receiveReserves.Add(p);
252 }
253 }
254
262 {
263 return m_sentData;
264 }
265
273 {
274 return m_sentRowData;
275 }
276
282 void SetSendErrorMode(bool isSendError)
283 {
284 m_isSendError = isSendError;
285 }
286
287private:
288
289 INDEX m_Find(void) const
290 {
291 loop ( i, m_receiveReserves )
292 {
293 const TParam& p = m_receiveReserves[i];
294 DWORD t = ::GetTickCount() - p.tick;
295 if ( p.delay == 0 || p.delay <= t )
296 {
297 return i;
298 }
299 }
300 return INVALID_INDEX;
301 }
302
303
305 struct TParam
306 {
307 DWORD tick;
308 DWORD delay;
309 CByteVector data;
310 };
311 CVectorT<TParam> m_receiveReserves;
312 CByteVector m_sentData;
313 CVectorT<CByteVector> m_sentRowData;
314 _super::TProperty m_property;
315 bool m_isSendError;
316 bool m_isOpened;
317};
318
319
320
321}; // TNB
322
通信関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
配列型情報管理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
テンポラリ通信クラス
virtual EState Check(void) const
[確認] チェック.
virtual bool SetReceiveTimeout(DWORD to)
[設定] 受信待ち時間設定.
virtual bool Open(void)
[処理] オープン.
void AddReceivingData(size_t size, LPCVOID pData, DWORD delay=0)
[処理] 受信情報に追加.
void AddReceivingData(const IConstCollectionT< BYTE > &data, DWORD delay=0)
[処理] 受信情報に追加.
CVectorT< CByteVector > & ReferSentRowData(void)
[取得] 送信情報参照.
virtual void Purge(void)
[処理] パージ.
virtual void Close(void)
[処理] クローズ.
virtual bool IsOpened(void) const
[確認] オープン確認.
virtual size_t Receive(size_t size, LPVOID _P)
[処理] 受信.
void SetSendErrorMode(bool isSendError)
[設定] 送信エラーモード設定.
virtual const TProperty & GetProperty(void) const
[取得] 性能取得
void SetPacketSize(DWORD receivePacketSize, DWORD sendPacketSize)
[設定] パケットサイズ設定.
virtual size_t GetReceivingDataSize(void) const
[確認] 受信データ確認.
CTemporaryCommunicator(void)
コンストラクタ
virtual size_t Send(size_t size, LPCVOID P)
[処理] 送信.
CByteVector & ReferSentData(void)
[取得] 送信情報参照.
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
virtual bool Remove(INDEX index)
[削除] 要素一つ削除.
Definition: TnbVector.h:397
virtual bool RemoveAll(void)
[削除] 空化
Definition: TnbVector.h:565
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
Definition: TnbVector.h:383
virtual size_t Append(const IConstCollectionT< TYP > &c)
[追加] 追加.
Definition: TnbVector.h:545
virtual size_t SetElements(size_t size, const TYP *P=NULL)
[設定] 複数要素設定.
Definition: TnbVector.h:526
#define EXCLUSIVE2(CLS1, CLS2)
簡易排他ツイン制御マクロ.
Definition: TnbSync.h:820
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
Definition: TnbSync.h:788
TNB Library
Definition: TnbDoxyTitle.txt:2
void MemCopy(T *_pDst, const void *pSrc, size_t len)
[複製] メモリコピー
Definition: TnbDef.h:376
性能プロパティ型.
通信インターフェース
@ CONNECTING
オープン&接続状態
@ CLOSED
クローズ状態
virtual size_t GetSize(void) const =0
[取得] 要素数取得.