示例#1
0
void Channel::handleEvent()
{
    if((pollEvent_&POLLHUP)&&!(pollEvent_&POLLIN))
    {
        if(closeCallback_) closeCallback_();
    }

    if(pollEvent_&(POLLIN|POLLPRI))
    {
        if(readCallback_) readCallback_();
    }

    if(pollEvent_&POLLOUT)
    {
        if(writeCallback_) writeCallback_();
    }
}
示例#2
0
    void run() {
        unsigned int curBufferSize = 2048;
        int8_t* buf = reinterpret_cast<int8_t*>(malloc(curBufferSize));
        while (running_) {
            ReadInformation next;
            {
                boost::mutex::scoped_lock lock(mutex_);
                if (!queue_.empty()) {
                    next = queue_.front();
                    queue_.pop();
                } else {
                    signal_.wait(lock);
                    continue;
                }
            }

            if (next.offset_ == 0xFFFFFFFFu) {
                // skip this. could be e.g. a static block with no data
                next.item_->setReadComplete();
                continue;
            }

            // trying to read out of file bounds
            if (next.offset_ > fileSize_ || (next.offset_ + next.readLen_) > fileSize_) {
                LOG_WARN << "Trying to read out of file bounds in file " << path_ << ", size=" << fileSize_ << " start=" << next.offset_
                        << " len=" << next.readLen_ << std::endl;
                continue;
            }

            if (next.offset_ != stream_.tellg()) {
                stream_.seekg(next.offset_, std::ios_base::beg);
            }

            if (next.readLen_ > curBufferSize) {
                //printf("Resizing buffer to %u\n", next.readLen_ * 2);
                buf = reinterpret_cast<int8_t*>(realloc(buf, next.readLen_ * 2));
                curBufferSize = next.readLen_;
            }

            //printf("decode thread read %u bytes from %u\n", next.readLen_, next.offset_);
            stream_.read(reinterpret_cast<char*>(buf), next.readLen_);
            readCallback_(next.index_, buf, next.readLen_, next.item_, next.extra_, next.userData_);
            next.item_->setReadComplete();
        }
    }
示例#3
0
        void Channel::handleEvent()
        {
            eventHandling_ = true;
#ifndef NDEBUG
            //MDLog( "channel::handleEvent revent:%d,  %s.", revents_, reventsToString().c_str() );
#endif
            //套接字关闭了,并且没有可读数据
            if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) 
            {
                if (closeCallback_) closeCallback_();
            }
            
            if (revents_ & POLLNVAL)
            {
                MDWarning( "fd = %d Channel::handle_event() POLLNVAL", fd_ );
            }

            //套接字出错了
            if (revents_ & (POLLERR | POLLNVAL))
            {
                if (errorCallback_) errorCallback_();
            }

            //数据可读 (注意windows下不支持POLLPRI和POLLRDHUP, POLLIN表示了优先级数据和普通数据的非阻塞可读)
            if (revents_ & (POLLIN | POLLPRI | POLLRDHUP))
            {
                if (readCallback_) readCallback_();
            }

            if (revents_ & POLLOUT)
            {
                if (writeCallback_) writeCallback_();
            }

            eventHandling_ = false;
        }