TNB Library
TnbOverlapReader.h
[詳解]
1#pragma once
14#include "TnbSync.h"
15
16
17
18//TNB Library
19namespace TNB
20{
21
22
23
38{
39public:
40
42 COverlapReader(void) : m_hFile(INVALID_HANDLE_VALUE), m_hEvent(NULL), m_isOverLapping(false)
43 {
44 Zero(m_overLapped);
45 }
46
49 {
50 Detach();
51 if ( m_hEvent != NULL )
52 {
53 ::CloseHandle(m_hEvent);
54 m_hEvent = NULL;
55 }
56 }
57
63 void Attach(HANDLE hFile, size_t size)
64 {
65 EXCLUSIVE( &m_sync );
66 m_Cancel();
67 m_hFile = hFile;
68 m_work.Resize(size);
69 }
70
74 void Detach(void)
75 {
76 EXCLUSIVE( &m_sync );
77 m_Cancel();
78 m_hFile = INVALID_HANDLE_VALUE;
79 m_work.Free();
80 }
81
91 size_t Read(LPVOID _P, DWORD wait = 0)
92 {
93 EXCLUSIVE( &m_sync );
94 size_t r = 0;
95 if ( m_hFile != INVALID_HANDLE_VALUE && m_work.GetSize() > 0 )
96 {
97 if ( m_isOverLapping )
98 {
99 // 読み込み待機中
100 r = m_GetResult(wait);
101 }
102 else
103 {
104 // 新規
105 r = m_Read(m_work.GetSize(), m_work.Ref());
106 if ( r == 0 )
107 {
108 r = m_GetResult(wait);
109 }
110 }
111 if ( r != INVALID_SIZE && r != 0 )
112 {
113 memcpy(_P, m_work.Ref(), m_work.GetSize());
114 }
115 }
116 return r;
117 }
118
119protected:
120
130 virtual bool OnRead(HANDLE hFile, LPVOID _P, size_t size, OVERLAPPED& overLapped)
131 {
132 DWORD d;
133 bool r = !! ::ReadFile(hFile, _P, size, &d, &overLapped);
134 return r && (size == d);
135 }
136
137private:
138
148 size_t m_Read(size_t size, LPVOID _P)
149 {
150 m_Cancel();
151 Zero(m_overLapped);
152 if ( m_hEvent == NULL )
153 {
154 m_hEvent = ::CreateEvent(NULL, FALSE, TRUE, NULL);
155 ::ResetEvent(m_hEvent);
156 }
157 m_overLapped.hEvent = m_hEvent;
158 DWORD work = ULONG_MAX;
159 if ( OnRead(m_hFile, _P, size, m_overLapped) )
160 {
161 // 成功
162 return size;
163 }
164 else if ( ::GetLastError() == ERROR_IO_PENDING )
165 {
166 // 非同期読み取り操作の結果をチェックする。
167 ::Sleep(0);
168 if( ::GetOverlappedResult(m_hFile, &m_overLapped, &work, FALSE) )
169 {
170 return work;
171 }
172 else if ( ::GetLastError() == ERROR_IO_INCOMPLETE )
173 {
174 m_isOverLapping = true;
175 return 0;
176 }
177 }
178 return INVALID_SIZE;
179 }
187 size_t m_GetResult(DWORD wait = 0)
188 {
189 if ( ! m_isOverLapping )
190 {
191 return INVALID_SIZE;
192 }
193 size_t result = INVALID_SIZE;
194 DWORD r = 0;
195 switch ( ::WaitForSingleObject(m_overLapped.hEvent, wait) )
196 {
197 case WAIT_OBJECT_0:
198 if ( ::GetOverlappedResult(m_hFile, &m_overLapped, &r, FALSE) )
199 {
200 result = r; // 成功
201 m_Cancel();
202 }
203 break;
204 case WAIT_TIMEOUT:
205 result = 0;
206 break;
207 case WAIT_ABANDONED:
208 case WAIT_FAILED:
209 default:
210 m_Cancel();
211 break;
212 }
213 return result;
214 }
219 void m_Cancel(void)
220 {
221 if ( m_isOverLapping )
222 {
223 ::CancelIo(m_hFile);
224 m_isOverLapping = false;
225 }
226 }
227 HANDLE m_hEvent;
228 bool m_isOverLapping;
229 OVERLAPPED m_overLapped;
230 HANDLE m_hFile;
231 CWorkMem m_work;
232 CSyncSection m_sync;
233};
234
235
236
249{
250 DEFSUPER(COverlapReader);
251public:
252
254 COverlapIoReader(void) : _super(), m_dwIoControlCode(0)
255 {
256 }
257
263 void SetIoControlCode(DWORD code)
264 {
265 m_dwIoControlCode = code;
266 }
267
268protected:
278 virtual bool OnRead(HANDLE hFile, LPVOID _P, size_t size, OVERLAPPED& overLapped)
279 {
280 DWORD dwByteReturn;
281 BOOL ret = ::DeviceIoControl(hFile, m_dwIoControlCode, _P, size, _P, size, &dwByteReturn, &overLapped);
282 return !! ret;
283 }
284private:
285 DWORD m_dwIoControlCode;
286};
287
288
289
290}; // TNB
同期処理関係のヘッダ
[ETC] コピー不可能スーパークラス.
Definition: TnbDef.h:599
オーバーラップ入力管理クラス.
virtual bool OnRead(HANDLE hFile, LPVOID _P, size_t size, OVERLAPPED &overLapped)
[通知] 読込み.
COverlapIoReader(void)
コンストラクタ
void SetIoControlCode(DWORD code)
[設定] コントロールコード設定.
オーバーラップ入力管理クラス.
virtual bool OnRead(HANDLE hFile, LPVOID _P, size_t size, OVERLAPPED &overLapped)
[通知] 読込み.
size_t Read(LPVOID _P, DWORD wait=0)
[読込] 読込み.
void Detach(void)
[設定] デタッチ.
COverlapReader(void)
コンストラクタ
~COverlapReader(void)
デストラクタ
void Attach(HANDLE hFile, size_t size)
[設定] アタッチ.
Section排他管理クラス
Definition: TnbSync.h:125
size_t GetSize(void) const
[取得] サイズ取得
Definition: TnbDef.h:665
void Resize(size_t l)
[設定] サイズ再設定
Definition: TnbDef.h:672
void Free(void)
[設定] 解放.
Definition: TnbDef.h:652
const TYP * Ref(void) const
[取得] ポインタ取得
Definition: TnbDef.h:712
#define EXCLUSIVE(CLS)
簡易排他制御マクロ.
Definition: TnbSync.h:788
void Zero(V &value)
[設定] ゼロクリア.
Definition: TnbDef.h:399
TNB Library
Definition: TnbDoxyTitle.txt:2