00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifdef _WIN32
00015 #include <spl/configwin32.h>
00016 #include <stdio.h>
00017 #else
00018 #include <spl/autoconf/config.h>
00019 #endif
00020 #include <spl/io/StreamReadPump.h>
00021
00022 using namespace spl;
00023
00024 StreamReadPump::StreamReadPump(IStreamPtr strm, int bufsize)
00025 : m_strm(strm),
00026 m_bufsize(bufsize),
00027 m_buf(bufsize),
00028 m_eventDispatch()
00029 {
00030 ASSERT( m_strm->CanRead() );
00031 Start();
00032 }
00033
00034 StreamReadPump::~StreamReadPump()
00035 {
00036 m_running = false;
00037 m_strm->Close();
00038 free( m_buf );
00039 }
00040
00041 void StreamReadPump::Run()
00042 {
00043 m_running = true;
00044 try
00045 {
00046 while ( m_running )
00047 {
00048 int count = m_strm->Read(m_buf, 0, m_bufsize);
00049 if ( ! m_running )
00050 {
00051 continue;
00052 }
00053 if ( -1 == count )
00054 {
00055 m_eventDispatch.DispatchOnClose();
00056 m_running = false;
00057 continue;
00058 }
00059 if ( 0 == count )
00060 {
00061 throw new IOException("Read error");
00062 }
00063 m_eventDispatch.DispatchOnRead(m_buf, count);
00064 }
00065 }
00066 catch ( OutOfMemoryException mex )
00067 {
00068 m_eventDispatch.DispatchOnError(mex.Message());
00069 }
00070 catch ( Exception ex )
00071 {
00072 m_eventDispatch.DispatchOnError(ex.Message());
00073 }
00074 catch ( Exception *ex )
00075 {
00076 m_eventDispatch.DispatchOnError(ex->Message());
00077 delete ex;
00078 }
00079 m_running = false;
00080 }
00081
00082 void StreamReadPump::Stop()
00083 {
00084 m_running = false;
00085 m_strm->Close();
00086 }
00087
00088 #ifdef DEBUG
00089 void StreamReadPump::ValidateMem() const
00090 {
00091 m_strm.ValidateMem();
00092 m_strm->ValidateMem();
00093 m_buf.ValidateMem();
00094 m_eventDispatch.ValidateMem();
00095 }
00096
00097 void StreamReadPump::CheckMem() const
00098 {
00099 m_strm.CheckMem();
00100 m_strm->CheckMem();
00101 m_buf.CheckMem();
00102 m_eventDispatch.CheckMem();
00103 }
00104 #endif