mirror of
https://github.com/Xevion/easy7zip.git
synced 2025-12-08 04:07:02 -06:00
21.02
This commit is contained in:
@@ -14,7 +14,7 @@ class CBinderInStream:
|
||||
public:
|
||||
MY_UNKNOWN_IMP1(ISequentialInStream)
|
||||
STDMETHOD(Read)(void *data, UInt32 size, UInt32 *processedSize);
|
||||
~CBinderInStream() { _binder->CloseRead(); }
|
||||
~CBinderInStream() { _binder->CloseRead_CallOnce(); }
|
||||
CBinderInStream(CStreamBinder *binder): _binder(binder) {}
|
||||
};
|
||||
|
||||
@@ -37,19 +37,24 @@ STDMETHODIMP CBinderOutStream::Write(const void *data, UInt32 size, UInt32 *proc
|
||||
{ return _binder->Write(data, size, processedSize); }
|
||||
|
||||
|
||||
|
||||
WRes CStreamBinder::CreateEvents()
|
||||
static HRESULT Event__Create_or_Reset(NWindows::NSynchronization::CAutoResetEvent &event)
|
||||
{
|
||||
RINOK(_canWrite_Event.Create());
|
||||
RINOK(_canRead_Event.Create());
|
||||
return _readingWasClosed_Event.Create();
|
||||
WRes wres;
|
||||
if (event.IsCreated())
|
||||
wres = event.Reset();
|
||||
else
|
||||
wres = event.Create();
|
||||
return HRESULT_FROM_WIN32(wres);
|
||||
}
|
||||
|
||||
void CStreamBinder::ReInit()
|
||||
HRESULT CStreamBinder::Create_ReInit()
|
||||
{
|
||||
_canWrite_Event.Reset();
|
||||
_canRead_Event.Reset();
|
||||
_readingWasClosed_Event.Reset();
|
||||
RINOK(Event__Create_or_Reset(_canRead_Event));
|
||||
// RINOK(Event__Create_or_Reset(_canWrite_Event));
|
||||
|
||||
_canWrite_Semaphore.Close();
|
||||
// we need at least 3 items of maxCount: 1 for normal unlock in Read(), 2 items for unlock in CloseRead_CallOnce()
|
||||
_canWrite_Semaphore.Create(0, 3);
|
||||
|
||||
// _readingWasClosed = false;
|
||||
_readingWasClosed2 = false;
|
||||
@@ -59,27 +64,14 @@ void CStreamBinder::ReInit()
|
||||
_buf = NULL;
|
||||
ProcessedSize = 0;
|
||||
// WritingWasCut = false;
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
|
||||
void CStreamBinder::CreateStreams(ISequentialInStream **inStream, ISequentialOutStream **outStream)
|
||||
void CStreamBinder::CreateStreams2(CMyComPtr<ISequentialInStream> &inStream, CMyComPtr<ISequentialOutStream> &outStream)
|
||||
{
|
||||
// _readingWasClosed = false;
|
||||
_readingWasClosed2 = false;
|
||||
|
||||
_waitWrite = true;
|
||||
_bufSize = 0;
|
||||
_buf = NULL;
|
||||
ProcessedSize = 0;
|
||||
// WritingWasCut = false;
|
||||
|
||||
CBinderInStream *inStreamSpec = new CBinderInStream(this);
|
||||
CMyComPtr<ISequentialInStream> inStreamLoc(inStreamSpec);
|
||||
*inStream = inStreamLoc.Detach();
|
||||
|
||||
CBinderOutStream *outStreamSpec = new CBinderOutStream(this);
|
||||
CMyComPtr<ISequentialOutStream> outStreamLoc(outStreamSpec);
|
||||
*outStream = outStreamLoc.Detach();
|
||||
inStream = new CBinderInStream(this);
|
||||
outStream = new CBinderOutStream(this);
|
||||
}
|
||||
|
||||
// (_canRead_Event && _bufSize == 0) means that stream is finished.
|
||||
@@ -92,7 +84,9 @@ HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize)
|
||||
{
|
||||
if (_waitWrite)
|
||||
{
|
||||
RINOK(_canRead_Event.Lock());
|
||||
WRes wres = _canRead_Event.Lock();
|
||||
if (wres != 0)
|
||||
return HRESULT_FROM_WIN32(wres);
|
||||
_waitWrite = false;
|
||||
}
|
||||
if (size > _bufSize)
|
||||
@@ -105,17 +99,25 @@ HRESULT CStreamBinder::Read(void *data, UInt32 size, UInt32 *processedSize)
|
||||
if (processedSize)
|
||||
*processedSize = size;
|
||||
_bufSize -= size;
|
||||
|
||||
/*
|
||||
if (_bufSize == 0), then we have read whole buffer
|
||||
we have two ways here:
|
||||
- if we check (_bufSize == 0) here, we unlock Write only after full data Reading - it reduces the number of syncs
|
||||
- if we don't check (_bufSize == 0) here, we unlock Write after partial data Reading
|
||||
*/
|
||||
if (_bufSize == 0)
|
||||
{
|
||||
_waitWrite = true;
|
||||
_canRead_Event.Reset();
|
||||
_canWrite_Event.Set();
|
||||
// _canWrite_Event.Set();
|
||||
_canWrite_Semaphore.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
|
||||
HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSize)
|
||||
{
|
||||
if (processedSize)
|
||||
@@ -135,20 +137,20 @@ HRESULT CStreamBinder::Write(const void *data, UInt32 size, UInt32 *processedSiz
|
||||
_readingWasClosed2 = true;
|
||||
*/
|
||||
|
||||
HANDLE events[2] = { _canWrite_Event, _readingWasClosed_Event };
|
||||
DWORD waitResult = ::WaitForMultipleObjects(2, events, FALSE, INFINITE);
|
||||
if (waitResult >= WAIT_OBJECT_0 + 2)
|
||||
return E_FAIL;
|
||||
_canWrite_Semaphore.Lock();
|
||||
|
||||
// _bufSize : is remain size that was not read
|
||||
size -= _bufSize;
|
||||
|
||||
// size : is size of data that was read
|
||||
if (size != 0)
|
||||
{
|
||||
// if some data was read, then we report that size and return
|
||||
if (processedSize)
|
||||
*processedSize = size;
|
||||
return S_OK;
|
||||
}
|
||||
// if (waitResult == WAIT_OBJECT_0 + 1)
|
||||
_readingWasClosed2 = true;
|
||||
_readingWasClosed2 = true;
|
||||
}
|
||||
|
||||
// WritingWasCut = true;
|
||||
|
||||
Reference in New Issue
Block a user