Augmented Socket::Buffer features.

This commit is contained in:
Thulinma 2013-03-12 16:19:46 +01:00
parent 4e16982929
commit 97b51ff39a
2 changed files with 27 additions and 0 deletions

View file

@ -32,6 +32,18 @@ unsigned int Socket::Buffer::size(){
return data.size();
}
/// Returns either the amount of total bytes available in the buffer or max, whichever is smaller.
unsigned int Socket::Buffer::bytes(unsigned int max){
unsigned int i = 0;
for (std::deque<std::string>::iterator it = data.begin(); it != data.end(); ++it){
i += ( *it).size();
if (i >= max){
return max;
}
}
return i;
}
/// Appends this string to the internal std::deque of std::string objects.
/// It is automatically split every BUFFER_BLOCKSIZE bytes.
void Socket::Buffer::append(const std::string & newdata){
@ -62,6 +74,18 @@ void Socket::Buffer::append(const char * newdata, const unsigned int newdatasize
}
}
/// Prepends this data block to the internal std::deque of std::string objects.
/// It is _not_ automatically split every BUFFER_BLOCKSIZE bytes.
void Socket::Buffer::prepend(const std::string & newdata){
data.push_back(newdata);
}
/// Prepends this data block to the internal std::deque of std::string objects.
/// It is _not_ automatically split every BUFFER_BLOCKSIZE bytes.
void Socket::Buffer::prepend(const char * newdata, const unsigned int newdatasize){
data.push_back(std::string(newdata, (size_t)newdatasize));
}
/// Returns true if at least count bytes are available in this buffer.
bool Socket::Buffer::available(unsigned int count){
unsigned int i = 0;

View file

@ -30,8 +30,11 @@ namespace Socket {
std::deque<std::string> data;
public:
unsigned int size();
unsigned int bytes(unsigned int max);
void append(const std::string & newdata);
void append(const char * newdata, const unsigned int newdatasize);
void prepend(const std::string & newdata);
void prepend(const char * newdata, const unsigned int newdatasize);
std::string & get();
bool available(unsigned int count);
std::string remove(unsigned int count);