Skip to content

Commit

Permalink
more cleanup and flesh out tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Dec 24, 2023
1 parent 940422e commit ba4e2e5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 109 deletions.
103 changes: 0 additions & 103 deletions serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -2041,23 +2041,6 @@ namespace serialize
} \
} while (0)

/**
Serialize a safety check to the stream (read/write/measure).
This is a helper macro to make writing unified serialize functions easier.
Serialize macros returns false on error so we don't need to use exceptions for error handling on read. This is an important safety measure because packet data comes from the network and may be malicious.
IMPORTANT: This macro must be called inside a templated serialize function with template \<typename Stream\>. The serialize method must have a bool return value.
@param stream The stream object. May be a read, write or measure stream.
*/

#define serialize_check( stream ) \
do \
{ \
if ( !stream.SerializeCheck() ) \
{ \
return false; \
} \
} while (0)

/**
Serialize an object to the stream (read/write/measure).
This is a helper macro to make writing unified serialize functions easier.
Expand All @@ -2077,24 +2060,6 @@ namespace serialize
} \
while(0)

/**
Serialize an address to the stream (read/write/measure).
This is a helper macro to make writing unified serialize functions easier.
Serialize macros returns false on error so we don't need to use exceptions for error handling on read. This is an important safety measure because packet data comes from the network and may be malicious.
IMPORTANT: This macro must be called inside a templated serialize function with template \<typename Stream\>. The serialize method must have a bool return value.
@param stream The stream object. May be a read, write or measure stream.
@param value The address to serialize. Must be a valid address.
*/

#define serialize_address( stream, value ) \
do \
{ \
if ( !serialize::serialize_address_internal( stream, value ) ) \
{ \
return false; \
} \
} while (0)

template <typename Stream, typename T> bool serialize_int_relative_internal( Stream & stream, T previous, T & current )
{
uint32_t difference = 0;
Expand Down Expand Up @@ -2262,67 +2227,6 @@ namespace serialize
return true;
}

/**
Serialize an ack relative to the current sequence number (read/write/measure).
This is a helper macro to make writing unified serialize functions easier.
Serialize macros returns false on error so we don't need to use exceptions for error handling on read. This is an important safety measure because packet data comes from the network and may be malicious.
IMPORTANT: This macro must be called inside a templated serialize function with template \<typename Stream\>. The serialize method must have a bool return value.
@param stream The stream object. May be a read, write or measure stream.
@param sequence The current sequence number.
@param ack The ack sequence number, which is typically near the current sequence number.
*/

#define serialize_ack_relative( stream, sequence, ack ) \
do \
{ \
if ( !serialize::serialize_ack_relative_internal( stream, sequence, ack ) ) \
{ \
return false; \
} \
} while (0)

template <typename Stream> bool serialize_sequence_relative_internal( Stream & stream, uint16_t sequence1, uint16_t & sequence2 )
{
if ( Stream::IsWriting )
{
uint32_t a = sequence1;
uint32_t b = sequence2 + ( ( sequence1 > sequence2 ) ? 65536 : 0 );
serialize_int_relative( stream, a, b );
}
else
{
uint32_t a = sequence1;
uint32_t b = 0;
serialize_int_relative( stream, a, b );
if ( b >= 65536 )
{
b -= 65536;
}
sequence2 = uint16_t( b );
}

return true;
}

/**
Serialize a sequence number relative to another (read/write/measure).
This is a helper macro to make writing unified serialize functions easier.
Serialize macros returns false on error so we don't need to use exceptions for error handling on read. This is an important safety measure because packet data comes from the network and may be malicious.
IMPORTANT: This macro must be called inside a templated serialize function with template \<typename Stream\>. The serialize method must have a bool return value.
@param stream The stream object. May be a read, write or measure stream.
@param sequence1 The first sequence number to serialize relative to.
@param sequence2 The second sequence number to be encoded relative to the first.
*/

#define serialize_sequence_relative( stream, sequence1, sequence2 ) \
do \
{ \
if ( !serialize::serialize_sequence_relative_internal( stream, sequence1, sequence2 ) ) \
{ \
return false; \
} \
} while (0)

// read macros corresponding to each serialize_*. useful when you want separate read and write functions.

#define read_bits( stream, value, bits ) \
Expand Down Expand Up @@ -2365,10 +2269,7 @@ namespace serialize
#define read_align serialize_align
#define read_check serialize_check
#define read_object serialize_object
#define read_address serialize_address
#define read_int_relative serialize_int_relative
#define read_ack_relative serialize_ack_relative
#define read_sequence_relative serialize_sequence_relative

// write macros corresponding to each serialize_*. useful when you want separate read and write functions for some reason.

Expand Down Expand Up @@ -2402,12 +2303,8 @@ namespace serialize
#define write_bytes serialize_bytes
#define write_string serialize_string
#define write_align serialize_align
#define write_check serialize_check
#define write_object serialize_object
#define write_address serialize_address
#define write_int_relative serialize_int_relative
#define write_ack_relative serialize_ack_relative
#define write_sequence_relative serialize_sequence_relative
}

#endif // #ifndef SERIALIZE_H
19 changes: 13 additions & 6 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ struct TestData
float compressed_float_value;
double double_value;
uint64_t uint64_value;
uint32_t varint32_value;
uint64_t varint64_value;
int int_relative;
uint8_t bytes[17];
char string[256];
};
Expand All @@ -195,8 +198,6 @@ struct TestObject
data.e = 255;
data.f = 127;
data.g = true;
data.v32 = UINT32_MAX;
data.v64 = UINT64_MAX;

data.numItems = MaxItems / 2;
for ( int i = 0; i < data.numItems; ++i )
Expand All @@ -206,6 +207,9 @@ struct TestObject
data.float_value = 3.1415926f;
data.double_value = 1 / 3.0;
data.uint64_value = 0x1234567898765432L;
data.varint32_value = 123456;
data.varint64_value = 123456789101112;
data.int_relative = 5;

for ( int i = 0; i < (int) sizeof( data.bytes ); ++i )
data.bytes[i] = rand() % 255;
Expand All @@ -222,9 +226,6 @@ struct TestObject

serialize_int( stream, data.c, -100, 10000 );

serialize_varint32( stream, data.v32 );
serialize_varint64( stream, data.v64 );

serialize_bits( stream, data.d, 6 );
serialize_bits( stream, data.e, 8 );
serialize_bits( stream, data.f, 7 );
Expand All @@ -239,12 +240,18 @@ struct TestObject

serialize_float( stream, data.float_value );

serialize_compressed_float(stream, data.compressed_float_value, 0, 10, 0.01);
serialize_compressed_float( stream, data.compressed_float_value, 0, 10, 0.01 );

serialize_double( stream, data.double_value );

serialize_uint64( stream, data.uint64_value );

serialize_varint32( stream, data.varint32_value );

serialize_varint64( stream, data.varint64_value );

serialize_int_relative( stream, data.a, data.int_relative );

serialize_bytes( stream, data.bytes, sizeof( data.bytes ) );

serialize_string( stream, data.string, sizeof( data.string ) );
Expand Down

0 comments on commit ba4e2e5

Please sign in to comment.