首页 > 代码库 > 网络收发之cycleBuf
网络收发之cycleBuf
1 #pragma once 2 3 #include <iostream> 4 #include <string> 5 6 class cyclebuffer 7 { 8 protected: 9 volatile int32_t m_nReadIndex; 10 volatile int32_t m_nWriteIndex; 11 volatile int32_t m_nDataSize; //有效数据大小 12 volatile int32_t m_nBufSize; //总buf大小 13 uint_8 *m_pBuf; 14 15 public: 16 //剩余空间数 17 int32_t Space() 18 { 19 return m_nBufSize-m_nDataSize; 20 } 21 22 //获取buf的总大小 23 int32_t Capatity() 24 { 25 return m_nBufSize; 26 } 27 28 //获取有效数据长度 29 int32_t Size() 30 { 31 return m_nDataSize; 32 } 33 //清空 34 void Reset() 35 { 36 m_nReadIndex = 0; 37 m_nwriteIndex = 0; 38 m_nDataSize = 0; 39 } 40 41 //增加nSize个字节 42 int32_t Grow(int32_t nSize) 43 { 44 45 } 46 47 //写到写列表的头部---针对新数据 48 int32_t WriteToHead(const uint8_t *pBuf, int32_t nDataSize) 49 { 50 if(nDataSize < 0 || pBuf == NULL) 51 { 52 return 0; 53 } 54 55 //空间不够 56 if(m_nDataSize + nDataSize > m_nBufsize) 57 { 58 if(Grow((m_nDataSize+nDataSize) - m_nDataSize) <= 0) 59 { 60 return 0; 61 } 62 } 63 64 if(m_nWriteIndex < m_nReadIndex) 65 { 66 memcpy(&m_pBuf[m_nReadIndex-nDataSize], pBuf, nDataSize); 67 m_nReadIndex -= nDataSize; 68 } 69 else if(m_nWriteIndex == m_nReadIndex) 70 { 71 return Write(pBuf, nDataSize); 72 } 73 else 74 { 75 int32_t nLeftDataSize = (nDataSize-m_nReadIndex<0)?nDataSize:m_nReadIndex; 76 if(nLeftDataSize < nDataSize) //此时nLeftDataSize == m_nReadIndex,m_nReadIndex前面的空间不够 77 { 78 int32_t nTailDataSize = nDataSize - nLeftDataSize; 79 int32_t nWriteIndex = m_nBufSize - nTailDatasize; 80 memcpy(&m_pBuf[nWriteIndex], pBuf, nDataSize-nLeftDataSize); //从内存最后向前写 81 memcpy(&m_pBuf[0], pBuf+nTailDataSize, nLeftDataSize); 82 m_nReadIndex = nWriteIndex; 83 } 84 else 85 { 86 if(m_nReadIndex-nDataSize < 0) 87 { 88 return 0; 89 } 90 memcpy(&m_pBuf[m_nReadIndex-nDataSize], pBuf, nDataSize); 91 m_nReadIndex -= nDataSize; 92 } 93 } 94 m_nDataSize += nDataSize; 95 return nDataSize; 96 } 97 98 //写到列表的尾部 99 int32_t Write(const uint8_t *pBuf, int32_t nDataSize)100 {101 if(nDataSize <= 0 || NULL == pBuf)102 {103 return 0;104 }105 106 if(nDataSize + m_nDataSize > m_nBufSize)107 {108 if(Grow((nDataSize+nBufSize)-m_nDataSize) <= 0)109 {110 return 0;111 }112 }113 114 if(m_nWriteIndex < m_nReadIndex)115 {116 memcpy(&m_pBuf[m_nWriteIndex], pBuf, nDataSize);117 m_nWriteIndex += nDataSize;118 }119 else120 {121 int32_t nLeftDataSize = m_nBufSize - m_nWriteIndex;122 if(nLeftDataSize < nDataSize)123 {124 {125 memcpy(&m_pBuf[m_nWriteIndex], pBuf, nLeftDataSize);126 memcpy(&m_pBuf[0], pBuf+nLeftDataSize, nDataSize-nLeftDataSize);127 m_nwriteIndex = nDataSize - nLeftDataSize;128 }129 else130 {131 memcpy(&m_pBuf[m_nWriteIndex], pBuf, nDataSize);132 m_nWriteIndex += nDataSize;133 }134 }135 m_nDataSize += nDataSize;136 return nDataSize;137 }138 139 //读取 读列表的头部内存140 int32_t Read(uint8_t *pBuf, const int32_t nWantSize)141 {142 if(nWantSize <= 0 || NULL == pBuf)143 {144 return 0;145 }146 147 int32_t nDataSize = ((m_nDataSize < nWantSize)?m_nDataSize : nWantSize);148 if(nDataSize<=0)149 {150 return 0;151 }152 if(m_nReadIndex < m_nWriteIndex)153 {154 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize);155 m_nReadIndex += nDataSize;156 }157 else158 {159 int32_t nLeftDataSize = m_nBufSize - m_nReadIndex;160 if(nLeftDataSize < nDataSize)161 {162 memcpy(pBuf, &m_pBuf[m_nReadIndex], nLeftDataSize);163 memcpy(pBuf+nLeftDataSize, &m_pBuf[0], nDataSize-nLeftDataSize);164 m_nReadIndex = nDataSize-nLeftDataSize;165 }166 else167 {168 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize);169 m_nReadIndex += nDataSize;170 }171 }172 m_nDataSize -= nDataSize;173 return nDataSize;174 }175 176 177 //读取数据但是不修改读索引178 int32_t PeekRead(uint8_t *pBuf, const int32_t nWantSize)179 {180 if(nWantSize <= 0 || pBuf == NULL)181 {182 return 0;183 }184 185 int32_t nDataSize = ((m_nDataSize < nWantSize) ? m_nDataSize : nWantSize);186 if(m_nReadIndex < m_nWriteIndex)187 {188 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize);189 }190 else if(m_nReadIndex == m_nWriteIndex)191 {192 return 0;193 }194 else195 {196 int32_t nLeftDataSize = m_nBufSize - m_nReadIndex;197 if(nLeftDataSize < nDataSize)198 {199 memcpy(pBuf, &m_pBuf[m_nReadIndex], nLeftDataSize);200 memcpy(pBuf+nLeftDataSize, &m_pBuf[0], nDataSize-nLeftDataSize);201 }202 else203 {204 memcpy(pBuf, &m_pBuf[m_nReadIndex], nDataSize);205 }206 }207 return nDataSize;208 }209 210 };
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。