• 采用环形缓冲队列构建异步通信系统


    敬请参见独立博客页:  http://1.oliverwang.sinaapp.com/?p=36

    /*
    The MIT License (MIT)
    
    Copyright (c) <2014> <oliver-lxtech2013@gmail.com>
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    */
    
    #ifndef QUEUE_H
    #define QUEUE_H
    
    #include "boost/thread/mutex.hpp"
    
    /* A loop queue implementation */
    class Queue
    {
        public:
            Queue(int Size);
            virtual ~Queue();
            int AvailableRead();                                    /* Return the Available size */
            int AvailableWrite();                                   /* return the free size */
            int Pop(char *dest, int len);                           /* Pop up len's byte into dest */
            int Push(char *src, int len);                           /* Push len bytes from src into queue */
            int Read(char *dest, int len);                          /* Read */
    
        private:
            char *QueueBuf;        /* Memory pool for the queue */
            int QueueSize;         /* Queue Size */
            int queueTail;         /* Queue tail */
            int queueFront;        /* Queue front */
            boost::mutex mtx;      /* Boost mutex */
    };
    
    #endif // QUEUE_H
    /*
    The MIT License (MIT)
    
    Copyright (c) <2014> <oliver-lxtech2013@gmail.com>
    
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
    */
    
    #include "Queue.h"
    #include "assert.h"
    
    
    Queue::Queue(int Size) : QueueSize(Size)
    {
        //ctor
        QueueBuf = new char[QueueSize];           /* Get the memory pool from heap */
        queueTail = queueFront = 0;
    }
    
    Queue::~Queue()
    {
        //dtor
        delete QueueBuf;
    }
    
    /** Push len bytes into queue buffer from src.
      @param src            the source of data
      @param len            length of the data
      @return int the real length of push
    */
    int Queue::Push(char *src, int len) {
    
        assert(src);
    
        int length, ret;
        int avail_write = AvailableWrite();
        length = ret = (avail_write < len) ? avail_write : len;
        mtx.lock();
        while(length--) {
            *(QueueBuf + queueTail) = *src++;    /* queueTail is the offset */
            queueTail++;                         /* Copy one, plus one */
            if(queueTail > QueueSize)
                queueTail = 0;
        }
        mtx.unlock();
    
        return ret;
    }
    
    /** Pop len bytes from queue buffer into dest.
      @param dest           the destination of data
      @param len            length of the data
      @return int the real length of push
    */
    int Queue::Pop(char *dest, int len) {
    
        assert(dest);
    
        int length, ret;
        int avail_read  = AvailableRead();
        length = ret = (avail_read < len) ? avail_read : len;
        mtx.lock();
        while(length--) {
            *dest++ = *(QueueBuf + queueFront);
            queueFront++;
            if(queueFront > QueueSize)
                queueFront = 0;
        }
        mtx.unlock();
    
        return ret;
    }
    
    /** Return the available bytes for read
      @param  none
      @param  none
      @return The available bytes for read
    */
    int Queue::AvailableRead() {
        mtx.lock();
        int occupied = queueTail - queueFront;
        mtx.unlock();
        if(occupied < 0) {
            occupied += QueueSize;
        }
        return occupied;
    }
    
    /** Return the available bytes for write
      @param  none
      @param  none
      @return The available bytes for write
    */
    int Queue::AvailableWrite() {
        return QueueSize - AvailableRead();
    }
  • 相关阅读:
    软件测试七年之痒,依然热爱!我还是从前那个少年!
    我想从功能测试转向自动化测试,怎么办?
    python中的一些内置函数
    python中eval()
    集合
    列表的切片:取出来还是一个列表,可用在复制列表元素的操作
    字符串常用的方法
    dict字典,以及字典的一些基本应用
    list列表(也叫数组),以及常用的一些方法
    jsonpath的用法
  • 原文地址:https://www.cnblogs.com/openIoT/p/4182230.html
Copyright © 2020-2023  润新知