AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
include/agxNet/Util.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16#pragma once
17
18// TODO Integrate with agxStream serialization...
19
20namespace 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 * writeUInt32(agx::UInt8 *buffer, agx::UInt32 value)
static agx::String parseString(agx::UInt8 *&buffer, agx::UInt8 *)
static agx::UInt32 parseUInt32(agx::UInt8 *&buffer, agx::UInt8 *)
static agx::UInt8 * writeString(agx::UInt8 *buffer, const agx::String &string)
uint32_t UInt32
Definition Integer.h:32
uint8_t UInt8
Definition Integer.h:30