11 #define AI_LIL_ENDIAN 1234
12 #define AI_BIG_ENDIAN 4321
15 #define AI_BYTEORDER __BYTE_ORDER
17 #define AI_BYTEORDER AI_LIL_ENDIAN
20 #if AI_BYTEORDER == AI_LIL_ENDIAN
21 #define AI_SwapLE16(X) (X)
22 #define AI_SwapLE32(X) (X)
23 #define AI_SwapLE64(X) (X)
24 #define AI_SwapBE16(X) AI_Swap16(X)
25 #define AI_SwapBE32(X) AI_Swap32(X)
26 #define AI_SwapBE64(X) AI_Swap64(X)
28 #define AI_SwapLE16(X) AI_Swap16(X)
29 #define AI_SwapLE32(X) AI_Swap32(X)
30 #define AI_SwapLE64(X) AI_Swap64(X)
31 #define AI_SwapBE16(X) (X)
32 #define AI_SwapBE32(X) (X)
33 #define AI_SwapBE64(X) (X)
38 typedef uint8_t ProtocolId;
39 typedef std::deque<uint8_t> streamContainer;
41 const ProtocolId PROTO_PING = 0;
42 const ProtocolId PROTO_STATE = 1;
43 const ProtocolId PROTO_CHARACTER_STATIC = 2;
44 const ProtocolId PROTO_CHARACTER_DETAILS = 3;
45 const ProtocolId PROTO_SELECT = 4;
46 const ProtocolId PROTO_PAUSE = 5;
47 const ProtocolId PROTO_CHANGE = 6;
48 const ProtocolId PROTO_NAMES = 7;
49 const ProtocolId PROTO_RESET = 8;
50 const ProtocolId PROTO_STEP = 9;
51 const ProtocolId PROTO_UPDATENODE = 10;
52 const ProtocolId PROTO_DELETENODE = 11;
53 const ProtocolId PROTO_ADDNODE = 12;
62 #if defined(__GNUC__) && defined(__i386__)
63 static inline uint16_t AI_Swap16(uint16_t x) {
64 __asm__(
"xchgb %b0,%h0":
"=q"(x):
"0"(x));
67 #elif defined(__GNUC__) && defined(__x86_64__)
68 static inline uint16_t AI_Swap16(uint16_t x) {
69 __asm__(
"xchgb %b0,%h0":
"=Q"(x):
"0"(x));
73 static inline uint16_t AI_Swap16(uint16_t x) {
74 return static_cast<uint16_t
>((x << 8) | (x >> 8));
78 #if defined(__GNUC__) && defined(__i386__)
79 static inline uint32_t AI_Swap32(uint32_t x) {
80 __asm__(
"bswap %0":
"=r"(x):
"0"(x));
83 #elif defined(__GNUC__) && defined(__x86_64__)
84 static inline uint32_t AI_Swap32(uint32_t x) {
85 __asm__(
"bswapl %0":
"=r"(x):
"0"(x));
89 static inline uint32_t AI_Swap32(uint32_t x) {
90 return static_cast<uint32_t
>((x << 24) | ((x << 8) & 0x00FF0000) | ((x >> 8) & 0x0000FF00) | (x >> 24));
94 #if defined(__GNUC__) && defined(__i386__)
95 static inline uint64_t AI_Swap64(uint64_t x) {
103 __asm__(
"bswapl %0 ; bswapl %1 ; xchgl %0,%1":
"=r"(v.s.a),
"=r"(v.s.b):
"0"(v.s.a),
"1"(v.s. b));
106 #elif defined(__GNUC__) && defined(__x86_64__)
107 static inline uint64_t AI_Swap64(uint64_t x) {
108 __asm__(
"bswapq %0":
"=r"(x):
"0"(x));
112 static inline uint64_t AI_Swap64(uint64_t x) {
114 const uint32_t lo =
static_cast<uint32_t
>(x & 0xFFFFFFFF);
116 const uint32_t hi =
static_cast<uint32_t
>(x & 0xFFFFFFFF);
125 const ProtocolId _id;
128 static void addByte(streamContainer& out, uint8_t byte);
129 static void addBool(streamContainer& out,
bool value);
130 static void addShort(streamContainer& out, int16_t word);
131 static void addInt(streamContainer& out, int32_t dword);
132 static void addLong(streamContainer& out, int64_t dword);
133 static void addFloat(streamContainer& out,
float value);
134 static void addString(streamContainer& out,
const std::string&
string);
136 static bool readBool(streamContainer& in);
137 static uint8_t readByte(streamContainer& in);
138 static int16_t readShort(streamContainer& in);
139 static int32_t peekInt(
const streamContainer& in);
140 static int32_t readInt(streamContainer& in);
141 static int64_t readLong(streamContainer& in);
142 static float readFloat(streamContainer& in);
143 static std::string readString(streamContainer& in);
153 inline const ProtocolId& getId()
const {
157 virtual void serialize(streamContainer& out)
const {
162 inline void IProtocolMessage::addByte(streamContainer& out, uint8_t byte) {
166 inline void IProtocolMessage::addBool(streamContainer& out,
bool value) {
167 out.push_back(value);
170 inline bool IProtocolMessage::readBool(streamContainer& in) {
171 return readByte(in) == 1;
174 inline uint8_t IProtocolMessage::readByte(streamContainer& in) {
175 const uint8_t b = in.front();
180 inline void IProtocolMessage::addFloat(streamContainer& out,
float value) {
189 inline float IProtocolMessage::readFloat(streamContainer& in) {
198 inline std::string IProtocolMessage::readString(streamContainer& in) {
202 const char chr =
static_cast<char>(in.front());
211 inline void IProtocolMessage::addString(streamContainer& out,
const std::string&
string) {
212 const std::size_t length =
string.length();
213 for (std::size_t i = 0; i < length; ++i) {
214 out.push_back(uint8_t(
string[i]));
216 out.push_back(uint8_t(
'\0'));
219 inline void IProtocolMessage::addShort(streamContainer& out, int16_t word) {
220 const int16_t swappedWord = AI_SwapLE16(word);
221 out.push_back(uint8_t(swappedWord));
222 out.push_back(uint8_t(swappedWord >> CHAR_BIT));
225 inline void IProtocolMessage::addInt(streamContainer& out, int32_t dword) {
226 int32_t swappedDWord = AI_SwapLE32(dword);
227 out.push_back(uint8_t(swappedDWord));
228 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
229 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
230 out.push_back(uint8_t(swappedDWord >> CHAR_BIT));
233 inline void IProtocolMessage::addLong(streamContainer& out, int64_t dword) {
234 int64_t swappedDWord = AI_SwapLE64(dword);
235 out.push_back(uint8_t(swappedDWord));
236 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
237 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
238 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
239 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
240 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
241 out.push_back(uint8_t(swappedDWord >>= CHAR_BIT));
242 out.push_back(uint8_t(swappedDWord >> CHAR_BIT));
245 inline int16_t IProtocolMessage::readShort(streamContainer& in) {
247 const int l =
sizeof(buf);
248 for (
int i = 0; i < l; ++i) {
252 const int16_t *word = (
const int16_t*) (
void*) &buf;
253 const int16_t val = AI_SwapLE16(*word);
257 inline int32_t IProtocolMessage::readInt(streamContainer& in) {
259 const int l =
sizeof(buf);
260 for (
int i = 0; i < l; ++i) {
264 const int32_t *word = (
const int32_t*) (
void*) &buf;
265 const int32_t val = AI_SwapLE32(*word);
269 inline int32_t IProtocolMessage::peekInt(
const streamContainer& in) {
271 const int l =
sizeof(buf);
272 streamContainer::const_iterator it = in.begin();
273 for (
int i = 0; i < l; ++i) {
279 const int32_t *word = (
const int32_t*) (
void*) &buf;
280 const int32_t val = AI_SwapLE32(*word);
284 inline int64_t IProtocolMessage::readLong(streamContainer& in) {
286 const int l =
sizeof(buf);
287 for (
int i = 0; i < l; ++i) {
291 const int64_t *word = (
const int64_t*) (
void*) &buf;
292 const int64_t val = AI_SwapLE64(*word);
296 #define PROTO_MSG(name, id) class name : public IProtocolMessage { public: name() : IProtocolMessage(id) {} }
301 PROTO_MSG(AIResetMessage, PROTO_RESET);
305 PROTO_MSG(AIPingMessage, PROTO_PING);
A protocol message is used for the serialization of the ai states for remote debugging.
Definition: IProtocolMessage.h:60