As requested by Coacci, posting the description of the basic classes
involved in network code.
ByteArray is an abstract class, it has a few methods implemented, but cannot
be instantiated.
what it does is to give a common interface to access things that wraps a
byte array (char *). This can be used to aid in the serialization of many
data types.
ByteArray
public:
const char& operator[](int index) const;
char& operator[](int index);
int getCapacity();
void copyFrom(const ByteArray& source, int srcIndex, int dstIndex, int
size);
private:
char& fetchCharacter(int index); // a wrapper to common functionality
shared
// by both [] operators.
ByteArray(char * wrappedData, int capacity);
virtual ~ByteArray();
protected:
int capacity;
char* data;
--
With ByteArray as a basis, we can start creating wrappers for many things.
There is a class of information that can be wrapped in byte arrays for easy
serialization: all those "integer-based" types (char, short, int, unsigned
char, unsigned short, unsigned int, void* and fixed-point (which I will
explain later)). To aid those in serialization, we use a basic template
class that can make them, it is ByteArrayTemplateWrapper.
template <class T>
ByteArrayTemplateWrapper extends ByteArray as public
public:
ByteArrayTemplateWrapper(const T wrappedData);
~ByteArrayTemplateWrapper();
const T& getValue();
void setValue(const T&);
private:
T internalData;
Well, what it does:
It receives a pointer to an entity of the T Type, and initialize its parent
ByteArray with a pointer to it (converted to char*) as data and the
sizeof(T) value as capacity. That effectively creates an serialized array of
the data described by the object.
WARNING: One should not use this template to serialize data that holds
pointers or stuff like that when sending through the network, as it
obviously would make no sense to the other end of the connection.
The ByteArrayTemplateWrapper is realized through a few typedefs, as follows:
typedef ByteArrayTemplateWrapper<int> ByteArrayIntWrapper;
typedef ByteArrayTemplateWrapper<short> ByteArrayShortWrapper;
typedef ByteArrayTemplateWrapper<char> ByteArrayCharWrapper;
typedef ByteArrayTemplateWrapper<void*> ByteArrayVoidPtrWrapper;
typedef ByteArrayTemplateWrapper<unsigned int> ByteArrayUnsignedIntWrapper;
typedef ByteArrayTemplateWrapper<unsigned short>
ByteArrayUnsignedShortWrapper;
typedef ByteArrayTemplateWrapper<unsigned char>
ByteArrayUnsignedCharWrapper;
These are commonly used to handle the information packed into a message's
body.
--
Along with the TemplateWrappers there is also an "Allocated Array Wrapper",
that just creates an array of chars from heap space and holds its pointer
until it is destructed (be it by scope ending or explicit delete). It
follows:
AllocByteArray extends ByteArray as public
public:
AllocByteArray(int capacity);
AllocByteArray(char* source, int size);
~AllocByteArray();
And that is pretty much it. The constructors are simple, one just allocates
it, the other allocates and copies the data from the source array to the
newly allocated one.
Well, I just hope this is enough information for you to start the UML class
diagrams, Coacci. I have to work a bit now, otherwise my boss will get mad
at me (lol).
Cheers,
Alexandre Moreira.
|