medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 69773421b1cd453d0df49dd451af871b3e618118
parent ab147445a06bad9f12327610187c63f9dd681916
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Tue Nov 22 21:39:11 +0200

Refactor stream API a bit

Diffstat:
server/main.cc | 10++++------
stream.h | 66+++++++++++++++++++++++++++++++++++++++++++++++++-----------------
2 files changed, 53 insertions(+), 23 deletions(-)
diff --git a/server/main.cc b/server/main.cc @@ -152,12 +152,10 @@ int main() { memset( buf, 0, sizeof( buf ) ); size_t len; while( receive( server_fd_ipv4, buf, &len, sizeof( buf ) ) ) { - Stream stream; - stream.ptr = buf; - // stream.len = len + ReadStream stream( buf, len );; - u64 sid; - stream = read_u64( stream, &sid ); + u64 sid = read_u64( &stream ); + if( !stream.ok ) continue; if( sid == u64( -1 ) ) { u64 new_sid = rng_next_u64( &rng ); @@ -174,7 +172,7 @@ int main() { } } } - while( receive( server_fd_ipv6, buf, &len, sizeof( buf ) ) ); + // while( receive( server_fd_ipv6, buf, &len, sizeof( buf ) ) ); printf( "frame\n" ); for( size_t i = 0; i < ARRAY_COUNT( states ); i++ ) { diff --git a/stream.h b/stream.h @@ -1,37 +1,69 @@ +// TODO: need to come up with something for writers though + #ifndef _STREAM_H_ #define _STREAM_H_ #include "intrinsics.h" #include "platform_inline.h" -struct Stream { +struct ReadStream { + ReadStream( char * buf, size_t len ) { + ptr = buf; + one_past_end = buf + len; + ok = true; + } + + char * ptr; + char * one_past_end; + bool ok; +}; + +struct WriteStream { + WriteStream( char * buf, size_t len ) { + start = buf; + ptr = buf; + one_past_end = buf + len; + ok = true; + } + + char * start; char * ptr; + char * one_past_end; + bool ok; }; -static forceinline bool cpu_is_little_endian() { -#if FORCE_LITTLE_ENDIAN - return true; -#else +static bool cpu_is_little_endian() { u16 i = 1; char * c = ( char * ) &i; return c[ 0 ] == 1; -#endif } -// TODO: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html -// maybe use __builtin_bswap16/32/64 and http://stackoverflow.com/a/13208789 -#define DEF_READ_WRITE( type ) \ - inline Stream read_##type( Stream stream, type * v ) { \ +#define DEF_READ_WRITE( T ) \ + inline void read( ReadStream * stream, T * x ) { \ ASSERT( cpu_is_little_endian() ); \ - *v = *( type * ) stream.ptr; \ - stream.ptr += sizeof( type ); \ - return stream; \ + if( !stream->ok || checked_cast< size_t >( stream->one_past_end - stream->ptr ) < sizeof( T ) ) { \ + stream->ok = false; \ + *x = T(); \ + } \ + memcpy( x, stream->ptr, sizeof( T ) ); \ + stream->ptr += sizeof( T ); \ } \ - inline Stream write_##type( Stream stream, const type * v ) { \ + inline T read_##T( ReadStream * stream ) { \ + T x; \ + read( stream, &x ); \ + return x; \ + } \ + inline void write( WriteStream * stream, const T & x ) { \ ASSERT( cpu_is_little_endian() ); \ - *( type * ) stream.ptr = *v; \ - stream.ptr += sizeof( type ); \ - return stream; \ + if( !stream->ok || checked_cast< size_t >( stream->one_past_end - stream->ptr ) < sizeof( T ) ) { \ + stream->ok = false; \ + return; \ + } \ + memcpy( stream->ptr, &x, sizeof( T ) ); \ + stream->ptr += sizeof( T ); \ + } \ + inline void write_##T( WriteStream * stream, const T & x ) { \ + write( stream, x ); \ } DEF_READ_WRITE( u8 )