transport layer NPL message compressor(encoder) and decompresser(decoder) Currently, we use a very simply custom encoding/decoding algorithm.
More...
#include <NPLCodec.h>
|
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...
|
|
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.
§ 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
-
outstring | the output buffer to store compressed data. Data will be appended to it. |
src | the input source bytes |
nSrcSize | length of source in byte |
compressionlevel | compression 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
-
dest | the destination buffer, which needs to be at least nSrcSize in length |
src | the input source bytes to decode |
nSrcSize | length of source in byte |
nPublicKey | the 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.
- shift the private key by nPublicKey mod private key size.
- for each src char, computer the dest char as XOR of src and private key
- 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
-
outstring | the output buffer to store decompressed data. Data will be appended to it. |
src | the input source bytes |
nSrcSize | length 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
-
dest | the destination buffer, which needs to be at least nSrcSize in length |
src | the input source bytes to encode |
nSrcSize | length of source in byte |
nPublicKey | the 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
-
sKey | the byte array of key |
nSize | size 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:
- Client/trunk/ParaEngineClient/NPL/NPLCodec.h
- Client/trunk/ParaEngineClient/NPL/NPLCodec.cpp