[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[certi-cvs] certi/libHLA CMakeLists.txt MessageBufferTests....
From: |
CERTI CVS commits |
Subject: |
[certi-cvs] certi/libHLA CMakeLists.txt MessageBufferTests.... |
Date: |
Fri, 10 Jun 2011 14:05:31 +0000 |
CVSROOT: /sources/certi
Module name: certi
Changes by: Eric NOULARD <erk> 11/06/10 14:05:30
Modified files:
libHLA : CMakeLists.txt MessageBufferTests.cc
Added files:
libHLA : MsgBuffer.h MsgBuffer.c MsgBufferTests.c
Log message:
Add message buffer handling in C.
The two implementations (C and C++) will eventually be merged in order
avoid code duplication.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/CMakeLists.txt?cvsroot=certi&r1=1.14&r2=1.15
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/MessageBufferTests.cc?cvsroot=certi&r1=1.1&r2=1.2
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/MsgBuffer.h?cvsroot=certi&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/MsgBuffer.c?cvsroot=certi&rev=1.1
http://cvs.savannah.gnu.org/viewcvs/certi/libHLA/MsgBufferTests.c?cvsroot=certi&rev=1.1
Patches:
Index: CMakeLists.txt
===================================================================
RCS file: /sources/certi/certi/libHLA/CMakeLists.txt,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -b -r1.14 -r1.15
--- CMakeLists.txt 27 Apr 2011 15:07:43 -0000 1.14
+++ CMakeLists.txt 10 Jun 2011 14:05:30 -0000 1.15
@@ -29,21 +29,26 @@
SOURCE_GROUP("Source Files\\Alloc" FILES ${LIBHLA_ALLOC_SRCS})
ENDIF (NOT WIN32)
-SET(LIBHLA_MB_SRCS MessageBuffer.cc MessageBuffer.hh)
-LIST(APPEND LIBHLA_EXPORTED_INCLUDES MessageBuffer.hh)
+SET(LIBHLA_MB_SRCS MessageBuffer.cc MessageBuffer.hh MsgBuffer.c MsgBuffer.h)
+LIST(APPEND LIBHLA_EXPORTED_INCLUDES MessageBuffer.hh MsgBuffer.h)
+set_source_files_properties(MsgBuffer.c MsgBuffer.h PROPERTIES LANGUAGE "C")
SET(LIBHLA_MB_TEST_SRCS MessageBufferTests.cc)
-
ADD_EXECUTABLE(MessageBufferTests ${LIBHLA_MB_TEST_SRCS})
-
TARGET_LINK_LIBRARIES(MessageBufferTests HLA)
-INSTALL(TARGETS MessageBufferTests
+add_executable(MsgBufferTests MsgBufferTests.c)
+set_source_files_properties(MsgBufferTests.c PROPERTIES LANGUAGE "C")
+target_link_libraries(MsgBufferTests HLA)
+
+
+INSTALL(TARGETS MessageBufferTests MsgBufferTests
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib)
ADD_TEST(LibHLA_MB_Tests MessageBufferTests)
+ADD_TEST(LibHLA_MB_Tests_InC MsgBufferTests)
SET(LIBHLA_CLOCK_SRCS
Clock.cc
Index: MessageBufferTests.cc
===================================================================
RCS file: /sources/certi/certi/libHLA/MessageBufferTests.cc,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -b -r1.1 -r1.2
--- MessageBufferTests.cc 28 Mar 2010 16:08:33 -0000 1.1
+++ MessageBufferTests.cc 10 Jun 2011 14:05:30 -0000 1.2
@@ -199,8 +199,8 @@
MsgBuf.updateReservedBytes();
cout << " now (pseudo) sending a buffer of size " << MsgBuf.size()
<< " bytes ..." << endl;
memcpy(MsgBuf2(0),MsgBuf(0),MsgBuf.reservedBytes);
- printf("MsgBuf =
");MsgBuf.show(MsgBuf(0),MsgBuf.reservedBytes);printf("\n");
- printf("MsgBuf =
");MsgBuf2.show(MsgBuf(0),MsgBuf2.reservedBytes);printf("\n");
+ printf(" MsgBuf =
");MsgBuf.show(MsgBuf(0),MsgBuf.reservedBytes);printf("\n");
+ printf(" MsgBuf2 =
");MsgBuf2.show(MsgBuf(0),MsgBuf2.reservedBytes);printf("\n");
cout << " now (pseudo) receiving buffer header of " << (uint32_t)
MsgBuf.reservedBytes << " bytes ..." << endl;
cout << " reconstruct receive buffer size from buffer header ..."
<<endl;
MsgBuf2.assumeSizeFromReservedBytes();
Index: MsgBuffer.h
===================================================================
RCS file: MsgBuffer.h
diff -N MsgBuffer.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MsgBuffer.h 10 Jun 2011 14:05:30 -0000 1.1
@@ -0,0 +1,492 @@
+/* ----------------------------------------------------------------------------
+ * CERTI - HLA RunTime Infrastructure
+ * Copyright (C) 2002-2005 ONERA
+ *
+ * This program is free software ; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation ; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY ; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program ; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
---------------------------------------------------------------------------- */
+
+#ifndef MSG_BUFFER_H
+#define MSG_BUFFER_H
+
+#ifdef _MSC_VER
+#ifndef FAKED_INTTYPES_DEFINED
+#define FAKED_INTTYPES_DEFINED
+typedef unsigned __int64 uint64_t;
+typedef signed __int64 int64_t;
+typedef unsigned __int32 uint32_t;
+typedef signed __int32 int32_t;
+typedef unsigned __int16 uint16_t;
+typedef signed __int16 int16_t;
+typedef unsigned __int8 uint8_t;
+typedef signed __int8 int8_t;
+typedef short int int_least16_t;
+#endif
+#else
+#include <inttypes.h>
+#endif
+
+#if defined(_WIN32) || defined(__CYGWIN__)
+ #pragma warning(disable: 4251)
+ #define ANY_DLL_EXPORT __declspec(dllexport)
+ #define ANY_DLL_IMPORT __declspec(dllimport)
+ #define ANY_DLL_LOCAL
+#else
+ #if (__GNUC__ >= 4)
+ #define ANY_DLL_EXPORT __attribute__ ((visibility("default")))
+ #define ANY_DLL_IMPORT __attribute__ ((visibility("default")))
+ #define ANY_DLL_LOCAL __attribute__ ((visibility("hidden")))
+ #else
+ #define ANY_DLL_EXPORT
+ #define ANY_DLL_IMPORT
+ #define ANY_DLL_LOCAL
+ #endif
+#endif
+
+#if defined(HLA_EXPORTS)
+ #define MB_EXPORT ANY_DLL_EXPORT
+#else
+ #define MB_EXPORT ANY_DLL_IMPORT
+#endif
+
+/* define our own boolean type */
+#ifndef bool_t
+typedef uint8_t bool_t;
+#define bFALSE 0
+#define bTRUE 1
+#endif
+
+#define MB_UINT64_SWAP_LE_BE_CONSTANT(val) ((uint64_t) ( \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x00000000000000ffU)) << 56) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x000000000000ff00U)) << 40) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x00000000ff000000U)) << 8) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0xff00000000000000U)) >> 56)))
+
+#ifdef HOST_IS_BIG_ENDIAN
+ #define MB_UINT64_SWAP_LE_BE(val) (MB_UINT64_SWAP_LE_BE_CONSTANT(val))
+ #define MB_UINT64_TO_BE(val) (val)
+ #define MB_UINT64_TO_LE(val) (MB_UINT64_SWAP_LE_BE (val))
+#else
+ #define MB_UINT64_SWAP_LE_BE(val) (MB_UINT64_SWAP_LE_BE_CONSTANT(val))
+ #define MB_UINT64_TO_BE(val) (MB_UINT64_SWAP_LE_BE (val))
+ #define MB_UINT64_TO_LE(val) (val)
+#endif
+
+#define MB_UINT64_FROM_BE(val) (MB_UINT64_TO_BE(val))
+#define MB_UINT64_FROM_LE(val) (MB_UINT64_TO_LE(val))
+#define MB_ENCODE_DOUBLE_TO_UINT64BE(val) (MB_UINT64_TO_BE
(*(uint64_t*)(val)))
+#define MB_DECODE_DOUBLE_FROM_UINT64BE(val) (MB_UINT64_FROM_BE
(*(uint64_t*)(val)))
+
+/*
+ * Basic bit swapping functions
+ */
+#define MB_UINT16_SWAP_BYTES(val) ((uint16_t) ( \
+ (((uint16_t) (val) & (uint16_t) 0x00ffU) << 8) | \
+ (((uint16_t) (val) & (uint16_t) 0xff00U) >> 8)))
+
+#define MB_UINT32_SWAP_BYTES(val) ((uint32_t) ( \
+ (((uint32_t) (val) & (uint32_t) 0x000000ffU) << 24) | \
+ (((uint32_t) (val) & (uint32_t) 0x0000ff00U) << 8) | \
+ (((uint32_t) (val) & (uint32_t) 0x00ff0000U) >> 8) | \
+ (((uint32_t) (val) & (uint32_t) 0xff000000U) >> 24)))
+
+#if defined (__x86_64__)
+# define MB_INT64_CONSTANT(val) (val##L)
+# define MB_UINT64_FORMAT "lu"
+# define MB_XINT64_FORMAT "lx"
+# define MB_INT64_FORMAT "l"
+#else
+# define MB_INT64_CONSTANT(val) (val##LL)
+# define MB_UINT64_FORMAT "llu"
+# define MB_XINT64_FORMAT "llx"
+# define MB_INT64_FORMAT "ll"
+#endif
+
+#define MB_UINT64_SWAP_BYTES(val) ((uint64_t) ( \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x00000000000000ffU)) << 56) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x000000000000ff00U)) << 40) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x0000000000ff0000U)) << 24) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x00000000ff000000U)) << 8) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x000000ff00000000U)) >> 8) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x0000ff0000000000U)) >> 24) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0x00ff000000000000U)) >> 40) | \
+ (((uint64_t) (val) & \
+ (uint64_t) MB_INT64_CONSTANT(0xff00000000000000U)) >> 56)))
+
+#include <string.h>
+#define DEFAULT_MESSAGE_BUFFER_SIZE 255
+/*
+ * We reserve 5 bytes at the beginning of the buffer
+ * The first byte is a bitset which is used to
+ * to tell if the buffer is big or little endian
+ * The 4 following bytes is for an uint32_t which
+ * may be used to store the buffer size
+ */
+#define MB_RESERVED_BYTES 5
+
+/* BEGIN_C_DECLS should be used at the beginning of your declarations,
+ so that C++ compilers don't mangle their names. Use END_C_DECLS at
+ the end of C declarations. */
+#undef BEGIN_C_DECLS
+#undef END_C_DECLS
+#ifdef __cplusplus
+# define BEGIN_C_DECLS extern "C" {
+# define END_C_DECLS }
+#else
+# define BEGIN_C_DECLS /* empty */
+# define END_C_DECLS /* empty */
+#endif
+
+/**
+ * MessageBuffer is a class managing a byte buffer for
+ * portable heterogeneous Message exchange.
+ * It provides member functions to read and write basics types
+ * [unsigned] int 8/16/32/64, float, double, byte etc...
+ * The message buffer will encode the written [type] data with proper
+ * byte ordering and padding.
+ * A message buffer handles heterogeneous write/read pair, in fact
+ * it the central class for heterogeneity handling.
+ * One must read from the buffer in the exact order the write was done.
+ * MessageBuffer is dynamically sized, however reallocation are
+ * only done when needed.
+ */
+typedef struct MsgBuffer {
+
+ /** The buffer itself */
+ uint8_t* buffer;
+ /** The provisioned buffer size */
+ uint32_t bufferMaxSize;
+ /** Endianess toggle */
+ bool_t bufferHasMyEndianness;
+ /**
+ * The write offset is the offset of the buffer
+ * where the next write operation will write to.
+ */
+ uint32_t writeOffset;
+ /**
+ * The read offset is the offset of the buffer
+ * where the next read operation will read from.
+ */
+ uint32_t readOffset;
+} MsgBuffer_t;
+
+BEGIN_C_DECLS
+
+/**
+ * Return true if the host is BidEndian
+ */
+MB_EXPORT bool_t MB_HostIsBigEndian();
+
+/**
+ * Return true if the host is LittleEndian
+ */
+MB_EXPORT bool_t MB_HostIsLittleEndian();
+
+/**
+ * Show n bytes of data content in hexa on stdout.
+ */
+MB_EXPORT void MB_show(void* data, uint32_t n);
+
+
+/**
+ * Default message buffer ructor.
+ * The default message buffer size is DEFAULT_MESSAGE_BUFFER_SIZE.
+ * @param[in,out] msg pointer to the message buffer structure
+ */
+MB_EXPORT
+void
+MB_create(MsgBuffer_t* msg);
+
+/**
+ * Constructor with size.
+ * Build a MessageBuffer with provisioned bufSize buffer.
+ * @param[in,out] msg pointer to the message buffer structure
+ * @param[in] bufferMaxSize, the [initial] maixmum size of the buffer
+ */
+MB_EXPORT
+void
+MB_createWithSize(MsgBuffer_t* msg, uint32_t bufferMaxSize);
+
+/**
+ * Destructor
+ * @param[in,out] msg pointer to the message buffer structure
+ */
+MB_EXPORT
+void
+MB_destroy(MsgBuffer_t* msg);
+
+/**
+ * Return the current buffer size (in bytes).
+ * This is the size in bytes
+ * of the element that were written to the buffer.
+ * @return the current buffer size
+ */
+MB_EXPORT
+uint32_t
+MB_size(MsgBuffer_t* msg);
+
+/**
+ * Return the current maximum buffer size (in bytes).
+ * This is the size of the allocated buffer.
+ * @return the current buffer maximum size
+ */
+MB_EXPORT
+uint32_t
+MB_maxSize(MsgBuffer_t* msg);
+
+/**
+ * Assume the buffer is big endian.
+ */
+MB_EXPORT
+void
+MB_assumeBufferIsBigEndian(MsgBuffer_t* msg);
+
+/**
+ * Assume the buffer is little endian.
+ */
+MB_EXPORT
+void
+MB_assumeBufferIsLittleEndian(MsgBuffer_t* msg);
+
+/**
+ * Reset buffer state
+ * This method should be called if ones want
+ * to re-use a buffer in order to avoid reallocation.
+ */
+MB_EXPORT
+void
+MB_reset(MsgBuffer_t* msg);
+
+/**
+ * Seek buffer in order to write at specified place
+ * Will set the write pointer to the seeked offset.
+ */
+MB_EXPORT
+void MB_seek_write(MsgBuffer_t* msg, uint32_t offset);
+
+/**
+ * Resize the current maximum buffer size (in bytes).
+ * This is the size of the allocated buffer.
+ * @param[in] the new buffer maximum size
+ * @return the new buffer maximum size
+ */
+MB_EXPORT
+uint32_t MB_resize(MsgBuffer_t* msg, uint32_t newSize);
+
+/**
+ * Assume that the underlying buffer has
+ * the specified size.
+ * This may be used after ones get raw pointer
+ * to the underlying buffer and has written to the buffer
+ * using memcpy/recv.
+ * @param[in] size the assumed size
+ */
+MB_EXPORT
+void MB_assumeSize(MsgBuffer_t* msg, uint32_t size);
+
+/**
+ * Assume that the underlying buffer has the size
+ * specified by the reserved bytes header.
+ * The method will checked the endianity of the buffer
+ * from the first reserved byte and then assume that
+ * the buffer has size specified by the following 4 bytes.
+ * A reallocation of the underlying memory buffer will
+ * be done if necessary.
+ */
+MB_EXPORT
+void MB_assumeSizeFromReservedBytes(MsgBuffer_t* msg);
+
+#define MB_DECLARE_SIGNED(type) \
+ MB_EXPORT int32_t \
+ MB_write_##type##s(MsgBuffer_t* msg, type##_t* data, uint32_t n) {
\
+ return MB_write_u##type##s(msg, (u##type##_t*)(data),n); \
+} \
+\
+MB_EXPORT int32_t \
+MB_read_##type##s(MsgBuffer_t* msg, type##_t* data, uint32_t n) { \
+ return MB_read_u##type##s(msg, (u##type##_t*)(data),n); \
+} \
+
+
+#define MB_DECLARE_SINGLE_READ_WRITE(type,suffix) \
+ MB_DECLARE_SINGLE_READ_WRITE_(type,type##suffix)
+
+#define MB_DECLARE_SINGLE_READ_WRITE2(type) \
+ MB_DECLARE_SINGLE_READ_WRITE_(type,type)
+
+
+#define MB_DECLARE_SINGLE_READ_WRITE_(type,datatype) \
+ int32_t \
+ MB_write_##type(MsgBuffer_t* msg, datatype data) { \
+ return MB_write_##type##s(msg, &data,1); \
+} \
+\
+MB_EXPORT int32_t \
+MB_read_##type(MsgBuffer_t* msg, datatype* data) { \
+ return MB_read_##type##s(msg,data,1); \
+} \
+\
+MB_EXPORT datatype MB_get_##type(MsgBuffer_t* msg) {\
+ datatype retval; \
+ MB_read_##type##s(msg,&retval,1);\
+ return retval; \
+}
+
+MB_EXPORT
+int32_t
+MB_write_uint8s(MsgBuffer_t* msg, uint8_t* data, uint32_t n);
+
+MB_EXPORT
+int32_t
+MB_read_uint8s(MsgBuffer_t* msg, uint8_t* data, uint32_t n);
+
+MB_DECLARE_SINGLE_READ_WRITE(uint8,_t)
+MB_DECLARE_SIGNED(int8)
+MB_DECLARE_SINGLE_READ_WRITE(int8,_t)
+
+MB_EXPORT
+int32_t
+MB_write_chars(MsgBuffer_t* msg, char* data, uint32_t n) {
+ return MB_write_uint8s(msg,(uint8_t*)(data),n);
+}
+
+MB_EXPORT
+int32_t
+MB_read_chars(MsgBuffer_t* msg, char* data, uint32_t n) {
+ return MB_read_uint8s(msg,(uint8_t*)(data),n);
+}
+MB_DECLARE_SINGLE_READ_WRITE2(char)
+
+#define MB_write_bytes MB_write_chars
+#define MB_read_bytes MB_read_chars
+#define MB_write_byte MB_write_char
+#define MB_read_byte MB_read_char
+
+MB_EXPORT
+int32_t
+MB_write_uint16s(MsgBuffer_t* msg, uint16_t* data, uint32_t n);
+
+MB_EXPORT
+int32_t
+MB_read_uint16s(MsgBuffer_t* msg, uint16_t* data, uint32_t n);
+
+MB_DECLARE_SINGLE_READ_WRITE(uint16,_t)
+MB_DECLARE_SIGNED(int16)
+MB_DECLARE_SINGLE_READ_WRITE(int16,_t)
+
+MB_EXPORT
+int32_t
+MB_write_uint32s(MsgBuffer_t* msg, uint32_t* data, uint32_t n);
+
+MB_EXPORT
+int32_t
+MB_read_uint32s(MsgBuffer_t* msg, uint32_t* data, uint32_t n);
+
+MB_DECLARE_SINGLE_READ_WRITE(uint32,_t)
+MB_DECLARE_SIGNED(int32)
+MB_DECLARE_SINGLE_READ_WRITE(int32,_t)
+
+MB_EXPORT
+int32_t
+MB_write_uint64s(MsgBuffer_t* msg, uint64_t* data, uint32_t n);
+
+MB_EXPORT
+int32_t
+MB_read_uint64s(MsgBuffer_t* msg, uint64_t* data, uint32_t n);
+
+MB_DECLARE_SINGLE_READ_WRITE(uint64,_t)
+MB_DECLARE_SIGNED(int64)
+MB_DECLARE_SINGLE_READ_WRITE(int64,_t)
+
+MB_EXPORT
+int32_t
+MB_write_floats(MsgBuffer_t* msg, float* data, uint32_t n);
+
+MB_EXPORT
+int32_t
+MB_read_floats(MsgBuffer_t* msg, float* data, uint32_t n);
+
+MB_DECLARE_SINGLE_READ_WRITE2(float)
+
+MB_EXPORT
+int32_t
+MB_write_doubles(MsgBuffer_t* msg, double* data, uint32_t n);
+
+MB_EXPORT
+int32_t
+MB_read_doubles(MsgBuffer_t* msg, double* data, uint32_t n);
+
+MB_DECLARE_SINGLE_READ_WRITE2(double)
+
+MB_EXPORT
+int32_t
+MB_write_string(MsgBuffer_t* msg, char* str);
+
+MB_EXPORT
+void
+MB_read_string(MsgBuffer_t* msg, char* str, uint32_t slen);
+
+MB_EXPORT
+int32_t
+MB_write_bool(MsgBuffer_t* msg, bool_t toggle) {
+ if(toggle) {
+ return MB_write_uint8(msg,1);
+ } else {
+ return MB_write_uint8(msg,0);
+ }
+}
+
+MB_EXPORT
+bool_t
+MB_read_bool(MsgBuffer_t* msg) {
+ return (1==MB_get_uint8(msg));
+}
+
+/**
+ * Update the reserved bytes area.
+ * @post the reserved bytes are up-to-date.
+ */
+MB_EXPORT
+void MB_updateReservedBytes(MsgBuffer_t* msg);
+
+/*
+ * Pseudo index operator.
+ * This will be used in send/receive socket call.
+ * @return address of the underlying buffer + offset.
+ */
+MB_EXPORT
+void* MB_get(MsgBuffer_t* msg, uint32_t offset);
+
+END_C_DECLS
+#endif
Index: MsgBuffer.c
===================================================================
RCS file: MsgBuffer.c
diff -N MsgBuffer.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MsgBuffer.c 10 Jun 2011 14:05:30 -0000 1.1
@@ -0,0 +1,466 @@
+/* ----------------------------------------------------------------------------
+ * CERTI - HLA RunTime Infrastructure
+ * Copyright (C) 2002-2005 ONERA
+ *
+ * This program is free software ; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation ; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY ; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program ; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ * USA
+ *
+ *
----------------------------------------------------------------------------*/
+
+#include "MsgBuffer.h"
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+static void MB_initialize(MsgBuffer_t* msg) {
+ assert(msg);
+ msg->buffer = NULL;
+ msg->bufferMaxSize = DEFAULT_MESSAGE_BUFFER_SIZE;
+ msg->bufferHasMyEndianness = bTRUE;
+ msg->writeOffset = MB_RESERVED_BYTES;
+ msg->readOffset = MB_RESERVED_BYTES;
+} /* end of MB_initialize() */
+
+static void MB_error(MsgBuffer_t* msg, const char* funName, uint32_t sz,
uint32_t acsz) {
+ fprintf(stderr,"%s::invalid access of size <%d> inside a buffer of
accessible size <%d> (writeOffset=%d,readOffset=%d)\n",
+ funName, sz, acsz, msg->writeOffset,msg->readOffset);
+ exit(EXIT_FAILURE);
+} /* end of MB_error */
+
+/**
+ * Reallocate the underlying buffer to size n.
+ * Reallocation will be done only if it is necessary.
+ */
+static void MB_reallocate(MsgBuffer_t* msg, uint32_t n);
+
+/**
+ * Update reserved bytes in order to indicate
+ * the actual size of the buffer.
+ */
+static void MB_setSizeInReservedBytes(MsgBuffer_t* msg, uint32_t n);
+
+
+bool_t
+MB_HostIsBigEndian() {
+#ifdef HOST_IS_BIG_ENDIAN
+ return bTRUE;
+#else
+ return bFALSE;
+#endif
+}
+
+bool_t
+MB_HostIsLittleEndian() {
+#ifdef HOST_IS_BIG_ENDIAN
+ return bFALSE;
+#else
+ return bTRUE;
+#endif
+}
+
+void
+MB_show(void* data, uint32_t n) {
+ uint32_t i;
+ uint8_t* u8data = (uint8_t*)(data);
+ for (i=0;i<n;++i) {
+ if ((0==(i%2))&&(i>0)) {
+ printf(" ");
+ }
+ printf("%02X",u8data[i]);
+ }
+}
+
+void
+MB_create(MsgBuffer_t* msg) {
+ MB_initialize(msg);
+ MB_reallocate(msg,DEFAULT_MESSAGE_BUFFER_SIZE);
+ memset(msg->buffer,0,msg->bufferMaxSize);
+} /* end of MB_MessageBuffer() */
+
+void
+MB_createWithSize(MsgBuffer_t* msg, uint32_t n) {
+ MB_initialize(msg);
+ MB_reallocate(msg,n);
+ memset(msg->buffer,0,msg->bufferMaxSize);
+} /* end of MB_MessageBuffer(uint32_t) */
+
+/*
+ * FIXME we may put a COMPILED FLAG here in order
+ * to prevent reallocation beside the first one (when buffer==NULL)
+ */
+void MB_reallocate(MsgBuffer_t* msg, uint32_t n) {
+ uint8_t* oldbuf = msg->buffer;
+ /* (re)allocation is done iff
+ * - buffer is NULL (never allocated)
+ * or
+ * - requested size exceed current bufferMaxSize
+ */
+ if ((NULL==msg->buffer) || (n > msg->bufferMaxSize)) {
+ /* FIXME should try/catch for alloc error
+ * ands wrap-up inside RTIinternalError
+ */
+ msg->buffer = calloc(n,sizeof(uint8_t));
+ msg->bufferMaxSize = n;
+ /*
+ * If oldbuf wasn't null then copy
+ * oldbuf in the new buf
+ * the writeOffset should be valid.
+ */
+ if (NULL!=oldbuf) {
+ memcpy(msg->buffer, oldbuf, msg->writeOffset);
+ free(oldbuf);
+ oldbuf = NULL;
+ } else {
+ MB_updateReservedBytes(msg);
+ }
+ }
+} /* end of MB_MessageBuffer(MsgBuffer_t*, uint32_t) */
+
+void
+MB_destroy(MsgBuffer_t* msg) {
+ if (NULL!=msg->buffer) {
+ free(msg->buffer);
+ msg->buffer = NULL;
+ }
+} /* end of MB_~MessageBuffer() */
+
+uint32_t MB_size(MsgBuffer_t* msg) {
+ return msg->writeOffset;
+}
+
+uint32_t MB_maxSize(MsgBuffer_t* msg) {
+ return msg->bufferMaxSize;
+}
+
+void MB_assumeBufferIsBigEndian(MsgBuffer_t* msg) {
+ msg->bufferHasMyEndianness = MB_HostIsBigEndian();
+ msg->buffer[0] = 0x01;
+} /* end of MB_assumeBufferIsBigEndian() */
+
+void MB_assumeBufferIsLittleEndian(MsgBuffer_t* msg) {
+ msg->bufferHasMyEndianness = MB_HostIsLittleEndian();
+ msg->buffer[0] = 0x00;
+} /* end of MB_assumeBufferIsLittleEndian() */
+
+void MB_reset(MsgBuffer_t* msg) {
+ msg->bufferHasMyEndianness = bTRUE;
+ msg->writeOffset = MB_RESERVED_BYTES;
+ msg->readOffset = MB_RESERVED_BYTES;
+ MB_updateReservedBytes(msg);
+} /* MB_resetBuffer() */
+
+uint32_t MB_resize(MsgBuffer_t* msg, uint32_t newSize) {
+ MB_reallocate(msg,newSize);
+ return msg->bufferMaxSize;
+}
+
+void MB_assumeSize(MsgBuffer_t* msg, uint32_t size) {
+ /* This is done in order to overflow
+ * buffer max size but this may well be
+ * an error (FIXME should throw an exception ?)
+ */
+ if (size <= msg->bufferMaxSize) {
+ msg->writeOffset = size;
+ } else {
+ msg->writeOffset = msg->bufferMaxSize;
+ }
+}
+
+void MB_assumeSizeFromReservedBytes(MsgBuffer_t* msg) {
+ uint32_t toBeAssumedSize;
+ /* verify endianity from reserved byte 0 */
+ if (msg->buffer[0]==0x01) {
+ MB_assumeBufferIsBigEndian(msg);
+ } else {
+ MB_assumeBufferIsLittleEndian(msg);
+ }
+ /* read size from reserved bytes 1..4 */
+ msg->readOffset = 1;
+ toBeAssumedSize = MB_get_uint32(msg);
+ /* reallocation may be needed */
+ MB_reallocate(msg,toBeAssumedSize);
+ MB_assumeSize(msg,toBeAssumedSize);
+} /* end of assumeSizeFromReservedBytes */
+
+void MB_setSizeInReservedBytes(MsgBuffer_t* msg, uint32_t n) {
+ uint32_t oldWR_Offset;
+ /* backup write Offset */
+ oldWR_Offset = msg->writeOffset;
+ /* update size in reserved bytes */
+ msg->writeOffset = 1;
+ MB_write_uint32(msg,n);
+ /* restore writeOffset */
+ msg->writeOffset = oldWR_Offset;
+} /* end of setSizeInReservedBytes */
+
+int32_t
+MB_write_uint8s(MsgBuffer_t* msg, uint8_t* data, uint32_t n) {
+
+ if (n >= (msg->bufferMaxSize - msg->writeOffset)) {
+ /* reallocate buffer on-demand */
+ MB_reallocate(msg, msg->bufferMaxSize+
(n-(msg->bufferMaxSize-msg->writeOffset))
+ + DEFAULT_MESSAGE_BUFFER_SIZE);
+ }
+ /* copy data */
+ memcpy(msg->buffer+msg->writeOffset, data, n);
+ /* update write offset */
+ msg->writeOffset += n;
+ return (msg->writeOffset-n);
+} /* end of MB_write_uint8s(uint8_t*, uint32_t) */
+
+int32_t
+MB_read_uint8s(MsgBuffer_t* msg, uint8_t* data, uint32_t n) {
+ if (n + msg->readOffset > msg->writeOffset) {
+ MB_error(msg,"MB_read_uint8s",n,msg->writeOffset-msg->readOffset);
+ }
+
+ memcpy(data, msg->buffer+msg->readOffset, n);
+ msg->readOffset += n;
+ return (msg->readOffset-n);
+} /* end of MB_read_uint8s(uint8_t*, uint32_t) */
+
+int32_t
+MB_write_uint16s(MsgBuffer_t* msg, uint16_t* data, uint32_t n) {
+ uint32_t i;
+ uint16_t an_uint16;
+
+ if ((2*n) >= (msg->bufferMaxSize - msg->writeOffset)) {
+ /* reallocate buffer on-demand */
+ MB_reallocate(msg,msg->bufferMaxSize+ (2*n)-(msg->bufferMaxSize
- msg->writeOffset)
+ + DEFAULT_MESSAGE_BUFFER_SIZE);
+ }
+
+ /* do not swap byte if it is not necessary */
+ if (msg->bufferHasMyEndianness) {
+ memcpy(msg->buffer+msg->writeOffset, data, 2*n);
+ msg->writeOffset += 2*n;
+ } else {
+ for (i=0; i<n; ++i) {
+ an_uint16 = MB_UINT16_SWAP_BYTES(data[i]);
+ memcpy(msg->buffer+msg->writeOffset, &an_uint16, 2);
+ msg->writeOffset += 2;
+ }
+ }
+ return (msg->writeOffset-2*n);
+} /* end of MB_write_uint16s(uint16_t*, uint32_t) */
+
+int32_t
+MB_read_uint16s(MsgBuffer_t* msg, uint16_t* data, uint32_t n) {
+ uint32_t i;
+ uint16_t an_uint16;
+
+ if (2*n + msg->readOffset > msg->writeOffset) {
+ MB_error(msg,"MB_read_uint16s",n,msg->writeOffset-msg->readOffset);
+ }
+
+ /* do not swap byte if it is not necessary */
+ if (msg->bufferHasMyEndianness) {
+ memcpy(data, msg->buffer+msg->readOffset, 2*n);
+ msg->readOffset += 2*n;
+ } else {
+ for (i=0; i<n; ++i) {
+ memcpy(&an_uint16, msg->buffer+msg->readOffset,2);
+ data[i] = MB_UINT16_SWAP_BYTES(an_uint16);
+ msg->readOffset += 2;
+ }
+ }
+
+ return (msg->readOffset-2*n);
+} /* end of MB_read_uint16s(uint16_t*, uint32_t) */
+
+int32_t
+MB_write_uint32s(MsgBuffer_t* msg, uint32_t* data, uint32_t n) {
+ uint32_t i;
+ uint32_t an_uint32;
+
+ if ((4*n) >= (msg->bufferMaxSize - msg->writeOffset)) {
+ /* reallocate buffer on-demand */
+ MB_reallocate(msg, msg->bufferMaxSize+
(4*n)-(msg->bufferMaxSize - msg->writeOffset)
+ + DEFAULT_MESSAGE_BUFFER_SIZE);
+ }
+
+ /* do not swap byte if it is not necessary */
+ if (msg->bufferHasMyEndianness) {
+ memcpy(msg->buffer+msg->writeOffset, data, 4*n);
+ msg->writeOffset += 4*n;
+ } else {
+ for (i=0; i<n; ++i) {
+ an_uint32 = MB_UINT32_SWAP_BYTES(data[i]);
+ memcpy(msg->buffer+msg->writeOffset,&an_uint32,4);
+ msg->writeOffset += 4;
+ }
+ }
+ return (msg->writeOffset-4*n);
+} /* end of MB_write_uint32s */
+
+int32_t
+MB_read_uint32s(MsgBuffer_t* msg, uint32_t* data, uint32_t n) {
+ uint32_t i;
+ uint32_t an_uint32;
+
+ if (4*n + msg->readOffset > msg->writeOffset) {
+ MB_error(msg,"MB_read_uint32s",n,msg->writeOffset-msg->readOffset);
+ }
+
+ /* do not swap byte if it is not necessary */
+ if (msg->bufferHasMyEndianness) {
+ memcpy(data, msg->buffer+msg->readOffset, 4*n);
+ msg->readOffset += 4*n;
+ } else {
+ for (i=0; i<n; ++i) {
+ memcpy(&an_uint32,msg->buffer+msg->readOffset,4);
+ data[i] = MB_UINT32_SWAP_BYTES(an_uint32);
+ msg->readOffset += 4;
+ }
+ }
+ return (msg->readOffset-4*n);
+} /* end of MB_read_uint32s */
+
+int32_t
+MB_write_uint64s(MsgBuffer_t* msg, uint64_t* data, uint32_t n) {
+ uint32_t i;
+ union deux32 {
+ uint32_t ui32[2];
+ uint64_t ui64;
+ } a_deux32;
+ uint32_t an_uint32;
+
+ if ((8*n) >= (msg->bufferMaxSize - msg->writeOffset)) {
+ /* reallocate buffer on-demand */
+ MB_reallocate(msg,msg->bufferMaxSize+ (8*n)-(msg->bufferMaxSize
- msg->writeOffset)
+ + DEFAULT_MESSAGE_BUFFER_SIZE);
+ }
+
+ /* do not swap byte if it is not necessary */
+ if (msg->bufferHasMyEndianness) {
+ memcpy(msg->buffer+msg->writeOffset, data, 8*n);
+ msg->writeOffset += 8*n;
+ } else {
+ for (i=0; i<n; ++i) {
+ a_deux32.ui64 = data[i];
+ an_uint32 = MB_UINT32_SWAP_BYTES(a_deux32.ui32[1]);
+ memcpy(msg->buffer+msg->writeOffset,&an_uint32,4);
+ msg->writeOffset += 4;
+ an_uint32 = MB_UINT32_SWAP_BYTES(a_deux32.ui32[0]);
+ memcpy(msg->buffer+msg->writeOffset,&an_uint32,4);
+ msg->writeOffset += 4;
+ }
+ }
+ return (msg->writeOffset-8*n);
+} /* end of MB_write_uint64s */
+
+int32_t
+MB_read_uint64s(MsgBuffer_t* msg, uint64_t* data, uint32_t n) {
+ uint32_t i;
+ union deux32 {
+ uint32_t ui32[2];
+ uint64_t ui64;
+ } a_deux32;
+ uint32_t an_uint32;
+
+ if (8*n + msg->readOffset > msg->writeOffset) {
+ MB_error(msg,"read_uint64s",n,msg->writeOffset-msg->readOffset);
+ }
+
+ /* do not swap byte if it is not necessary */
+ if (msg->bufferHasMyEndianness) {
+ memcpy(data, msg->buffer+msg->readOffset, 8*n);
+ msg->readOffset += 8*n;
+ } else {
+ for (i=0; i<n; ++i) {
+ memcpy(&an_uint32,msg->buffer+msg->readOffset,4);
+ a_deux32.ui32[1] = MB_UINT32_SWAP_BYTES(an_uint32);
+ msg->readOffset += 4;
+
+ memcpy(&an_uint32,msg->buffer+msg->readOffset,4);
+ a_deux32.ui32[0] = MB_UINT32_SWAP_BYTES(an_uint32);
+ msg->readOffset += 4;
+
+ data[i] = a_deux32.ui64;
+ }
+ }
+ return (msg->readOffset-8*n);
+}
+
+int32_t
+MB_write_floats(MsgBuffer_t* msg, float* data, uint32_t n) {
+ uint32_t* data32;
+ data32 = (uint32_t*)(data);
+ return MB_write_uint32s(msg,data32,n);
+}
+
+int32_t
+MB_read_floats(MsgBuffer_t* msg, float* data, uint32_t n) {
+ uint32_t* data32;
+ data32 = (uint32_t*)(data);
+ return MB_read_uint32s(msg,data32,n);
+}
+
+int32_t
+MB_write_doubles(MsgBuffer_t* msg, double* data, uint32_t n) {
+ uint64_t* data64;
+ data64 = (uint64_t*)(data);
+ return MB_write_uint64s(msg,data64,n);
+}
+
+int32_t MB_read_doubles(MsgBuffer_t* msg, double* data, uint32_t n) {
+ uint64_t* data64;
+ data64 = (uint64_t*)(data);
+ return MB_read_uint64s(msg,data64,n);
+}
+
+int32_t
+MB_write_string(MsgBuffer_t* msg, char* str) {
+ MB_write_int32(msg,strlen(str));
+ return MB_write_chars(msg,str,strlen(str));
+} /* end of MB_write_string */
+
+void
+MB_read_string(MsgBuffer_t* msg, char* str, uint32_t slen)
+{
+ int32_t len = MB_get_int32(msg);
+
+ if (len + msg->readOffset > msg->writeOffset) {
+ MB_error(msg,"MB_read_string",len,msg->writeOffset-msg->readOffset);
+ }
+
+ if (slen>len) {
+ strncpy(str,(char*)(msg->buffer+msg->readOffset),len);
+ /* we should terminate the C string */
+ str[len]='\0';
+ } else {
+ MB_error(msg,"MB_read_string NOT enough output storage",len,slen);
+ }
+ msg->readOffset += len;
+} /* end of MB_read_string */
+
+void
+MB_updateReservedBytes(MsgBuffer_t* msg) {
+ /* set up buffer endianess */
+ if ((MB_HostIsBigEndian() && msg->bufferHasMyEndianness) ||
+ (MB_HostIsLittleEndian() && !msg->bufferHasMyEndianness)) {
+ msg->buffer[0] = 0x01;
+ } else {
+ msg->buffer[0] = 0x00;
+ }
+ /* set up size */
+ MB_setSizeInReservedBytes(msg,MB_size(msg));
+} /* end of updateReservedBytes */
+
+void*
+MB_get(MsgBuffer_t* msg, uint32_t offset) {
+ return msg->buffer+offset;
+}
+
Index: MsgBufferTests.c
===================================================================
RCS file: MsgBufferTests.c
diff -N MsgBufferTests.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ MsgBufferTests.c 10 Jun 2011 14:05:30 -0000 1.1
@@ -0,0 +1,237 @@
+/* ----------------------------------------------------------------------------
+ * CERTI - HLA RunTime Infrastructure
+ * Copyright (C) 2002-2005 ONERA
+ *
+ * This file is part of CERTI
+ *
+ * CERTI is free software ; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation ; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * CERTI is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY ; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program ; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ *
----------------------------------------------------------------------------*/
+
+#include "MsgBuffer.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#define MYVSTR_SIZE 2048
+
+void messageBufferTests(MsgBuffer_t* MsgBuf) {
+ MsgBuffer_t MsgBuf2;
+ char* str = "a char* string";
+ char* stdstr = "a NULL terminated char* string";
+ uint8_t u8 = 232;
+ int8_t i8 = -125;
+ uint16_t u16 = 0xFFAA;
+ int16_t i16 = -576;
+ uint32_t u32 = 0xFFAAEEBB;
+ int32_t i32 = -9999;
+ uint64_t u64 = 0xFFEEDDCC;
+ int64_t i64 = -1000000000;
+ float f32 = 3.1415927f;
+ double d64 = 2.7182818;
+ bool_t trueBool = bTRUE;
+ bool_t falseBool = bFALSE;
+ char* vstr = NULL;
+ char vstdstr[MYVSTR_SIZE];
+ uint8_t vu8 = 0;
+ int8_t vi8 = 0;
+ uint16_t vu16 = 0;
+ int16_t vi16 = 0;
+ uint32_t vu32 = 0;
+ int32_t vi32 = 0;
+ uint64_t vu64 = 0;
+ int64_t vi64 = 0;
+ float vf32 = 0.0;
+ double vd64 = 0.0;
+ bool_t vtrueBool = bFALSE;
+ bool_t vfalseBool = bTRUE;
+ printf("Testing MsgBuffer structure BEGIN...\n");
+ printf(" Current (Default) MsgBuffer MaxSize =
%d\n",MB_maxSize(MsgBuf));
+ printf(" Current (initially void) MessageBuffer size =
%d\n",MB_size(MsgBuf));
+ printf(" Encoding to buffer...\n");
+ printf(" bytes string = %s\n",str);
+ MB_write_uint32(MsgBuf, strlen(str));
+ MB_write_bytes(MsgBuf, str,strlen(str));
+ MB_write_uint8(MsgBuf, u8);
+ MB_write_uint16(MsgBuf, u16);
+ MB_write_uint32(MsgBuf, u32);
+ MB_write_uint64(MsgBuf, u64);
+ MB_write_int8(MsgBuf, i8);
+ MB_write_int16(MsgBuf, i16);
+ MB_write_int32(MsgBuf, i32);
+ MB_write_int64(MsgBuf, i64);
+ MB_write_float(MsgBuf, f32);
+ MB_write_double(MsgBuf, d64);
+ MB_write_bool(MsgBuf, trueBool);
+ MB_write_bool(MsgBuf, falseBool);
+ printf(" std::string = %s\n",stdstr);
+ MB_write_string(MsgBuf,stdstr);
+ printf(" Current MessageBuffer size =
%d\n",MB_size(MsgBuf));
+ printf(" Decoding from buffer...\n");
+ MB_read_uint32(MsgBuf,&vu32);
+ vstr = malloc(sizeof(char)*(vu32+1));
+ memset(vstr,'\0',vu32+1);
+ MB_read_bytes(MsgBuf,vstr,vu32);
+ printf(" bytes string = %s\n",vstr); assert(0==strcmp(str,vstr));
+ MB_read_uint8(MsgBuf,&vu8); assert(vu8==u8);
+ MB_read_uint16(MsgBuf,&vu16); assert(vu16==u16);
+ MB_read_uint32(MsgBuf,&vu32); assert(vu32==u32);
+ MB_read_uint64(MsgBuf,&vu64); assert(vu64==u64);
+ MB_read_int8(MsgBuf,&vi8); assert(vi8==i8);
+ MB_read_int16(MsgBuf,&vi16); assert(vi16==i16);
+ MB_read_int32(MsgBuf,&vi32); assert(vi32==i32);
+ MB_read_int64(MsgBuf,&vi64); assert(vi64==i64);
+ MB_read_float(MsgBuf,&vf32); assert(vf32==f32);
+ MB_read_double(MsgBuf,&vd64); assert(vd64==d64);
+ vtrueBool = MB_read_bool(MsgBuf); assert(trueBool==vtrueBool);
+ vfalseBool = MB_read_bool(MsgBuf); assert(falseBool==vfalseBool);
+ MB_read_string(MsgBuf,vstdstr,MYVSTR_SIZE);
assert(0==strcmp(vstdstr,stdstr));
+ printf(" std::string = %s\n",vstdstr);
+ printf(" All encoded/decoded values are equal.\n");
+ printf(" Now reset buffer.\n");
+ MB_reset(MsgBuf);
+ printf(" Re-write on buffer...\n");
+ MB_write_uint8(MsgBuf,u8);
+ MB_write_uint16(MsgBuf,u16);
+ MB_write_uint32(MsgBuf,u32);
+ MB_write_uint64(MsgBuf,u64);
+ MB_write_int8(MsgBuf,i8);
+ MB_write_int16(MsgBuf,i16);
+ MB_write_int32(MsgBuf,i32);
+ MB_write_int64(MsgBuf,i64);
+ MB_write_float(MsgBuf,f32);
+ MB_write_double(MsgBuf,d64);
+ printf(" Read using alternative method...\n");
+ vu8 = MB_get_uint8(MsgBuf); assert(vu8==u8);
+ vu16 = MB_get_uint16(MsgBuf); assert(vu16==u16);
+ vu32 = MB_get_uint32(MsgBuf); assert(vu32==u32);
+ vu64 = MB_get_uint64(MsgBuf); assert(vu64==u64);
+ vi8 = MB_get_int8(MsgBuf); assert(vi8==i8);
+ vi16 = MB_get_int16(MsgBuf); assert(vi16==i16);
+ vi32 = MB_get_int32(MsgBuf); assert(vi32==i32);
+ vi64 = MB_get_int64(MsgBuf); assert(vi64==i64);
+ vf32 = MB_get_float(MsgBuf); assert(vf32==f32);
+ vd64 = MB_get_double(MsgBuf); assert(vd64==d64);
+ printf(" All encoded/decoded values are equal.\n");
+ printf(" Now reset buffer.\n");
+ MB_reset(MsgBuf);
+ printf(" Re-write on buffer...\n");
+ MB_write_uint8(MsgBuf,u8); printf(" Written <"); MB_show(&u8,1);
printf(">\n");
+ MB_write_uint16(MsgBuf,u16);printf(" Written <"); MB_show(&u16,2);
printf(">\n");
+ MB_write_uint32(MsgBuf,u32);printf(" Written <"); MB_show(&u32,4);
printf(">\n");
+ MB_write_uint64(MsgBuf,u64);printf(" Written <"); MB_show(&u64,8);
printf(">\n");
+ MB_write_int8(MsgBuf,i8);printf(" Written <"); MB_show(&i8,1);
printf(">\n");
+ MB_write_int16(MsgBuf,i16);printf(" Written <"); MB_show(&i16,2);
printf(">\n");
+ MB_write_int32(MsgBuf,i32);printf(" Written <"); MB_show(&i32,4);
printf(">\n");
+ MB_write_int64(MsgBuf,i64);printf(" Written <"); MB_show(&i64,8);
printf(">\n");
+ MB_write_float(MsgBuf,f32);printf(" Written <"); MB_show(&f32,4);
printf(">\n");
+ MB_write_double(MsgBuf,d64);printf(" Written <"); MB_show(&d64,8);
printf(">\n");
+
+ printf(" ...and assume it is ");
+ if (MB_HostIsBigEndian()) {
+ printf("Little Endian");
+ MB_assumeBufferIsLittleEndian(MsgBuf);
+ printf(" whereas the host was big\n");
+ }
+ if (MB_HostIsLittleEndian()) {
+ printf("Big Endian");
+ MB_assumeBufferIsBigEndian(MsgBuf);
+ printf(" whereas the host was little\n");
+ }
+ MB_read_uint8(MsgBuf,&vu8); printf(" Read <"); MB_show(&vu8,1);
printf(">\n");
+ MB_read_uint16(MsgBuf,&vu16); printf(" Read <"); MB_show(&vu16,2);
printf(">\n");
+ MB_read_uint32(MsgBuf,&vu32); printf(" Read <"); MB_show(&vu32,4);
printf(">\n");
+ MB_read_uint64(MsgBuf,&vu64); printf(" Read <"); MB_show(&vu64,8);
printf(">\n");
+ MB_read_int8(MsgBuf,&vi8); printf(" Read <"); MB_show(&vi8,1);
printf(">\n");
+ MB_read_int16(MsgBuf,&vi16); printf(" Read <"); MB_show(&vi16,2);
printf(">\n");
+ MB_read_int32(MsgBuf,&vi32); printf(" Read <"); MB_show(&vi32,4);
printf(">\n");
+ MB_read_int64(MsgBuf,&vi64); printf(" Read <"); MB_show(&vi64,8);
printf(">\n");
+ MB_read_float(MsgBuf,&vf32); printf(" Read <"); MB_show(&vf32,4);
printf(">\n");
+ MB_read_double(MsgBuf,&vd64); printf(" Read <"); MB_show(&vd64,8);
printf(">\n");
+ free(vstr);
+
+ printf(" Trying to overload the buffer...\n");
+ printf(" Current (Default) MessageBuffer MaxSize =
%d\n",MB_maxSize(MsgBuf));
+ MB_reset(MsgBuf);
+ u32 = MB_maxSize(MsgBuf)*2;
+ vstr = malloc(sizeof(char)*(u32+1));
+ vstr[u32]='\0';
+ memset(vstr,'A',u32);
+ MB_write_uint32(MsgBuf,u32);
+ MB_write_chars(MsgBuf,vstr,u32);
+ printf(" Written char* is %s\n",vstr);
+ u32 = MB_size(MsgBuf);
+ free(vstr);
+ printf(" Current MessageBuffer MaxSize =
%d\n",MB_maxSize(MsgBuf));
+ assert(MYVSTR_SIZE>u32);
+ MB_read_string(MsgBuf,vstdstr,u32);
+ printf(" Read String is %s\n",vstdstr);
+ printf(" Forcibly resize the buffer to %d\n",MB_maxSize(MsgBuf)*2);
+ MB_resize(MsgBuf,MB_maxSize(MsgBuf)*2);
+ assert(MYVSTR_SIZE>u32);
+ printf(" Current MessageBuffer MaxSize =
%d\n",MB_maxSize(MsgBuf));
+ printf(" Now reset buffer and assume size is =
%d\n",u32);
+ MB_reset(MsgBuf);
+ MB_assumeSize(MsgBuf,u32);
+ MB_read_string(MsgBuf,vstdstr,MYVSTR_SIZE);
+ printf(" (re)Read String is %s\n",vstdstr);
+
+ printf(" preparing a buffer for network send...\n");
+ MB_reset(MsgBuf);
+ MB_write_uint32(MsgBuf,u32);
+ MB_write_uint8(MsgBuf,u8);
+ MB_write_uint16(MsgBuf,u16);
+ MB_updateReservedBytes(MsgBuf);
+ MB_create(&MsgBuf2);
+ printf(" now (pseudo) sending a buffer of size %d bytes
...\n",MB_size(MsgBuf));
+ memcpy(MB_get(&MsgBuf2,0),MB_get(MsgBuf,0),MB_RESERVED_BYTES);
+ printf(" MsgBuf =
");MB_show(MB_get(MsgBuf,0),MB_RESERVED_BYTES);printf("\n");
+ printf(" MsgBuf2 =
");MB_show(MB_get(&MsgBuf2,0),MB_RESERVED_BYTES);printf("\n");
+ printf(" now (pseudo) receiving buffer header of %d bytes
...\n",MB_RESERVED_BYTES);
+ printf(" reconstruct receive buffer size from buffer header ...\n");
+ MB_assumeSizeFromReservedBytes(&MsgBuf2);
+ printf(" seems to contain %d bytes...\n",MB_size(&MsgBuf2));
+ printf(" now (pseudo) receiving buffer content ...\n");
+
memcpy(MB_get(&MsgBuf2,MB_RESERVED_BYTES),MB_get(MsgBuf,MB_RESERVED_BYTES),MB_size(&MsgBuf2));
+ printf(" reading buffer content...\n");
+ vu32 = MB_get_uint32(&MsgBuf2); assert(vu32==u32);
+ vu8 = MB_get_uint8(&MsgBuf2); assert(vu8==u8);
+ vu16 = MB_get_uint16(&MsgBuf2); assert(vu16==u16);
+ MB_destroy(&MsgBuf2);
+ printf("Testing MsgBuffer END.\n");
+} /* end of messageBufferTests */
+
+int
+main(int argc, char **argv) {
+
+ MsgBuffer_t MsgBuf;
+
+ MB_create(&MsgBuf);
+ printf("LibHLA MsgBuffer (pure C language) Tests->BEGIN...\n");
+
+ printf(" Host is ");
+ if (MB_HostIsBigEndian()) {
+ printf("Big Endian");
+ }
+ if (MB_HostIsLittleEndian()) {
+ printf("Little Endian");
+ }
+ printf("\n");
+ messageBufferTests(&MsgBuf);
+
+ MB_destroy(&MsgBuf);
+ printf("LibHLA MsgBuffer (pure C language) Test->END.\n");
+ return 0;
+}
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [certi-cvs] certi/libHLA CMakeLists.txt MessageBufferTests....,
CERTI CVS commits <=