mirror of
https://github.com/debauchee/barrier.git
synced 2024-12-23 11:02:21 +03:00
ad15393732
of polling/sleeping. changed CTCPSocket to not use thread cancellation but to instead use m_connected to exit the thread. also shortened poll timeout.
79 lines
1.3 KiB
C++
79 lines
1.3 KiB
C++
#include "CBufferedOutputStream.h"
|
|
#include "CLock.h"
|
|
#include "CMutex.h"
|
|
#include "CThread.h"
|
|
#include "IJob.h"
|
|
#include "XIO.h"
|
|
#include <assert.h>
|
|
|
|
//
|
|
// CBufferedOutputStream
|
|
//
|
|
|
|
CBufferedOutputStream::CBufferedOutputStream(CMutex* mutex, IJob* closeCB) :
|
|
m_mutex(mutex),
|
|
m_closeCB(closeCB),
|
|
m_empty(mutex, true),
|
|
m_closed(false)
|
|
{
|
|
assert(m_mutex != NULL);
|
|
}
|
|
|
|
CBufferedOutputStream::~CBufferedOutputStream()
|
|
{
|
|
delete m_closeCB;
|
|
}
|
|
|
|
const void* CBufferedOutputStream::peek(UInt32 n)
|
|
{
|
|
return m_buffer.peek(n);
|
|
}
|
|
|
|
void CBufferedOutputStream::pop(UInt32 n)
|
|
{
|
|
m_buffer.pop(n);
|
|
if (m_buffer.getSize() == 0) {
|
|
m_empty.broadcast();
|
|
}
|
|
}
|
|
|
|
UInt32 CBufferedOutputStream::getSize() const
|
|
{
|
|
return m_buffer.getSize();
|
|
}
|
|
|
|
void CBufferedOutputStream::close()
|
|
{
|
|
CLock lock(m_mutex);
|
|
if (m_closed) {
|
|
throw XIOClosed();
|
|
}
|
|
|
|
m_closed = true;
|
|
m_buffer.pop(m_buffer.getSize());
|
|
if (m_closeCB) {
|
|
m_closeCB->run();
|
|
}
|
|
}
|
|
|
|
UInt32 CBufferedOutputStream::write(
|
|
const void* data, UInt32 n)
|
|
{
|
|
CLock lock(m_mutex);
|
|
if (m_closed) {
|
|
throw XIOClosed();
|
|
}
|
|
|
|
m_buffer.write(data, n);
|
|
return n;
|
|
}
|
|
|
|
void CBufferedOutputStream::flush()
|
|
{
|
|
// wait until all data is written
|
|
CLock lock(m_mutex);
|
|
while (m_buffer.getSize() > 0) {
|
|
m_empty.wait();
|
|
}
|
|
}
|