AGX Dynamics  2.35.0.1
include/agxNet/Util.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2022. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, is copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 #pragma once
17 
18 // TODO Integrate with agxStream serialization...
19 
20 namespace agxNet
21 {
22  static inline size_t stringSize(const agx::String& string)
23  {
24  return string.size() + sizeof(agx::UInt32);
25  }
26 
27  static inline agx::UInt8 *writeString(agx::UInt8 *buffer, const agx::String& string)
28  {
29  *(agx::UInt32 *)buffer = htonl((agx::UInt32)string.size());
30  buffer += sizeof(agx::UInt32);
31  strncpy((char *)buffer, string.c_str(), string.size());
32  return buffer + string.size();
33  }
34 
35  static inline agx::UInt8 *writeUInt32(agx::UInt8 *buffer, agx::UInt32 value)
36  {
37  *(agx::UInt32 *)buffer = htonl(value);
38  return buffer + sizeof(agx::UInt32);
39  }
40 
41 #ifdef AGX_DEBUG
42  static inline agx::UInt32 parseUInt32(agx::UInt8 *& buffer, agx::UInt8 *end)
43 #else
44  static inline agx::UInt32 parseUInt32(agx::UInt8 *& buffer, agx::UInt8 * /*end*/)
45 #endif
46  {
47  agxAssert(end - buffer >= (ptrdiff_t)sizeof(agx::UInt32));
48  agx::UInt32 result = ntohl(*(agx::UInt32 *)buffer);
49  buffer += sizeof(agx::UInt32);
50  return result;
51  }
52 
53 #ifdef AGX_DEBUG
54  static inline agx::String parseString(agx::UInt8 *& buffer, agx::UInt8 *end)
55 #else
56  static inline agx::String parseString(agx::UInt8 *& buffer, agx::UInt8 * /*end*/)
57 #endif
58  {
59  agxAssert(end - buffer >= (ptrdiff_t)sizeof(agx::UInt32));
60  agx::UInt32 stringSize = ntohl(*(agx::UInt32 *)buffer);
61  buffer += sizeof(agx::UInt32);
62  agxAssert(agx::UInt32(end - buffer) >= stringSize);
63  agx::String result((const char *)buffer, stringSize);
64  buffer += stringSize;
65  return result;
66  }
67 
68 }
69 
70 
71 #endif /* AGXNET_UTIL_H */
#define agxAssert(expr)
Definition: debug.h:143
Containins classes for sending/reading data over sockets as well as compression functionality.
Definition: MacUtil.h:23
static size_t stringSize(const agx::String &string)
static agx::UInt8 * writeString(agx::UInt8 *buffer, const agx::String &string)
static agx::String parseString(agx::UInt8 *&buffer, agx::UInt8 *)
static agx::UInt32 parseUInt32(agx::UInt8 *&buffer, agx::UInt8 *)
static agx::UInt8 * writeUInt32(agx::UInt8 *buffer, agx::UInt32 value)
uint32_t UInt32
Definition: Integer.h:40
uint8_t UInt8
Definition: Integer.h:38
LinearProbingHashSetImplementation< KeyT, HashT >::iterator end(LinearProbingHashSetImplementation< KeyT, HashT > &set)