asynchstreambuf Class Reference

an asynch version of streambuf It is assumed that two versions of the file will be open i.e. read and write. The read and write will run in seperate threads. Any access to the file that might fail due to lack of data will cause the read thread to sleep until more data is available. It is assumed that the write thread will never seek - this would corrupt the BytesAvailable count. More...

#include <ccafile.h>

List of all members.

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()
LoadContextGetLoadContext () const

Private Member Functions

BOOL DataAvailable (INT32 Length)

Private Attributes

LoadContextpLoadContext
wxCondition * WakeEvents [2]
BOOL DontFail


Detailed Description

an asynch version of streambuf It is assumed that two versions of the file will be open i.e. read and write. The read and write will run in seperate threads. Any access to the file that might fail due to lack of data will cause the read thread to sleep until more data is available. It is assumed that the write thread will never seek - this would corrupt the BytesAvailable count.

Author:
Chris_Parks (Xara Group Ltd) <camelotdev@xara.com>
Date:
5/8/96
See also:
streambuf; ios; costream

Definition at line 126 of file ccafile.h.


Constructor & Destructor Documentation

asynchstreambuf::asynchstreambuf  ) 
 

constructor

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96

Definition at line 139 of file ccafile.cpp.

00140 {   
00141     
00142     
00143     DontFail = FALSE;
00144 }

asynchstreambuf::~asynchstreambuf  ) 
 

destructor - clean up event and critical section

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96

Definition at line 157 of file ccafile.cpp.

00158 {
00159     
00160 }


Member Function Documentation

BOOL asynchstreambuf::DataAvailable INT32  Length  )  [private]
 

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96

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 }

void asynchstreambuf::DownloadHasEnded HRESULT  Status  ) 
 

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 }

LoadContext* asynchstreambuf::GetLoadContext  )  const [inline]
 

Definition at line 155 of file ccafile.h.

00155 {return pLoadContext;}

streampos asynchstreambuf::seekoff streamoff  sOff,
ios::seekdir  eDir,
INT32  mode = ios::in|ios::out
[virtual]
 

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 -

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96
Parameters:
Standard streambuf [INPUTS]

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 }

streampos asynchstreambuf::seekpos streampos  sPos,
INT32  mode = ios::in|ios::out
[virtual]
 

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 -

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96
Parameters:
Standard streambuf [INPUTS]

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 }

void asynchstreambuf::SetDontFail BOOL  state  )  [inline]
 

Definition at line 147 of file ccafile.h.

00147 { DontFail = state;};

void asynchstreambuf::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()

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96

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 }

INT32 asynchstreambuf::xsgetn char *  c,
INT32  n
[virtual]
 

the 'read' function - block if data is not available

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96

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 }

INT32 asynchstreambuf::xsputn const char *  c,
INT32  n
[virtual]
 

the 'write' function - wake the read thread

Author:
Chris_Snook (Xara Group Ltd) <camelotdev@xara.com>
Date:
21/08/96

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 }


Member Data Documentation

BOOL asynchstreambuf::DontFail [private]
 

Definition at line 140 of file ccafile.h.

LoadContext* asynchstreambuf::pLoadContext [private]
 

Definition at line 134 of file ccafile.h.

wxCondition* asynchstreambuf::WakeEvents[2] [private]
 

Definition at line 136 of file ccafile.h.


The documentation for this class was generated from the following files:
Generated on Sat Nov 10 03:50:03 2007 for Camelot by  doxygen 1.4.4