5 #include "StreamBinder.h"
6 #include "../../Common/Defs.h"
7 #include "../../Common/MyCom.h"
9 using namespace NWindows;
10 using namespace NSynchronization;
12 class CSequentialInStreamForBinder:
13 public ISequentialInStream,
19 STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
21 CStreamBinder *m_StreamBinder;
23 ~CSequentialInStreamForBinder() { m_StreamBinder->CloseRead(); }
24 void SetBinder(CStreamBinder *streamBinder) { m_StreamBinder = streamBinder; }
27 STDMETHODIMP CSequentialInStreamForBinder::Read(void *data, UInt32 size, UInt32 *processedSize)
28 { return m_StreamBinder->Read(data, size, processedSize); }
30 class CSequentialOutStreamForBinder:
31 public ISequentialOutStream,
37 STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
40 CStreamBinder *m_StreamBinder;
42 ~CSequentialOutStreamForBinder() { m_StreamBinder->CloseWrite(); }
43 void SetBinder(CStreamBinder *streamBinder) { m_StreamBinder = streamBinder; }
46 STDMETHODIMP CSequentialOutStreamForBinder::Write(const void *data, UInt32 size, UInt32 *processedSize)
47 { return m_StreamBinder->Write(data, size, processedSize); }
50 //////////////////////////
52 // (_thereAreBytesToReadEvent && _bufferSize == 0) means that stream is finished.
54 HRes CStreamBinder::CreateEvents()
56 RINOK(_allBytesAreWritenEvent.Create(true));
57 RINOK(_thereAreBytesToReadEvent.Create());
58 return _readStreamIsClosedEvent.Create();
61 void CStreamBinder::ReInit()
63 _thereAreBytesToReadEvent.Reset();
64 _readStreamIsClosedEvent.Reset();
70 void CStreamBinder::CreateStreams(ISequentialInStream **inStream,
71 ISequentialOutStream **outStream)
73 CSequentialInStreamForBinder *inStreamSpec = new
74 CSequentialInStreamForBinder;
75 CMyComPtr<ISequentialInStream> inStreamLoc(inStreamSpec);
76 inStreamSpec->SetBinder(this);
77 *inStream = inStreamLoc.Detach();
79 CSequentialOutStreamForBinder *outStreamSpec = new
80 CSequentialOutStreamForBinder;
81 CMyComPtr<ISequentialOutStream> outStreamLoc(outStreamSpec);
82 outStreamSpec->SetBinder(this);
83 *outStream = outStreamLoc.Detach();
90 HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize)
92 UInt32 sizeToRead = size;
95 RINOK(_thereAreBytesToReadEvent.Lock());
96 sizeToRead = MyMin(_bufferSize, size);
99 MoveMemory(data, _buffer, sizeToRead);
100 _buffer = ((const Byte *)_buffer) + sizeToRead;
101 _bufferSize -= sizeToRead;
102 if (_bufferSize == 0)
104 _thereAreBytesToReadEvent.Reset();
105 _allBytesAreWritenEvent.Set();
109 if (processedSize != NULL)
110 *processedSize = sizeToRead;
111 ProcessedSize += sizeToRead;
115 void CStreamBinder::CloseRead()
117 _readStreamIsClosedEvent.Set();
120 HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSize)
126 _allBytesAreWritenEvent.Reset();
127 _thereAreBytesToReadEvent.Set();
130 events[0] = _allBytesAreWritenEvent;
131 events[1] = _readStreamIsClosedEvent;
132 DWORD waitResult = ::WaitForMultipleObjects(2, events, FALSE, INFINITE);
133 if (waitResult != WAIT_OBJECT_0 + 0)
135 // ReadingWasClosed = true;
138 // if(!_allBytesAreWritenEvent.Lock())
141 if (processedSize != NULL)
142 *processedSize = size;
146 void CStreamBinder::CloseWrite()
148 // _bufferSize must be = 0
149 _thereAreBytesToReadEvent.Set();