本文整理汇总了C++中Shared::RemoveBytes方法的典型用法代码示例。如果您正苦于以下问题:C++ Shared::RemoveBytes方法的具体用法?C++ Shared::RemoveBytes怎么用?C++ Shared::RemoveBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Shared
的用法示例。
在下文中一共展示了Shared::RemoveBytes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: InitAsyncHandler
void AsyncByteStream::InitAsyncHandler()
{
if ( _eventSource != nullptr )
throw std::logic_error("This stream is already set up for async operation.");
Weak<RingBuffer> weakReadBuf = _readbuf;
Weak<RingBuffer> weakWriteBuf = _writebuf;
_eventSource = new RunLoop::EventSource([=](RunLoop::EventSource&) {
// atomically pull out the event flags here
ThreadEvent t = _event.exchange(Wait);
bool hasRead = false, hasWritten = false;
uint8_t buf[4096];
Shared<RingBuffer> readBuf = weakReadBuf.lock();
Shared<RingBuffer> writeBuf = weakWriteBuf.lock();
if ( (t & ReadSpaceAvailable) == ReadSpaceAvailable && readBuf )
{
std::lock_guard<RingBuffer> _(*readBuf);
size_type read = this->read_for_async(buf, readBuf->SpaceAvailable());
if ( read != 0 )
{
readBuf->WriteBytes(buf, read);
hasRead = true;
}
}
if ( (t & DataToWrite) == DataToWrite && writeBuf )
{
std::lock_guard<RingBuffer> _(*writeBuf);
size_type written = writeBuf->ReadBytes(buf, writeBuf->BytesAvailable());
written = this->write_for_async(buf, written);
if ( written != 0 )
{
// only remove as much as actually went out
writeBuf->RemoveBytes(written);
hasWritten = true;
}
}
auto invocation = [this, hasRead, hasWritten] () {
if ( hasRead )
_eventHandler(AsyncEvent::HasBytesAvailable, this);
if ( hasWritten )
_eventHandler(AsyncEvent::HasSpaceAvailable, this);
};
if ( _targetRunLoop != nullptr )
{
_targetRunLoop->PerformFunction(invocation);
}
else
{
invocation();
}
});
if ( _asyncRunLoop == nullptr )
{
std::mutex __mut;
std::condition_variable __inited;
std::unique_lock<std::mutex> __lock(__mut);
_asyncIOThread = std::thread([&](){
AsyncByteStream::_asyncRunLoop = RunLoop::CurrentRunLoop();
{
std::unique_lock<std::mutex> __(__mut);
__inited.notify_all();
}
// now run the run loop
// only spin an empty run loop a certain amount of time before giving up
// and exiting the thread entirely
// FIXME: There's a gap here where a race could lose an EventSource addition
static constexpr unsigned kMaxEmptyTicks(1000);
static constexpr std::chrono::milliseconds kTickLen(10);
unsigned __emptyTickCounter = 0;
do
{
RunLoop::ExitReason __r = RunLoop::CurrentRunLoop()->Run(true, std::chrono::seconds(20));
if ( __r == RunLoop::ExitReason::RunFinished )
{
if ( ++__emptyTickCounter == kMaxEmptyTicks )
break; // exit the thread
// wait a bit and try again
std::this_thread::sleep_for(kTickLen);
}
// by definition not an empty runloop
__emptyTickCounter = 0;
} while (1);
// nullify the global before we quit
// deletion isn't necessary, it's done by TLS in run_loop.cpp
_asyncRunLoop = nullptr;
//.........这里部分代码省略.........