TNB Library
TnbCopyDataMessage.h
[詳解]
1#pragma once
13#include "TnbCommunication.h"
14
15
16
17//TNB Library
18namespace TNB
19{
20
21
22
52{
53 DEFSUPER(ICommunication);
54 _super::TProperty m_property;
55protected:
58 ULONG_PTR m_dwCookie;
59
60public:
61
65 CCopyDataMessage(void) : m_property(_T("CopyDataMsg"), TNBVIW_KIND_TEMP), m_dwCookie(0), m_hPartnerWnd(NULL)
66 {
67 }
68
73 virtual const TProperty& GetProperty(void) const
74 {
75 return m_property;
76 }
77
85 virtual bool SetReceiveTimeout(DWORD to)
86 {
87 return false;
88 }
89
95 void SetParamater(HWND hPartnerWnd, ULONG_PTR dwCookie = 0)
96 {
97 m_hPartnerWnd = hPartnerWnd;
98 m_dwCookie = dwCookie;
99 }
100
106 virtual bool Open(void)
107 {
108 return IsOpened();
109 }
110
114 virtual void Close(void)
115 {
117 return;
118 }
119
125 virtual bool IsOpened(void) const
126 {
127 return !! ::IsWindow(m_hPartnerWnd);
128 }
129
137 virtual size_t Send(size_t size, LPCVOID P)
138 {
139 if ( IsOpened() )
140 {
141 COPYDATASTRUCT data;
142 data.dwData = m_dwCookie;
143 data.cbData = ToDword(size);
144 data.lpData = const_cast<PVOID>(P);
145 LPARAM l = reinterpret_cast<LPARAM>(&data);
146 DWORD_PTR result = 0;
147 if ( ::SendMessageTimeout(m_hPartnerWnd, WM_COPYDATA, 0, l, SMTO_NORMAL, 1000, &result) )
148 {
149 return result;
150 }
151 }
152 return INVALID_SIZE;
153 }
154
161 virtual size_t Receive(size_t size, LPVOID _P)
162 {
163 size_t l = m_vbReceivedData.GetElements(size, static_cast<BYTE*>(_P));
165 return l;
166 }
167
172 virtual size_t GetReceivingDataSize(void) const
173 {
174 return m_vbReceivedData.GetSize();
175 }
176
183 virtual EState Check(void) const
184 {
185 if ( ::IsWindow(m_hPartnerWnd) )
186 {
187 return CONNECTING;
188 }
189 return CLOSED;
190 }
191
195 virtual void Purge(void)
196 {
198 }
199
210 bool RelayEvent(UINT message, WPARAM wParam, LPARAM lParam, LRESULT& _result)
211 {
212 if ( message == WM_COPYDATA )
213 {
214 COPYDATASTRUCT* pData = reinterpret_cast<COPYDATASTRUCT*>(lParam);
215 if ( pData->dwData == m_dwCookie )
216 {
217 size_t size = pData->cbData;
218 PVOID P = pData->lpData;
219 _result = m_vbReceivedData.AddElements(size, static_cast<const BYTE*>(P));
220 return true;
221 }
222 }
223 return false;
224 }
225};
226
227
228
259{
260 DEFSUPER(CCopyDataMessage);
261 UINT m_msg;
262 enum
263 {
264 COOKIE_MASK = 0x3FFFFFFF,
265 HASBYTE_MASK = 0xC0000000,
266 HASBYTE_SHIFT = 30,
267 HAS1BYTE = 0x00000000,
268 HAS2BYTE = 0x40000000,
269 HAS3BYTE = 0x80000000,
270 HAS4BYTE = 0xC0000000,
271 };
272public:
273
275 CCopyDataPostMessage(void) : _super(), m_msg(0)
276 {
277 }
278
284 virtual bool Open(void)
285 {
286 return m_msg != 0 && IsOpened();
287 }
288
293 void SetMessage(UINT msg)
294 {
295 m_msg = msg;
296 }
297
302 void SetMessage(LPCTSTR msg)
303 {
304 m_msg = ::RegisterWindowMessage(msg);
305 }
306
314 virtual size_t Send(size_t size, LPCVOID P)
315 {
316 if ( ! IsOpened() )
317 {
318 return INVALID_SIZE;
319 }
320 ULONG_PTR cookie = m_dwCookie & COOKIE_MASK;
321 const BYTE* B = static_cast<const BYTE*>(P);
322 INDEX pos = 0;
323 loop ( i, size / 4 )
324 {
325 const DWORD* W = reinterpret_cast<const DWORD*>(&B[pos]);
326 ::PostMessage(m_hPartnerWnd, m_msg, cookie | HAS4BYTE, *W);
327 pos += 4;
328 }
329 switch ( size & 3 )
330 {
331 case 0:
332 default:
333 break;
334 case 1:
335 ::PostMessage(m_hPartnerWnd, m_msg, cookie | HAS1BYTE, B[pos]);
336 break;
337 case 2:
338 ::PostMessage(m_hPartnerWnd, m_msg, cookie | HAS2BYTE, B[pos] | (B[pos + 1] << 8));
339 break;
340 case 3:
341 ::PostMessage(m_hPartnerWnd, m_msg, cookie | HAS3BYTE, B[pos] | (B[pos + 1] << 8) | (B[pos + 2] << 16));
342 break;
343 }
344 return size;
345 }
346
357 bool RelayEvent(UINT message, WPARAM wParam, LPARAM lParam, LRESULT& _result)
358 {
359 if ( message == m_msg )
360 {
361 if ( (wParam & COOKIE_MASK) == (m_dwCookie & COOKIE_MASK) )
362 {
363 int len = (ToInt(wParam & HASBYTE_MASK) >> HASBYTE_SHIFT) + 1;
364 if ( len >= 1 )
365 {
366 m_vbReceivedData.Add(static_cast<BYTE>(lParam & 0xFF));
367 }
368 if ( len >= 2 )
369 {
370 m_vbReceivedData.Add(static_cast<BYTE>((lParam >> 8) & 0xFF));
371 }
372 if ( len >= 3 )
373 {
374 m_vbReceivedData.Add(static_cast<BYTE>((lParam >> 16) & 0xFF));
375 }
376 if ( len >= 4 )
377 {
378 m_vbReceivedData.Add(static_cast<BYTE>((lParam >> 24) & 0xFF));
379 }
380 return true;
381 }
382 }
383 return false;
384 }
385};
386
387
388
389}; // TNB
390
通信関係のヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
コピーデータメッセージクラス
virtual EState Check(void) const
[確認] チェック.
bool RelayEvent(UINT message, WPARAM wParam, LPARAM lParam, LRESULT &_result)
[処理] イベント中継
CCopyDataMessage(void)
コンストラクタ
virtual bool SetReceiveTimeout(DWORD to)
[設定] 受信待ち時間設定.
virtual bool Open(void)
[処理] オープン.
void SetParamater(HWND hPartnerWnd, ULONG_PTR dwCookie=0)
[設定] オープン設定
HWND m_hPartnerWnd
送信先のウィンドウ
virtual void Purge(void)
[処理] パージ.
virtual void Close(void)
[処理] クローズ.
CVectorT< BYTE > m_vbReceivedData
受信したデータ。
virtual bool IsOpened(void) const
[確認] オープン確認.
virtual size_t Receive(size_t size, LPVOID _P)
[処理] 受信.
virtual const TProperty & GetProperty(void) const
[取得] 性能取得
virtual size_t GetReceivingDataSize(void) const
[確認] 受信データ確認.
ULONG_PTR m_dwCookie
識別子
virtual size_t Send(size_t size, LPCVOID P)
[処理] 送信.
コピーデータポストメッセージクラス
bool RelayEvent(UINT message, WPARAM wParam, LPARAM lParam, LRESULT &_result)
[処理] メッセージ中継.
void SetMessage(LPCTSTR msg)
[設定] メッセージ設定
virtual bool Open(void)
[処理] オープン.
void SetMessage(UINT msg)
[設定] メッセージ設定
CCopyDataPostMessage(void)
コンストラクタ
virtual size_t Send(size_t size, LPCVOID P)
[処理] 送信.
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
virtual size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbVector.h:368
virtual size_t RemoveElements(INDEX index, size_t size=0)
[削除] 要素削除.
Definition: TnbVector.h:409
virtual bool RemoveAll(void)
[削除] 空化
Definition: TnbVector.h:565
virtual size_t AddElements(size_t size, const TYP *P=NULL)
[追加] 複数要素追加.
Definition: TnbVector.h:456
virtual INDEX Add(const TYP &t)
[追加] 要素一つ追加.
Definition: TnbVector.h:383
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:395
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
TNB Library
Definition: TnbDoxyTitle.txt:2
性能プロパティ型.
通信インターフェース
@ CONNECTING
オープン&接続状態
@ CLOSED
クローズ状態
virtual size_t GetElements(size_t size, TYP *_P, INDEX offset=0) const
[取得] 複数要素取り出し.