当前位置: 首页>>代码示例>>C++>>正文


C++ Shared::ReadBytes方法代码示例

本文整理汇总了C++中Shared::ReadBytes方法的典型用法代码示例。如果您正苦于以下问题:C++ Shared::ReadBytes方法的具体用法?C++ Shared::ReadBytes怎么用?C++ Shared::ReadBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Shared的用法示例。


在下文中一共展示了Shared::ReadBytes方法的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;
//.........这里部分代码省略.........
开发者ID:aironik,项目名称:readium-sdk,代码行数:101,代码来源:byte_stream.cpp


注:本文中的Shared::ReadBytes方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。