Last active
September 8, 2016 10:51
-
-
Save vovaprog/e053c8e728718cce0edc21d31a2a89c4 to your computer and use it in GitHub Desktop.
ring buffer to use with sockets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef TRANSFER_RING_BUFFER_H | |
#define TRANSFER_RING_BUFFER_H | |
class TransferRingBuffer | |
{ | |
public: | |
TransferRingBuffer() = default; | |
TransferRingBuffer(int size): bufSize(size) | |
{ | |
buf = new char[size]; | |
} | |
~TransferRingBuffer() | |
{ | |
if(buf != nullptr) | |
{ | |
delete[] buf; | |
} | |
} | |
TransferRingBuffer(const TransferRingBuffer &tm) = delete; | |
TransferRingBuffer(TransferRingBuffer &&tm) = delete; | |
TransferRingBuffer& operator=(const TransferRingBuffer &tm) = delete; | |
TransferRingBuffer& operator=(TransferRingBuffer && tm) = delete; | |
void init(int newSize) | |
{ | |
if(newSize != bufSize) | |
{ | |
if(buf != nullptr) | |
{ | |
delete[] buf; | |
buf = nullptr; | |
} | |
bufSize = newSize; | |
buf = new char[bufSize]; | |
} | |
} | |
bool startWrite(void* &data, int &size) | |
{ | |
if(writeHead == bufSize) | |
{ | |
if(readHead == 0) | |
{ | |
return false; | |
} | |
else | |
{ | |
writeHead = 0; | |
} | |
} | |
data = buf + writeHead; | |
if(writeHead >= readHead) | |
{ | |
size = bufSize - writeHead; | |
} | |
else | |
{ | |
size = readHead - writeHead - 1; | |
} | |
return (size > 0); | |
} | |
void endWrite(int size) | |
{ | |
writeHead += size; | |
} | |
bool startRead(void* &data, int &size) | |
{ | |
data = buf + readHead; | |
if(readHead < writeHead) | |
{ | |
size = writeHead - readHead; | |
} | |
else if(readHead > writeHead) | |
{ | |
size = bufSize - readHead; | |
} | |
else | |
{ | |
size = 0; | |
} | |
return (size > 0); | |
} | |
void endRead(int size) | |
{ | |
readHead += size; | |
if(readHead == bufSize) | |
{ | |
readHead = 0; | |
if(writeHead == bufSize) | |
{ | |
writeHead = 0; | |
} | |
} | |
} | |
#ifdef TRANSFER_RING_BUFFER_DEBUG | |
void printInfo() | |
{ | |
printf("writeHead: %d readHead: %d\n", writeHead, readHead); | |
} | |
#endif | |
protected: | |
char *buf = nullptr; | |
int readHead = 0, writeHead = 0, bufSize = 0; | |
}; | |
#endif // TRANSFER_RING_BUFFER_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment