MCS  0.3.3-alpha7
mcs::Serializable Class Reference

Serialize memory buffers or files into chunks. More...

#include <mcs.hh>

+ Inheritance diagram for mcs::Serializable:

Public Member Functions

void * getEntireBuffer (unsigned int &size)
 
bool knowSize ()
 Reinitialize internal data. More...
 
unsigned int maxChunkSize ()
 Return the max size allowed for a chunk. More...
 
unsigned int nChunk ()
 If knowSize() is true, return how many chunks are required to send all data. More...
 
void * nextChunk (unsigned int &chunksize)
 Fills a buffer with next chunk to be sent. More...
 
Serializableoperator= (const Serializable &)
 Declared to avoid using of default assignment operator. More...
 
 Serializable (const Serializable &)
 Declared to avoid using of default copy constructor. More...
 
 Serializable (unsigned int lmaxChunksize=16384)
 Constructor, for custom data (type = SERIAL_UNKNOWN). More...
 
 Serializable (void *from, unsigned int size, bool freeAfterUse, unsigned int lmaxChunksize=16384)
 Constructor, for memory buffers (type = SERIAL_BUFFER). More...
 
 Serializable (string filename, unsigned int lmaxChunksize=16384)
 Constructor, for files (type = SERIAL_FILENAME). More...
 
 Serializable (int type, unsigned int lmaxChunksize=16384)
 
unsigned int size ()
 If knowSize() is true, return the size of the entire block of data. More...
 
virtual ~Serializable ()
 Destructor. More...
 

Private Member Functions

void init (int type, unsigned int lmaxChunksize)
 Common initialization. More...
 
virtual char * nextChunk_unknown (char *userdata, char *buf, unsigned int &chunksize, bool firstTime)
 Custom routine to fill a buffer with a chunk. More...
 
void serialize ()
 
virtual bool serialize_buffer (char *&from, unsigned int &size)
 
void serialize_end ()
 
virtual string serialize_filename ()
 
virtual char * serialize_unknown ()
 

Private Attributes

char * buf
 Pointer to temporary buffer containing the next chunk. More...
 
string filename
 Filename of the file being sent, if type = MCS_SERIAL_FILENAME. More...
 
bool firstTime
 
bool freeAfterUse
 If true the external buffer will be automatically freed, used when type = MCS_SERIAL_BUFFER. More...
 
char * from
 Pointer to source buffer, if type = MCS_SERIAL_BUFFER. More...
 
unsigned int lsize
 Size of the actual chunk. More...
 
unsigned int maxChunksize
 Maximum size of the chunk. More...
 
 MCS_DEBUG_ALLOC
 
ifstream * stream
 Pointer to an ifstream, if type = MCS_SERIAL_FILENAME. More...
 
int type
 Type of data to be sent. More...
 
char * userdata
 

Detailed Description

Serialize memory buffers or files into chunks.

This class is used from the Socket class to split data buffers into chunks of a maximum size, so they can be sent across the network. Objects that must be sent through the network must derive this class and eventually implement its virtual methods.

Chunks are extracted from the source using the serialize_buffer() virtual method.

This class has an internal type specification which is used to determine how to retrieve data, this specification can be one of MCS_SERIAL_BUFFER (for memory buffers), MCS_SERIAL_FILENAME (for files) and MCS_SERIAL_UNKNOWN (for custom data). In this last case the user must implement the serialize_unknown() and nextChunk_unknown() virtual methods to properly read data into the internal buffer.

Definition at line 1290 of file mcs.hh.

Constructor & Destructor Documentation

◆ Serializable() [1/4]

mcs::Serializable::Serializable ( const Serializable )

Declared to avoid using of default copy constructor.

Warning
This constructor is declared but not implemented. If you try to use it you will get a compilation error.

◆ Serializable() [2/4]

mcs::Serializable::Serializable ( unsigned int  lmaxChunksize = 16384)

Constructor, for custom data (type = SERIAL_UNKNOWN).

Parameters
lmaxChunksizeMax allowed size for a chunk, default is MCS_CHUNKSIZE bytes.

Definition at line 59 of file Serializable.cc.

◆ Serializable() [3/4]

mcs::Serializable::Serializable ( void *  from,
unsigned int  size,
bool  freeAfterUse,
unsigned int  lmaxChunksize = 16384 
)

Constructor, for memory buffers (type = SERIAL_BUFFER).

Parameters
bufAddress of the buffer containing data to be sent.
sizeSize of the buffer.
lmaxChunksizeMax allowed size for a chunk, default is MCS_CHUNKSIZE.

Definition at line 64 of file Serializable.cc.

◆ Serializable() [4/4]

mcs::Serializable::Serializable ( string  filename,
unsigned int  lmaxChunksize = 16384 
)

Constructor, for files (type = SERIAL_FILENAME).

Parameters
filenameFile to be sent.
lmaxChunksizeMax allowed size for a chunk, default is MCS_CHUNKSIZE.

Definition at line 74 of file Serializable.cc.

◆ ~Serializable()

mcs::Serializable::~Serializable ( )
virtual

Destructor.

Definition at line 88 of file Serializable.cc.

Member Function Documentation

◆ init()

void mcs::Serializable::init ( int  type,
unsigned int  lmaxChunksize 
)
private

Common initialization.

Parameters
lmaxChunksizeMax allowed size for a chunk.

Definition at line 31 of file Serializable.cc.

◆ knowSize()

bool mcs::Serializable::knowSize ( )

Reinitialize internal data.

Reinitialize internal data, so that next call to fillBuffer() will read data from the beginning of the buffer or the file. If type = SERIAL_UNKNOWN the next call to fillBuffer will have firstTime = true.

Tells if the object is in use.

If a call to fillBuffer() has already be done, and data are not yet finished this will return true. Otherwise return false.

Tells if we already know the size of entire block of data.

If type = SERIAL_FILENAME or SERIAL_BUFFER, we already know the size of the entire block of data, and this return true. If type = SERIAL_UNKNOWN return false.

Definition at line 232 of file Serializable.cc.

◆ maxChunkSize()

unsigned int mcs::Serializable::maxChunkSize ( )

Return the max size allowed for a chunk.

Definition at line 251 of file Serializable.cc.

◆ nChunk()

unsigned int mcs::Serializable::nChunk ( )

If knowSize() is true, return how many chunks are required to send all data.

Definition at line 237 of file Serializable.cc.

◆ nextChunk()

void * mcs::Serializable::nextChunk ( unsigned int &  chunksize)

Fills a buffer with next chunk to be sent.

Parameters
chunksizeUpon exit contains the size of the chunk.
Returns
Address of the buffer containing the chunk, or NULL if there are no more data.

Definition at line 169 of file Serializable.cc.

◆ nextChunk_unknown()

char * mcs::Serializable::nextChunk_unknown ( char *  userdata,
char *  buf,
unsigned int &  chunksize,
bool  firstTime 
)
privatevirtual

Custom routine to fill a buffer with a chunk.

This virtual method should be overloadd in derived class, to fill the internal buffer when type = SERIAL_UNKNOWN. This method is called automatically by fillBuffer().

Parameters
bufAddress of the buffer to fill with the chunk.
chunksizeUpon exit must contain the size of the chunk.
firstTimeTell if it is the first time the method is called.

Definition at line 93 of file Serializable.cc.

◆ operator=()

Serializable& mcs::Serializable::operator= ( const Serializable )

Declared to avoid using of default assignment operator.

Warning
This operator is declared but not implemented. If you try to use it you will get a compilation error.

◆ size()

unsigned int mcs::Serializable::size ( )

If knowSize() is true, return the size of the entire block of data.

Definition at line 245 of file Serializable.cc.

Member Data Documentation

◆ buf

char* mcs::Serializable::buf
private

Pointer to temporary buffer containing the next chunk.

Definition at line 1296 of file mcs.hh.

◆ filename

string mcs::Serializable::filename
private

Filename of the file being sent, if type = MCS_SERIAL_FILENAME.

Definition at line 1305 of file mcs.hh.

◆ freeAfterUse

bool mcs::Serializable::freeAfterUse
private

If true the external buffer will be automatically freed, used when type = MCS_SERIAL_BUFFER.

Definition at line 1317 of file mcs.hh.

◆ from

char* mcs::Serializable::from
private

Pointer to source buffer, if type = MCS_SERIAL_BUFFER.

Definition at line 1311 of file mcs.hh.

◆ lsize

unsigned int mcs::Serializable::lsize
private

Size of the actual chunk.

Definition at line 1302 of file mcs.hh.

◆ maxChunksize

unsigned int mcs::Serializable::maxChunksize
private

Maximum size of the chunk.

Definition at line 1299 of file mcs.hh.

◆ stream

ifstream* mcs::Serializable::stream
private

Pointer to an ifstream, if type = MCS_SERIAL_FILENAME.

Definition at line 1320 of file mcs.hh.

◆ type

int mcs::Serializable::type
private

Type of data to be sent.

Definition at line 1308 of file mcs.hh.


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

mcslogo

MCS (My Customizable Server) ver. 0.3.3-alpha7
Documentation generated on Mon May 28 07:39:41 UTC 2018