From ba4e2e58c7c4b5e91aebea5a8250fd6072f947f0 Mon Sep 17 00:00:00 2001 From: Glenn Fiedler Date: Sun, 24 Dec 2023 18:56:27 -0500 Subject: [PATCH] more cleanup and flesh out tests --- serialize.h | 103 ---------------------------------------------------- test.cpp | 19 +++++++--- 2 files changed, 13 insertions(+), 109 deletions(-) diff --git a/serialize.h b/serialize.h index 74f480b..c5b7cef 100644 --- a/serialize.h +++ b/serialize.h @@ -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 \. 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. @@ -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 \. 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 bool serialize_int_relative_internal( Stream & stream, T previous, T & current ) { uint32_t difference = 0; @@ -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 \. 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 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 \. 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 ) \ @@ -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. @@ -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 diff --git a/test.cpp b/test.cpp index 5c45557..6d9452a 100644 --- a/test.cpp +++ b/test.cpp @@ -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]; }; @@ -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 ) @@ -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; @@ -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 ); @@ -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 ) );