TNB Library
TnbPipe.h
[詳解]
1#pragma once
11#include "TnbCommunication.h"
12
13
14
15//TNB Library
16namespace TNB
17{
18
19
20
38{
39 DEFSUPER(ICommunication);
40
41public:
42
44 CPipe(void)
45 : m_hRead(INVALID_HANDLE_VALUE), m_hWrite(INVALID_HANDLE_VALUE), m_dwPipeBufferSize(0), m_property(_T("Pipe"), TNBVIW_KIND_PIPE)
46 {
47 m_isActiveClient = false;
48 }
49
51 virtual ~CPipe(void)
52 {
53 Close();
54 }
55
62 void SetPipeBufferSize(DWORD dwSize = 0)
63 {
64 m_dwPipeBufferSize = dwSize;
65 }
66
71 virtual const TProperty& GetProperty(void) const
72 {
73 return m_property;
74 }
75
83 virtual bool SetReceiveTimeout(DWORD to)
84 {
85 return false;
86 }
87
94 void SettingNamedPipe(LPCTSTR lpszPipeName, bool isClient)
95 {
96 m_pipeName = lpszPipeName;
97 m_isModeClient = isClient;
98 }
99
105 virtual bool Open(void)
106 {
107 Close();
108 //
109 if ( m_pipeName.IsEmpty() )
110 {
111 //-- 名無しパイプオープン
112 SECURITY_ATTRIBUTES sa;
113 sa.lpSecurityDescriptor = NULL;
114 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
115 sa.bInheritHandle = TRUE;
116 return !! ::CreatePipe(&m_hRead, &m_hWrite, &sa, m_dwPipeBufferSize);
117 }
118 if ( m_isModeClient )
119 {
120 //== 名前付きパイプクライアントオープン(ここではオープンしない)
121 m_isActiveClient = true;
122 m_hRead = INVALID_HANDLE_VALUE;
123 m_hWrite = INVALID_HANDLE_VALUE;
124 return true;
125 }
126 //== 名前付きパイプサーバーオープン
127 m_hRead = ::CreateNamedPipe(m_pipeName,
128 PIPE_ACCESS_DUPLEX, //オープンモード
129 PIPE_TYPE_BYTE | PIPE_WAIT, //パイプモード
130 1, //パイプに対する最大インスタンス
131 m_dwPipeBufferSize,//出力バッファ
132 m_dwPipeBufferSize,//入力バッファ
133 0,//タイムアウト
134 NULL);
135 m_hWrite = INVALID_HANDLE_VALUE;
136 return m_hRead != INVALID_HANDLE_VALUE;
137 }
138
142 virtual void Close(void)
143 {
144 if ( m_hRead != INVALID_HANDLE_VALUE )
145 {
146 ::CloseHandle(m_hRead);
147 m_hRead = INVALID_HANDLE_VALUE;
148 }
149 if ( m_hWrite != INVALID_HANDLE_VALUE )
150 {
151 ::CloseHandle(m_hWrite);
152 m_hWrite = INVALID_HANDLE_VALUE;
153 }
154 m_isActiveClient = false;
155 }
156
162 virtual bool IsOpened(void) const
163 {
164 if ( m_isActiveClient )
165 {
166 return true;
167 }
168 return m_hRead != INVALID_HANDLE_VALUE;
169 }
170
178 virtual size_t Send(size_t size, LPCVOID P)
179 {
180 m_OnActiveClient();
181 //
182 DWORD dwWriten;
183 if ( ::WriteFile(GetWriteHandle(), P, down_cast<DWORD>(size), &dwWriten, NULL) )
184 {
185 return dwWriten;
186 }
187 return INVALID_SIZE;
188 }
189
198 virtual size_t Receive(size_t size, LPVOID _P)
199 {
200 size_t l = GetReceivingDataSize();
201 if ( l == 0 )
202 {
203 return 0;
204 }
205 if ( l != INVALID_SIZE )
206 {
207 if ( size < l )
208 {
209 l = size;
210 }
211 DWORD dwReadBytes = 0;
212 if ( !! ::ReadFile(m_hRead, _P, down_cast<DWORD>(l), &dwReadBytes, NULL) )
213 {
214 return dwReadBytes;
215 }
216 _GetLastError("ReadFile");
217 }
218 return INVALID_SIZE;
219 }
220
226 virtual size_t GetReceivingDataSize(void) const
227 {
228 m_OnActiveClient();
229 if ( m_hRead == INVALID_HANDLE_VALUE )
230 {
231 return INVALID_SIZE;
232 }
233 //
234 DWORD dwAvail;
235 if ( ::PeekNamedPipe(m_hRead, NULL, 0, NULL, &dwAvail, NULL) )
236 {
237 return dwAvail;
238 }
239 if ( ! m_pipeName.IsEmpty() && m_isModeClient )
240 {
241 m_hRead = INVALID_HANDLE_VALUE;
242 }
243 _GetLastError("PeekNamedPipe");
244 return INVALID_SIZE;
245 }
246
253 virtual EState Check(void) const
254 {
255 m_OnActiveClient();
256 return IsConnect() ? CONNECTING : CLOSED;
257 }
258
263 virtual void Purge(void)
264 {
265 }
266
271 HANDLE GetReadHandle(void) const
272 {
273 return m_hRead;
274 }
275
280 HANDLE GetWriteHandle(void) const
281 {
282 if ( m_pipeName.IsEmpty() )
283 {
284 return m_hWrite;
285 }
286 return m_hRead;
287 }
288
289private:
290 // 名前付きパイプクライアントがアクティブの時の処理
291 void m_OnActiveClient(void) const
292 {
293 if ( m_isActiveClient && ! m_pipeName.IsEmpty() && m_isModeClient )
294 {
295 if ( m_hRead == INVALID_HANDLE_VALUE )
296 {
297 // パイプクライアントをオープン
298 m_hRead = ::CreateFile(m_pipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
299 }
300 }
301 }
302
303 CStr m_pipeName;
304 bool m_isModeClient;
305 bool m_isActiveClient;
306 mutable HANDLE m_hRead;
307 HANDLE m_hWrite;
308 DWORD m_dwPipeBufferSize;
309 TProperty m_property;
310};
311
312
313
314}; // TNB
通信関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
パイプアクセスクラス
Definition: TnbPipe.h:38
HANDLE GetReadHandle(void) const
[取得] 読み込みハンドル取得
Definition: TnbPipe.h:271
virtual EState Check(void) const
[確認] チェック.
Definition: TnbPipe.h:253
CPipe(void)
コンストラクタ
Definition: TnbPipe.h:44
void SettingNamedPipe(LPCTSTR lpszPipeName, bool isClient)
[設定] パイプ名設定.
Definition: TnbPipe.h:94
virtual bool SetReceiveTimeout(DWORD to)
[設定] 受信待ち時間設定.
Definition: TnbPipe.h:83
virtual bool Open(void)
[処理] オープン.
Definition: TnbPipe.h:105
HANDLE GetWriteHandle(void) const
[取得] 書き込みハンドル取得
Definition: TnbPipe.h:280
void SetPipeBufferSize(DWORD dwSize=0)
[設定] パイプバッファサイズ設定
Definition: TnbPipe.h:62
virtual void Purge(void)
[処理] パージ.
Definition: TnbPipe.h:263
virtual void Close(void)
[処理] クローズ.
Definition: TnbPipe.h:142
virtual bool IsOpened(void) const
[確認] オープン確認.
Definition: TnbPipe.h:162
virtual size_t Receive(size_t size, LPVOID _P)
[処理] 受信.
Definition: TnbPipe.h:198
virtual ~CPipe(void)
デストラクタ
Definition: TnbPipe.h:51
virtual const TProperty & GetProperty(void) const
[取得] 性能取得.
Definition: TnbPipe.h:71
virtual size_t GetReceivingDataSize(void) const
[確認] 受信データ確認.
Definition: TnbPipe.h:226
virtual size_t Send(size_t size, LPCVOID P)
[処理] 送信.
Definition: TnbPipe.h:178
bool IsEmpty(void) const
[確認] 空チェック
Definition: TnbStr.h:528
TNB Library
Definition: TnbDoxyTitle.txt:2
性能プロパティ型.
通信インターフェース
virtual bool IsConnect(void) const
[確認] 接続確認.
@ CONNECTING
オープン&接続状態
@ CLOSED
クローズ状態