采用环形缓冲队列构建异步通信系统
2020-07-14 05:45
155 查看
敬请参见独立博客页: 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(); }
转载于:https://www.cnblogs.com/openIoT/p/4182230.html
相关文章推荐
- 夜幕降临 ---- 采用区块链构建下一代通信系统的设计方案
- 通信死时间以及缓冲队列的使用对系统死时间的影响
- 采用线程池和消息队列的“伪异步”I/O模型
- 通过python和websocket构建实时通信系统[扩展saltstack监控] 推荐
- 下载基于REST、SPDY、异步IO深入规模化网络、信息安全、通信优化企业应用系统
- ZeroMQ系统的缓冲队列
- 从构建分布式秒杀系统聊聊Disruptor高性能队列
- WebService构建异构系统通信应用
- 通过python和websocket构建实时通信系统[扩展saltstack监控]
- 基于HLA的分布式卫星通信仿真系统的构建
- 操作系统课程设计 消息缓冲队列通信
- 详解如何构建Promise队列实现异步函数顺序执行
- linux下 MQ第二弹:队列管理器的配置,实现双机MQI通道异步双向通信,亲测!!
- Linux系统编程之进程间通信方式:消息队列
- UE4基础:用蓝图构建出一个GameMode中的游戏事件通信系统(7月5日 化简)
- win9再曝光! Windows 9系统采用全新方式构建测试和更新
- 构建VoIP Web callback系统 ---基于Web方式的phone2phone通信方式(1)