Protocols::Generics::TcpSocketBuffer Class Reference

#include <TcpSocketBuffer.h> [code]

Inherits Protocols::Generics::SocketBuffer.

Inheritance diagram for Protocols::Generics::TcpSocketBuffer:

Inheritance graph
[legend]
Collaboration diagram for Protocols::Generics::TcpSocketBuffer:

Collaboration graph
[legend]
List of all members.

Detailed Description

Interface for a buffer, which SocketTransport could use.

Depending on the type of socket (stream or datagram) that is used by a SocketTransport object different buffering strategies should be applied. In some case, in order to abstract the differences between stream and datagram transports, some modifications of the buffered data (e.g. adding a length field to the header when the transport is TCP, see eDonkey).

Todo:
What about peek() and variants?
Todo:
Add '_' postfix to all private data variables, like readBuffer_.

Definition at line 45 of file TcpSocketBuffer.h.

Public Member Functions

 TcpSocketBuffer (int readBufferSize, int writeBufferSize)
 Constructs a TcpSocketBuffer object.
 ~TcpSocketBuffer ()
 Destructs an object.
bool canRead (int count) const
 Tells whether count bytes can be read from the read buffer.
QByteArray peek (int count) const
 Peeks count bytes from the read buffer.
QByteArray peekAtMost (int count) const
 Peeks at most count bytes from the read buffer.
QByteArray peekTo (const QByteArray &delimiter) const
 Peeks from the buffer all bytes up to and including delimiter.
QByteArray peekAll () const
 Peeks all bytes from the read buffer.
QByteArray read (int count)
 Reads count bytes from the read buffer.
QByteArray readAtMost (int count)
 Reads at most count bytes from the read buffer.
QByteArray readTo (const QByteArray &delimiter)
 Extracts from the buffer all bytes up to and including delimiter.
QByteArray readAll ()
 Extracts and returns all bytes from the read buffer.
bool canWrite (int count) const
 Tells whether count number of bytes can be buffered in the write buffer.
bool write (const QByteArray &bytes, bool flush=true)
 Writes bytes to the write buffer.
void tryReadFrom (Socket *socket)
 Tries to read from the socket to the read buffer.
void tryWriteTo (Socket *socket)
 Tries to write from the buffered write buffer to socket.

Private Member Functions

CALITKO_TESTABLE TcpSocketBuffer (const TcpSocketBuffer &)
TcpSocketBufferoperator= (const TcpSocketBuffer &)
void peek (char *destination, int size) const
 Copies the first bytes from the read buffer in outBytes.
void purgeReadBuffer (int count)
 Purges the first count bytes from the read buffer.
void setReadBufferSize (int size)
 Resets the read buffer size and all previous data is lost.
int readBufferSize () const
int bytesToRead () const
char * nextReadChunk () const
int nextReadChunkSize () const
void hasRead (int count)
 Adds count bytes to the end of the read buffer.
void write (const char *source, int size, bool flush)
 Buffers size bytes from source in the write buffer.
void setWriteBufferSize (int size)
 Resets the write buffer size and all previous data is lost.
int writeBufferSize () const
int bytesToWrite () const
char * nextWriteChunk () const
int nextWriteChunkSize () const
void hasWritten (int count)
 Remove count bytes from the beginning of the write buffer.

Private Attributes

char * readBuffer
int readSize
int readStart
int readEnd
char * writeBuffer
int writeSize
int writeStart
int writeEnd
int writeFlushEnd


Constructor & Destructor Documentation

CALITKO_TESTABLE Protocols::Generics::TcpSocketBuffer::TcpSocketBuffer const TcpSocketBuffer  )  [private]
 

TcpSocketBuffer::TcpSocketBuffer int  readBufferSize,
int  writeBufferSize
 

Constructs a TcpSocketBuffer object.

Parameters:
readBufferSize is the size to allocate for the read buffer.
writeBufferSize is the size to allocate for the write buffer.
Precondition:
readBufferSize and writeBufferSize are not negative.

