📄 circularresendbuffer.cpp
字号:
// File Name: CircularResendBuffer.cpp///////////////////////////////////////////////////#include <stdlib.h>#include "CircularResendBuffer.h"// We have two choices for using the data within the buffer// 1. copy data out (using read), which is safe but with low efficiency// 2. return a pointer for the caller to re-use this block of data// which is "fast" but we have to make sure the data won't become dirty//// For this "resending" buffer, since the "resend_pos" won't change// before we get the positive ack for "this" message, the data// won't be overwritten unexpectedlyCircularResendBuffer::CircularResendBuffer(int _size) : buffer(NULL), bufferSize(_size), write_pos(0), read_pos(0), confirmed_pos(-1){ buffer = (unsigned char *) malloc( bufferSize );}int CircularResendBuffer::reSize(int _newCount){ unsigned char * buf = (unsigned char *) malloc( _newCount ); if( buf ) { bufferSize = _newCount; free( buffer ); buffer = buf; } else { return -1; // failed without any change } return 0;}CircularResendBuffer::~CircularResendBuffer(void){ if( buffer ) free( buffer );}int CircularResendBuffer::read(unsigned char * _buffer, int _count){ if( write_pos == read_pos ) { return -1; // no data } else if( getStoredBytesNumber() < _count ) { return -2; // not enough data } // now, we have enough data for reading else if( write_pos > read_pos ) { memcpy( _buffer, buffer + read_pos, _count ); read_pos += _count; } else // write_pos < read_pos { if( bufferSize - read_pos >= _count ) { memcpy( _buffer, buffer + read_pos, _count ); read_pos = (read_pos + _count) % bufferSize; } else { memcpy( _buffer, buffer + read_pos, bufferSize - read_pos ); memcpy( _buffer + bufferSize - read_pos, buffer, _count - (bufferSize - read_pos) ); read_pos = _count - (bufferSize - read_pos); } } return 0;}int CircularResendBuffer::write(unsigned char * _buffer, int _count){ if( getFreeBytesNumber() < _count ) { return -3; // not enough space } // now, we have enough space for writting else if( write_pos > read_pos ) { if( bufferSize - write_pos >= _count ) { memcpy( buffer + write_pos, _buffer, _count ); write_pos = (write_pos + _count) % bufferSize; } else { memcpy( buffer + write_pos, _buffer, bufferSize - write_pos ); memcpy( buffer, _buffer + (bufferSize - write_pos), _count - (bufferSize - write_pos) ); write_pos = _count - (bufferSize - write_pos); } } else // write_pos < read_pos { memcpy( buffer + write_pos, _buffer, _count ); write_pos += _count; } return 0;}int CircularResendBuffer::getFreeBytesNumber(){ if( write_pos == read_pos ) { return bufferSize; // no data } else if( write_pos > read_pos ) { return bufferSize - (write_pos - read_pos); } else // write_pos < read_pos { return read_pos - write_pos; }}int CircularResendBuffer::getStoredBytesNumber(){ if( write_pos == read_pos ) { return 0; // no data } else if( write_pos > read_pos ) { return write_pos - read_pos; } else // write_pos < read_pos { return (bufferSize - read_pos) + write_pos; }}int CircularResendBuffer::getBytesForSend(){ return 0;}unsigned char * CircularResendBuffer::getSentMsgPtr(){ return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -