My Project
Static Public Member Functions | List of all members
NPL::NPLCodec Class Reference

transport layer NPL message compressor(encoder) and decompresser(decoder) Currently, we use a very simply custom encoding/decoding algorithm. More...

#include <NPLCodec.h>

Static Public Member Functions

static int Encode (char *dest, const char *src, int nSrcSize, uint32 nPublicKey=0)
 encode a byte array from src to dest More...
 
static int Decode (char *dest, const char *src, int nSrcSize, uint32 nPublicKey=0)
 decode a byte array from src to dest More...
 
static void UsePlainTextEncoding (bool bUsePlainTextEncoding)
 no matter what public key is used, we will ensure that the encoded message is plain text [0,128) [Not Thread Safe]: one must call this function before sending or receiving any encoded messages. More...
 
static void SetGenericKey (const byte *sKey, int nSize)
 set the generic key that is used for encoding/decoding. More...
 
static int Compress (string &outstring, const char *src, int nSrcSize, int compressionlevel=-1)
 Compress src to a string buffer using zlib. More...
 
static int Decompress (string &outstring, const char *src, int nSrcSize)
 Decompress src to destination. More...
 

Detailed Description

transport layer NPL message compressor(encoder) and decompresser(decoder) Currently, we use a very simply custom encoding/decoding algorithm.

The idea is to change the NPL codecs implementation very often so that no one will easily decode it without spending a lot of human time.

[thread safe]: class is thread safe.

Member Function Documentation

§ Compress()

int NPLCodec::Compress ( string &  outstring,
const char *  src,
int  nSrcSize,
int  compressionlevel = -1 
)
static

Compress src to a string buffer using zlib.

[Thread safe]

Parameters
outstringthe output buffer to store compressed data. Data will be appended to it.
srcthe input source bytes
nSrcSizelength of source in byte
compressionlevelcompression level, which is an integer in the range of -1 to 9. Lower compression levels result in faster execution, but less compression. Higher levels result in greater compression, but slower execution. The zlib constant Z_DEFAULT_COMPRESSION, equal to -1, provides a good compromise between compression and speed and is equivalent to level 6. Level 0 actually does no compression at all, and in fact expands the data slightly to produce the zlib format (it is not a byte-for-byte copy of the input).
Returns
: -1 if failed, 1 if succeed.

§ Decode()

int NPLCodec::Decode ( char *  dest,
const char *  src,
int  nSrcSize,
uint32  nPublicKey = 0 
)
static

decode a byte array from src to dest

The current custom algorithm is as follow.

Note
: this algorithm allows the dest to be the same as src, which performs decoding in place of src
Parameters
destthe destination buffer, which needs to be at least nSrcSize in length
srcthe input source bytes to decode
nSrcSizelength of source in byte
nPublicKeythe public key. Sometimes, we use the message length as public key to make the message more difficult to decode
Returns
: the number of bytes written to dest.
  1. shift the private key by nPublicKey mod private key size.
  2. for each src char, computer the dest char as XOR of src and private key
  3. increase src, dest, and private key by 1 byte, and repeat 2. If private key char is '\0', reset the private key to the beginning.

§ Decompress()

int NPLCodec::Decompress ( string &  outstring,
const char *  src,
int  nSrcSize 
)
static

Decompress src to destination.

[Thread safe]:

Parameters
outstringthe output buffer to store decompressed data. Data will be appended to it.
srcthe input source bytes
nSrcSizelength of source in byte

§ Encode()

int NPLCodec::Encode ( char *  dest,
const char *  src,
int  nSrcSize,
uint32  nPublicKey = 0 
)
static

encode a byte array from src to dest

Note
: this algorithm allows the dest to be the same as src, which performs encoding in place of src
Parameters
destthe destination buffer, which needs to be at least nSrcSize in length
srcthe input source bytes to encode
nSrcSizelength of source in byte
nPublicKeythe public key. Sometimes, we use the message length as public key to make the message more difficult to decode
Returns
: the number of bytes written to dest.

§ SetGenericKey()

void NPLCodec::SetGenericKey ( const byte *  sKey,
int  nSize 
)
static

set the generic key that is used for encoding/decoding.

If this is not called, the default internal key is used for message encoding. [Not Thread Safe]: one must call this function before sending or receiving any encoded messages. so it is usually called when the game engine starts.

Parameters
sKeythe byte array of key
nSizesize in bytes of the sKey. default is 64 bytes

§ UsePlainTextEncoding()

void NPLCodec::UsePlainTextEncoding ( bool  bUsePlainTextEncoding)
static

no matter what public key is used, we will ensure that the encoded message is plain text [0,128) [Not Thread Safe]: one must call this function before sending or receiving any encoded messages.

so it is usually called when the game engine starts.


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