Definition at line 35 of file TcpSocketBuffer.cpp.

TcpSocketBuffer::~TcpSocketBuffer  ) 
 

Destructs an object.

Definition at line 48 of file TcpSocketBuffer.cpp.


Member Function Documentation

int TcpSocketBuffer::bytesToRead  )  const [private]
 

See also:
readBufferSize(), setReadBufferSize()

Definition at line 317 of file TcpSocketBuffer.cpp.

int TcpSocketBuffer::bytesToWrite  )  const [private]
 

See also:
writeBufferSize(), setWriteBufferSize()

Definition at line 496 of file TcpSocketBuffer.cpp.

bool TcpSocketBuffer::canRead int  count  )  const [virtual]
 

Tells whether count bytes can be read from the read buffer.

Parameters:
count of bytes.
Returns:
true if count bytes can be read.

false if count bytes cannot be read.

Precondition:
count is not negative.
See also:
read(), peek()

Implements Protocols::Generics::Buffer.

Definition at line 223 of file TcpSocketBuffer.cpp.

bool TcpSocketBuffer::canWrite int  count  )  const [virtual]
 

Tells whether count number of bytes can be buffered in the write buffer.

Parameters:
count of bytes.
Returns:
true if count number of bytes can be written to the buffer.

false if the buffer cannot fit count more bytes.

Precondition:
count is not negative.
See also:
write()

Implements Protocols::Generics::Buffer.

Definition at line 416 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::hasRead int  count  )  [private]
 

Adds count bytes to the end of the read buffer.

Parameters:
count of bytes to mark as pushed to the read buffer.
Called after some data has been written at the location starting from nextReadChunk() to notify this buffer how many bytes have been written.

Precondition:
count is not negative.
See also:
nextReadChunk(), nextReadChunkSize()

Definition at line 358 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::hasWritten int  count  )  [private]
 

Remove count bytes from the beginning of the write buffer.

Parameters:
count of bytes that have been purged from the write buffer.
Called after some data has been read from the location starting from getWriteBuffer() to notify this buffer how many bytes have been read.

Precondition:
count is not negative.
See also:
nextWriteChunk(), nextWriteChunkSize()

Definition at line 533 of file TcpSocketBuffer.cpp.

char * TcpSocketBuffer::nextReadChunk  )  const [private]
 

The number of bytes you can write consequatively to the read buffer starting from the returned raw pointer is returned by nextReadChunkSize().

See also:
hasRead(), nextReadChunkSize()

Definition at line 329 of file TcpSocketBuffer.cpp.

int TcpSocketBuffer::nextReadChunkSize  )  const [private]
 

See also:
nextReadChunk(), hasRead()

Definition at line 339 of file TcpSocketBuffer.cpp.

char * TcpSocketBuffer::nextWriteChunk  )  const [private]
 

The number of bytes you can read consequatively from the write buffer starting at returned raw pointer is returned by nextWriteChunkSize().

See also:
nextWriteChunkSize(), hasWritten()

Definition at line 508 of file TcpSocketBuffer.cpp.

int TcpSocketBuffer::nextWriteChunkSize  )  const [private]
 

See also:
nextWriteChunk(), hasWritten()

Definition at line 517 of file TcpSocketBuffer.cpp.

TcpSocketBuffer& Protocols::Generics::TcpSocketBuffer::operator= const TcpSocketBuffer  )  [private]
 

void TcpSocketBuffer::peek char *  destination,
int  size
const [private]
 

Copies the first bytes from the read buffer in outBytes.

Parameters:
destination points to the location where to write size number of bytes.
size of the destination buffer.
Copies size from the read buffer to the memory poitend to by destination.

The buffer read position is not moved forward as that will be the case with read().

Precondition:
At least size bytes must be buffered in the read buffer.
See also:
canRead()

Definition at line 245 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::peek int  count  )  const [virtual]
 

Peeks count bytes from the read buffer.

Parameters:
count is the requested number of bytes that should be peeked from the read buffer.
Returns:
An array of count number of bytes if the bytes are available in the buffer.

An empty array if there were not enough bytes in the buffer.

Note:
This function does not purge the returned bytes from the read buffer. Subsequent peek or read operations will return the same data.

Implements Protocols::Generics::Buffer.

Definition at line 66 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::peekAll  )  const [virtual]
 

Peeks all bytes from the read buffer.

Returns:
all currently buffered data.
Note:
This function does not purge the returned bytes from the read buffer. Subsequent peek or read operations will return the same data.

Implements Protocols::Generics::Buffer.

Definition at line 127 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::peekAtMost int  count  )  const [virtual]
 

Peeks at most count bytes from the read buffer.

Parameters:
count is the maximum number of bytes that should be peeked from the read buffer.
Returns:
An array of count or less bytes that are bytes are available in the buffer.

An empty array if there were no bytes available in the buffer.

Note:
This function does not purge the returned bytes from the read buffer. Subsequent peek or read operations will return the same data.

Implements Protocols::Generics::Buffer.

Definition at line 90 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::peekTo const QByteArray &  delimiter  )  const [virtual]
 

Peeks from the buffer all bytes up to and including delimiter.

Parameters:
delimiter is a sequence of bytes that will be searched for in the read buffer. All bytes up to and including the delimiter will be returned.
Returns:
All bytes up to and including the delimiter if it was found.

An empty array therwise.

Note:
This function does not purge the returned bytes from the read buffer. Subsequent peek or read operations will return the same data.

This function will traverse the whole read buffer twice - first to extract the data and then to find the delimiter.

Implements Protocols::Generics::Buffer.

Definition at line 110 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::purgeReadBuffer int  count  )  [private]
 

Purges the first count bytes from the read buffer.

We implement read(), readTo() and readAll() in terms of peek(), peekTo() and peekAll() and then use purgeReadBuffer() to purge the bytes already extracted by the read functions.

Parameters:
count of bytes to purge from the read buffer.

Definition at line 269 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::read int  count  )  [virtual]
 

Reads count bytes from the read buffer.

Parameters:
count is the requested number of bytes that should be extracted from the read buffer.
Returns:
An array of count number of bytes if the bytes ara available in the buffer.

An empty array if there were not enough bytes in the buffer.

Implements Protocols::Generics::Buffer.

Definition at line 141 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::readAll  )  [virtual]
 

Extracts and returns all bytes from the read buffer.

Returns:
all currently buffered data.

Implements Protocols::Generics::Buffer.

Definition at line 187 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::readAtMost int  count  )  [virtual]
 

Reads at most count bytes from the read buffer.

Parameters:
count is the maximum number of bytes that should be extracted from the read buffer.
Returns:
An array of count or less bytes if the bytes ara available in the buffer.

An empty array if there were no bytes available in the buffer.

Implements Protocols::Generics::Buffer.

Definition at line 157 of file TcpSocketBuffer.cpp.

int TcpSocketBuffer::readBufferSize  )  const [private]
 

See also:
setReadBufferSize(), bytesToRead()

Definition at line 308 of file TcpSocketBuffer.cpp.

QByteArray TcpSocketBuffer::readTo const QByteArray &  delimiter  )  [virtual]
 

Extracts from the buffer all bytes up to and including delimiter.

Parameters:
delimiter is a sequence of bytes that will be searched for in the read buffer. All bytes up to and including the delimiter will be returned.
Returns:
All bytes up to and including the delimiter if it was found.

An empty array therwise.

Note:
This function will traverse the whole read buffer twice - first to extract the data and then to find the delimiter.

Implements Protocols::Generics::Buffer.

Definition at line 176 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::setReadBufferSize int  size  )  [private]
 

Resets the read buffer size and all previous data is lost.

Parameters:
size is the new buffer size to allocate.
Precondition:
size is not negative.
See also:
readBufferSize(), bytesToRead()
Todo:
use a QByteArray or an auto_ptr instead of raw pointers?

only called by the ctor - use ctor initializer lists instead?

Definition at line 292 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::setWriteBufferSize int  size  )  [private]
 

Resets the write buffer size and all previous data is lost.

Parameters:
size of the new write buffer to be allocated.
Precondition:
size is not negative.
See also:
writeBufferSize(), bytesToWrite()
Todo:
use a QByteArray or an auto_ptr instead of raw pointers?

only called by the ctor - use ctor initializer lists instead?

Definition at line 471 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::tryReadFrom Socket socket  )  [virtual]
 

Tries to read from the socket to the read buffer.

Parameters:
socket to try to read from.

Implements Protocols::Generics::SocketBuffer.

Definition at line 198 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::tryWriteTo Socket socket  )  [virtual]
 

Tries to write from the buffered write buffer to socket.

Parameters:
socket to try to write to.

Implements Protocols::Generics::SocketBuffer.

Definition at line 389 of file TcpSocketBuffer.cpp.

void TcpSocketBuffer::write const char *  source,
int  size,
bool  flush
[private]
 

Buffers size bytes from source in the write buffer.

Parameters:
source data buffer from which to write to the write buffer.
size of the source buffer.
flush determines whether the size bytes in source can be written right away or should be buffered until write() is called again with flush set to true.
The function copies size bytes starting from source in the write buffer.

The parameter flush will give the buffer a hint how the data should be passed to the transport. The buffer would try to send all bytes written in consequative calls to write() with flush set to false in a single piece to the transport layer. flush may be ignored for stream transports but its use is essential for datagram transports.

Precondition:
There is space to buffer size bytes in the write buffer.
See also:
canWrite()

Definition at line 443 of file TcpSocketBuffer.cpp.

bool TcpSocketBuffer::write const QByteArray &  bytes,
bool  flush = true
[virtual]
 

Writes bytes to the write buffer.

Parameters:
bytes are the bytes that should be buffered in the write buffer.
flush is a tip for tryWriteTo() whether it should not write to socket until a write() has been called with flush = true.
Returns:
true if all the bytes were buffered.

false if none were buffered.

Note:
The data in bytes is never buffered partially!

Implements Protocols::Generics::Buffer.

Definition at line 376 of file TcpSocketBuffer.cpp.

int TcpSocketBuffer::writeBufferSize  )  const [private]
 

See also:
setWriteBufferSize(), bytesToWrite()

Definition at line 487 of file TcpSocketBuffer.cpp.


Member Data Documentation

char* Protocols::Generics::TcpSocketBuffer::readBuffer [private]
 

Definition at line 94 of file TcpSocketBuffer.h.

int Protocols::Generics::TcpSocketBuffer::readEnd [private]
 

Definition at line 97 of file TcpSocketBuffer.h.

int Protocols::Generics::TcpSocketBuffer::readSize [private]
 

Definition at line 95 of file TcpSocketBuffer.h.

int Protocols::Generics::TcpSocketBuffer::readStart [private]
 

Definition at line 96 of file TcpSocketBuffer.h.

char* Protocols::Generics::TcpSocketBuffer::writeBuffer [private]
 

Definition at line 98 of file TcpSocketBuffer.h.

int Protocols::Generics::TcpSocketBuffer::writeEnd [private]
 

Definition at line 101 of file TcpSocketBuffer.h.

int Protocols::Generics::TcpSocketBuffer::writeFlushEnd [private]
 

Definition at line 102 of file TcpSocketBuffer.h.

int Protocols::Generics::TcpSocketBuffer::writeSize [private]
 

Definition at line 99 of file TcpSocketBuffer.h.

int Protocols::Generics::TcpSocketBuffer::writeStart [private]
 

Definition at line 100 of file TcpSocketBuffer.h.


The documentation for this class was generated from the following files: