#include <ccafile.h>
Public Member Functions | |
asynchstreambuf () | |
constructor | |
~asynchstreambuf () | |
destructor - clean up event and critical section | |
void | DownloadHasEnded (HRESULT Status) |
void | SetDontFail (BOOL state) |
virtual streampos | seekoff (streamoff sOff, ios::seekdir eDir, INT32 mode=ios::in|ios::out) |
before the normal seek operation is attempted we test to see whether it is possible - if not this thread will wait until a "xsputn" wakes us - | |
virtual streampos | seekpos (streampos, INT32 mode=ios::in|ios::out) |
before the normal seek operation is attempted we test to see whether it is possible - if not this thread will wait until a "xsputn" wakes us - | |
virtual INT32 | xsputn (const char *, INT32) |
the 'write' function - wake the read thread | |
virtual INT32 | xsgetn (char *, INT32) |
the 'read' function - block if data is not available | |
void | SetLoadContext (LoadContext *pLContext) |
attach a load context for this buffer (used to syb::nchorise the read and write threads) and initialise the handle array used in WaitForMulitpleObjects() | |
LoadContext * | GetLoadContext () const |
Private Member Functions | |
BOOL | DataAvailable (INT32 Length) |
Private Attributes | |
LoadContext * | pLoadContext |
wxCondition * | WakeEvents [2] |
BOOL | DontFail |
Definition at line 126 of file ccafile.h.
|
constructor
Definition at line 139 of file ccafile.cpp.
|
|
destructor - clean up event and critical section
Definition at line 157 of file ccafile.cpp.
|
|
Definition at line 411 of file ccafile.cpp. 00412 { 00413 00414 00415 if(pLoadContext == NULL) 00416 return 0; 00417 00418 //TRACEUSER( ME, _T("asking for %d got = %d\n"),Length,BytesAvailable); 00419 00420 //UINT32 BufferAvail = (UINT32)(IOFile->rdbuf()->in_avail()); 00421 if (pLoadContext->BytesAvailable > Length) 00422 { 00423 // TRACEUSER( ME, _T("YES Data Available !!! \n")); 00424 return TRUE ; 00425 } 00426 00427 //TRACEUSER( ME, _T("NO Data Available \n")); 00428 return FALSE; 00429 }
|
|
Definition at line 441 of file ccafile.cpp. 00442 { 00443 00444 00445 if(pLoadContext == NULL) 00446 return ; 00447 00448 // set the exit state - this will be tested in in all the wait loops 00449 pLoadContext->AsynchDownLoadComplete = TRUE; 00450 00451 TRACEUSER( "Chris", _T("###### Waking thread %x \n"),::GetCurrentThreadId()); 00452 00453 00454 // wake the read thread 00455 SetEvent(pLoadContext->hDataAvailableEvent); 00456 }
|
|
Definition at line 155 of file ccafile.h. 00155 {return pLoadContext;}
|
|
before the normal seek operation is attempted we test to see whether it is possible - if not this thread will wait until a "xsputn" wakes us -
Definition at line 196 of file ccafile.cpp. 00197 { 00198 00199 if(pLoadContext == NULL) 00200 return 0; 00201 00202 // test whether this seek can succeed and sleep if not 00203 // the write thread will wake us with a hDataAvailableEvent 00204 while(sOff>pLoadContext->TotalBytes && !pLoadContext->AsynchDownLoadComplete) 00205 { 00206 00207 TRACEUSER( ME, _T("can't seekoff - go to sleep.... \n")); 00208 00209 // note this is an auto-reset event 00210 // i.e. it will clear after this call returns... 00211 DWORD we = WaitForMultipleObjects(2,WakeEvents,FALSE,INFINITE); 00212 00213 // the EndDownLoad event - set the flag 00214 if(we == WAIT_OBJECT_0) 00215 { 00216 pLoadContext->AsynchDownLoadComplete = TRUE; 00217 TRACEUSER( "Chris", _T("...... Abort thread %x \n"),::GetCurrentThreadId()); 00218 } 00219 00220 } 00221 00222 // return the absolute file position 00223 streampos retPos = filebuf::seekoff(sOff,eDir,mode); 00224 00225 pLoadContext->BytesAvailable = pLoadContext->TotalBytes - retPos ; 00226 00227 TRACEUSER( ME, _T("seekoff to %d Bytes Available = %d\n"),sOff,pLoadContext->BytesAvailable); 00228 00229 return retPos ; 00230 00231 }
|
|
before the normal seek operation is attempted we test to see whether it is possible - if not this thread will wait until a "xsputn" wakes us -
Definition at line 245 of file ccafile.cpp. 00246 { 00247 00248 00249 if(pLoadContext == NULL) 00250 return 0; 00251 00252 00253 // test whether this seek can succeed - sleep if not 00254 // the write thread will wake us with a hDataAvailableEvent 00255 while(sPos>=pLoadContext->TotalBytes && !pLoadContext->AsynchDownLoadComplete) 00256 { 00257 00258 TRACEUSER( ME, _T("can't seekpos - go to sleep.... \n")); 00259 00260 // note this is an auto-reset event 00261 // i.e. it will clear after this call returns... 00262 DWORD we = WaitForMultipleObjects(2,WakeEvents,FALSE,INFINITE); 00263 00264 // the EndDownLoad event - set the flag 00265 if(we == WAIT_OBJECT_0) 00266 { 00267 pLoadContext->AsynchDownLoadComplete = TRUE; 00268 TRACEUSER( "Chris", _T("...... Abort thread %x \n"),::GetCurrentThreadId()); 00269 } 00270 00271 } 00272 00273 // return the absolute file position 00274 streampos retPos = filebuf::seekpos(sPos,mode); 00275 00276 pLoadContext->BytesAvailable = pLoadContext->TotalBytes - retPos; 00277 00278 TRACEUSER( ME, _T("seekpos to %d Bytes Available = %d\n"),sPos,pLoadContext->BytesAvailable); 00279 00280 return retPos; 00281 }
|
|
Definition at line 147 of file ccafile.h. 00147 { DontFail = state;};
|
|
attach a load context for this buffer (used to syb::nchorise the read and write threads) and initialise the handle array used in WaitForMulitpleObjects()
Definition at line 172 of file ccafile.cpp. 00173 { 00174 00175 pLoadContext= pLContext; 00176 WakeEvents[0] = RalphDocument::g_hAbortDownLoadEvent; 00177 WakeEvents[1] = pLoadContext->hDataAvailableEvent; 00178 00179 00180 }
|
|
the 'read' function - block if data is not available
Definition at line 334 of file ccafile.cpp. 00335 { 00336 00337 00338 if(pLoadContext == NULL) 00339 return 0; 00340 00341 /* if(pLoadContext->BytesAvailable == 0) 00342 { 00343 AfxDebugBreak(); 00344 }*/ 00345 00346 TRACEUSER( ME, _T("request %d Get - Bytes Available = %d\n"),n,pLoadContext->BytesAvailable); 00347 00348 // test whether this read can succed and sleep if not 00349 // the write thread will wake us with a hDataAvailableEvent 00350 while(!DataAvailable(n)&& !pLoadContext->AsynchDownLoadComplete) 00351 { 00352 00353 TRACEUSER( "Chris", _T("###### Waiting in thread %x \n"),::GetCurrentThreadId()); 00354 00355 // note this is an auto-reset event 00356 // i.e. it will clear after this call returns... 00357 00358 DWORD we = WaitForMultipleObjects(2,WakeEvents,FALSE,INFINITE); 00359 00360 // the EndDownLoad event - set the flag 00361 if(we == WAIT_OBJECT_0) 00362 { 00363 pLoadContext->AsynchDownLoadComplete = TRUE; 00364 TRACEUSER( "Chris", _T("...... Abort thread %x \n"),::GetCurrentThreadId()); 00365 } 00366 00367 TRACEUSER( "Chris", _T("###### Wake thread %x \n"),::GetCurrentThreadId()); 00368 00369 00370 } 00371 00372 00373 if(DontFail && pLoadContext->BytesAvailable<n) 00374 { 00375 n = pLoadContext->BytesAvailable; 00376 00377 } 00378 00379 if(pLoadContext->TotalBytes == 0) 00380 { 00381 return n; 00382 } 00383 00384 INT32 ret = filebuf::xsgetn(c,n); 00385 00386 00387 00388 TRACEUSER( ME, _T("received %d \n"),ret); 00389 00390 00391 // if(ret==1) 00392 // TRACEUSER( ME, _T("%c"),*c); 00393 00394 00395 // decrement bytes available after the read 00396 pLoadContext->BytesAvailable -= ret; 00397 00398 return ret; 00399 }
|
|
the 'write' function - wake the read thread
Definition at line 294 of file ccafile.cpp. 00295 { 00296 00297 00298 if(pLoadContext == NULL) 00299 return 0; 00300 00301 00302 //for(INT32 i=0;i<n;i++) 00303 // TRACEUSER( ME, _T("%c"),c[i]); 00304 00305 INT32 ret = filebuf::xsputn(c,n); 00306 00307 00308 // total available 00309 pLoadContext->BytesAvailable+=ret; 00310 00311 // absolute total 00312 pLoadContext->TotalBytes+=ret; 00313 00314 TRACEUSER( ME, _T("Put TotalBytes = %d BytesAvailable = %d\n"),pLoadContext->TotalBytes,pLoadContext->BytesAvailable); 00315 00316 // flush the buffer - otherwise xsgetn might try to read data that is still buffered 00317 sync(); 00318 00319 // wake the read thread 00320 SetEvent(pLoadContext->hDataAvailableEvent); 00321 00322 return ret; 00323 }
|
|
|
|
|
|
|