2007-07-28 06:41:51 -07:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
Warzone 2100 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.
|
|
|
|
|
|
|
|
Warzone 2100 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 Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2008-02-16 05:39:23 -08:00
|
|
|
/**
|
|
|
|
* @file nettypes.c
|
2007-08-08 11:50:28 -07:00
|
|
|
*
|
2007-08-17 07:02:17 -07:00
|
|
|
* Contains the 'new' Network API functions for sending and receiving both
|
2007-08-08 11:50:28 -07:00
|
|
|
* low-level primitives and higher-level types.
|
|
|
|
*/
|
|
|
|
|
2007-08-21 05:59:05 -07:00
|
|
|
#include "lib/framework/wzglobal.h"
|
2009-02-10 09:23:09 -08:00
|
|
|
#include "lib/framework/string_ext.h"
|
2007-08-08 11:50:28 -07:00
|
|
|
#include <string.h>
|
|
|
|
|
2007-12-27 11:29:53 -08:00
|
|
|
#include <SDL_endian.h>
|
2007-08-08 11:50:28 -07:00
|
|
|
|
|
|
|
#include "../framework/frame.h"
|
|
|
|
#include "netplay.h"
|
|
|
|
#include "nettypes.h"
|
|
|
|
|
2008-02-16 05:39:23 -08:00
|
|
|
static PACKETDIR NetDir;
|
|
|
|
|
|
|
|
static void NETsetPacketDir(PACKETDIR dir)
|
|
|
|
{
|
|
|
|
NetDir = dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
PACKETDIR NETgetPacketDir()
|
|
|
|
{
|
|
|
|
return NetDir;
|
|
|
|
}
|
|
|
|
|
2007-08-08 11:50:28 -07:00
|
|
|
/*
|
|
|
|
* Begin & End functions
|
|
|
|
*/
|
|
|
|
|
2007-11-25 04:12:03 -08:00
|
|
|
void NETbeginEncode(uint8_t type, uint8_t player)
|
2007-08-08 11:50:28 -07:00
|
|
|
{
|
2007-11-25 04:12:03 -08:00
|
|
|
NETsetPacketDir(PACKET_ENCODE);
|
|
|
|
NetMsg.type = type;
|
|
|
|
NetMsg.size = 0;
|
2008-03-24 09:51:17 -07:00
|
|
|
NetMsg.status = true;
|
2007-11-25 04:12:03 -08:00
|
|
|
NetMsg.destination = player;
|
|
|
|
memset(&NetMsg.body, '\0', MaxMsgSize);
|
|
|
|
}
|
2007-08-08 11:50:28 -07:00
|
|
|
|
2008-02-13 12:25:12 -08:00
|
|
|
void NETbeginDecode(uint8_t type)
|
2007-11-25 04:12:03 -08:00
|
|
|
{
|
|
|
|
NETsetPacketDir(PACKET_DECODE);
|
2008-02-13 12:25:12 -08:00
|
|
|
assert(type == NetMsg.type);
|
2007-11-25 04:12:03 -08:00
|
|
|
NetMsg.size = 0;
|
2008-03-24 09:51:17 -07:00
|
|
|
NetMsg.status = true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
2007-11-25 04:12:03 -08:00
|
|
|
BOOL NETend(void)
|
2007-08-08 11:50:28 -07:00
|
|
|
{
|
|
|
|
assert(NETgetPacketDir() != PACKET_INVALID);
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
// If we are decoding just return true
|
2008-01-04 02:32:59 -08:00
|
|
|
if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2008-01-04 02:32:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the packet is invalid or failed to compile
|
2007-08-08 11:50:28 -07:00
|
|
|
if (NETgetPacketDir() != PACKET_ENCODE || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have sent the packet, so make it invalid (to prevent re-sending)
|
|
|
|
NETsetPacketDir(PACKET_INVALID);
|
|
|
|
|
|
|
|
// Send the packet, updating the send functions is on my todo list!
|
2007-11-25 04:12:03 -08:00
|
|
|
if (NetMsg.destination == NET_ALL_PLAYERS)
|
2007-08-08 11:50:28 -07:00
|
|
|
{
|
2008-03-24 09:02:11 -07:00
|
|
|
return NETbcast(&NetMsg);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-03-24 09:02:11 -07:00
|
|
|
return NETsend(&NetMsg, NetMsg.destination);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Primitive functions (ints and strings). Due to the lack of C++ and the fact
|
|
|
|
* that I hate macros this is a lot longer than it should be.
|
|
|
|
*/
|
|
|
|
|
|
|
|
BOOL NETint8_t(int8_t *ip)
|
|
|
|
{
|
|
|
|
int8_t *store = (int8_t *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (sizeof(int8_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-bit (1 byte) integers need no endian-swapping!
|
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
|
|
|
*store = *ip;
|
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
|
|
|
*ip = *store;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(int8_t);
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NETuint8_t(uint8_t *ip)
|
|
|
|
{
|
|
|
|
uint8_t *store = (uint8_t *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (sizeof(uint8_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 8-bit (1 byte) integers need no endian-swapping!
|
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
|
|
|
*store = *ip;
|
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
|
|
|
*ip = *store;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(uint8_t);
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NETint16_t(int16_t *ip)
|
|
|
|
{
|
|
|
|
int16_t *store = (int16_t *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (sizeof(int16_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the integer into the network byte order (big endian)
|
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*store = SDL_SwapBE16(*ip);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*ip = SDL_SwapBE16(*store);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(int16_t);
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NETuint16_t(uint16_t *ip)
|
|
|
|
{
|
|
|
|
uint16_t *store = (uint16_t *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (sizeof(uint16_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the integer into the network byte order (big endian)
|
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*store = SDL_SwapBE16(*ip);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*ip = SDL_SwapBE16(*store);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(uint16_t);
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NETint32_t(int32_t *ip)
|
|
|
|
{
|
|
|
|
int32_t *store = (int32_t *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (sizeof(int32_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the integer into the network byte order (big endian)
|
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*store = SDL_SwapBE32(*ip);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*ip = SDL_SwapBE32(*store);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(int32_t);
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NETuint32_t(uint32_t *ip)
|
|
|
|
{
|
|
|
|
uint32_t *store = (uint32_t *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (sizeof(uint32_t) + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the integer into the network byte order (big endian)
|
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*store = SDL_SwapBE32(*ip);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
2007-12-27 11:29:53 -08:00
|
|
|
*ip = SDL_SwapBE32(*store);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(uint32_t);
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
2007-09-21 13:43:15 -07:00
|
|
|
BOOL NETfloat(float *fp)
|
2007-08-08 11:50:28 -07:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* NB: Not portable.
|
|
|
|
* This routine only works on machines with IEEE754 floating point numbers.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(__STDC_IEC_559__) \
|
|
|
|
&& !defined(__m68k__) && !defined(__sparc__) && !defined(__i386__) \
|
|
|
|
&& !defined(__mips__) && !defined(__ns32k__) && !defined(__alpha__) \
|
|
|
|
&& !defined(__arm__) && !defined(__ppc__) && !defined(__ia64__) \
|
2007-08-17 04:37:39 -07:00
|
|
|
&& !defined(__arm26__) && !defined(__sparc64__) && !defined(__amd64__) \
|
2007-08-17 10:54:26 -07:00
|
|
|
&& !defined(WZ_CC_MSVC) // Assume that all platforms supported by
|
|
|
|
// MSVC provide IEEE754 floating point numbers
|
2007-08-08 11:50:28 -07:00
|
|
|
# error "this platform hasn't been confirmed to support IEEE754 floating point numbers"
|
|
|
|
#endif
|
|
|
|
|
2007-09-21 13:43:15 -07:00
|
|
|
// IEEE754 floating point numbers can be treated the same as 32-bit integers
|
2007-08-08 11:50:28 -07:00
|
|
|
// with regards to endian conversion
|
|
|
|
STATIC_ASSERT(sizeof(float) == sizeof(int32_t));
|
|
|
|
|
2007-09-21 13:43:15 -07:00
|
|
|
return NETint32_t((int32_t *) fp);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOL NETbool(BOOL *bp)
|
|
|
|
{
|
|
|
|
// Bools are converted to uint8_ts
|
|
|
|
uint8_t tmp = (uint8_t) *bp;
|
|
|
|
NETuint8_t(&tmp);
|
|
|
|
|
|
|
|
// If we are decoding and managed to extract the value set it
|
|
|
|
if (NETgetPacketDir() == PACKET_DECODE && NetMsg.status)
|
|
|
|
{
|
|
|
|
*bp = (BOOL) tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NetMsg.status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* NETnull should be used to either add 4 bytes of padding to a message, or to
|
|
|
|
* discard 4 bytes of data from a message.
|
|
|
|
*/
|
2007-11-25 04:12:03 -08:00
|
|
|
BOOL NETnull()
|
2007-08-08 11:50:28 -07:00
|
|
|
{
|
|
|
|
uint32_t zero = 0;
|
|
|
|
return NETuint32_t(&zero);
|
|
|
|
}
|
|
|
|
|
2008-03-23 06:48:25 -07:00
|
|
|
/** Sends or receives a string to or from the current network package.
|
|
|
|
* \param str When encoding a packet this is the (NUL-terminated string to
|
|
|
|
* be sent in the current network package. When decoding this
|
|
|
|
* is the buffer to decode the string from the network package
|
|
|
|
* into. When decoding this string is guaranteed to be
|
|
|
|
* NUL-terminated provided that this buffer is at least 1 byte
|
|
|
|
* large.
|
|
|
|
* \param maxlen The buffer size of \c str. For static buffers this means
|
|
|
|
* sizeof(\c str), for dynamically allocated buffers this is
|
|
|
|
* whatever number you passed to malloc().
|
|
|
|
* \note If while decoding \c maxlen is smaller than the actual length of the
|
|
|
|
* string being decoded, the resulting string (in \c str) will be
|
|
|
|
* truncated.
|
|
|
|
*/
|
2007-08-08 11:50:28 -07:00
|
|
|
BOOL NETstring(char *str, uint16_t maxlen)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Strings sent over the network are prefixed with their length, sent as an
|
|
|
|
* unsigned 16-bit integer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Work out the length of the string if we are encoding
|
|
|
|
uint16_t len = (NETgetPacketDir() == PACKET_ENCODE) ? strnlen1(str, maxlen) : 0;
|
|
|
|
char *store;
|
|
|
|
|
|
|
|
// Add/fetch the length from the packet
|
|
|
|
NETuint16_t(&len);
|
|
|
|
|
|
|
|
// Map store to the message buffer
|
|
|
|
store = (char *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (len + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
|
|
|
memcpy(store, str, len);
|
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
2008-02-10 03:43:08 -08:00
|
|
|
// Truncate length if necessary
|
|
|
|
if (len > maxlen)
|
|
|
|
{
|
2008-02-11 11:26:23 -08:00
|
|
|
debug(LOG_ERROR, "NETstring: Decoding packet type %d from %d, buffer size %u truncated at %u",
|
|
|
|
NetMsg.type, NetMsg.source, len, maxlen);
|
2008-02-10 03:43:08 -08:00
|
|
|
len = maxlen;
|
|
|
|
}
|
2008-03-23 06:50:54 -07:00
|
|
|
memcpy(str, store, len);
|
|
|
|
// Guarantee NUL-termination
|
|
|
|
str[len - 1] = '\0';
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(len) + len;
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
2008-02-10 03:43:08 -08:00
|
|
|
|
2008-01-11 13:39:41 -08:00
|
|
|
BOOL NETbin(char *str, uint16_t maxlen)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Strings sent over the network are prefixed with their length, sent as an
|
|
|
|
* unsigned 16-bit integer.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Work out the length of the string if we are encoding
|
|
|
|
uint16_t len = (NETgetPacketDir() == PACKET_ENCODE) ? maxlen : 0;
|
|
|
|
char *store;
|
|
|
|
|
|
|
|
// Add/fetch the length from the packet
|
|
|
|
NETuint16_t(&len);
|
|
|
|
|
|
|
|
// Map store to the message buffer
|
|
|
|
store = (char *) &NetMsg.body[NetMsg.size];
|
|
|
|
|
|
|
|
// Make sure there is enough data/space left in the packet
|
|
|
|
if (len + NetMsg.size > MaxMsgSize || !NetMsg.status)
|
|
|
|
{
|
2008-03-24 09:51:17 -07:00
|
|
|
return NetMsg.status = false;
|
2008-01-11 13:39:41 -08:00
|
|
|
}
|
2007-08-08 11:50:28 -07:00
|
|
|
|
2008-01-11 13:39:41 -08:00
|
|
|
if (NETgetPacketDir() == PACKET_ENCODE)
|
|
|
|
{
|
|
|
|
memcpy(store, str, len);
|
|
|
|
}
|
|
|
|
else if (NETgetPacketDir() == PACKET_DECODE)
|
|
|
|
{
|
2008-02-10 03:43:08 -08:00
|
|
|
// Truncate length if necessary
|
|
|
|
if (len > maxlen)
|
|
|
|
{
|
2008-02-11 11:26:23 -08:00
|
|
|
debug(LOG_ERROR, "NETbin: Decoding packet type %d from %d, buffer size %u truncated at %u",
|
|
|
|
NetMsg.type, NetMsg.source, len, maxlen);
|
2008-02-10 03:43:08 -08:00
|
|
|
len = maxlen;
|
|
|
|
}
|
2008-01-11 13:39:41 -08:00
|
|
|
memcpy(str, store, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Increment the size of the message
|
|
|
|
NetMsg.size += sizeof(len) + len;
|
|
|
|
|
2008-03-24 09:51:17 -07:00
|
|
|
return true;
|
2008-01-11 13:39:41 -08:00
|
|
|
}
|
2008-02-10 03:43:08 -08:00
|
|
|
|
2007-12-24 15:36:02 -08:00
|
|
|
BOOL NETVector3uw(Vector3uw* vp)
|
|
|
|
{
|
|
|
|
return (NETuint16_t(&vp->x)
|
|
|
|
&& NETuint16_t(&vp->y)
|
|
|
|
&& NETuint16_t(&vp->z));
|
|
|
|
}
|
|
|
|
|
2008-03-23 06:35:01 -07:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
test_a,
|
|
|
|
test_b,
|
|
|
|
} test_enum;
|
|
|
|
|
2007-08-08 11:50:28 -07:00
|
|
|
static void NETcoder(PACKETDIR dir)
|
|
|
|
{
|
2008-03-23 06:12:58 -07:00
|
|
|
static const char original[] = "THIS IS A TEST STRING";
|
|
|
|
char str[sizeof(original)];
|
2008-03-24 09:51:17 -07:00
|
|
|
BOOL b = true;
|
2007-08-08 11:50:28 -07:00
|
|
|
uint32_t u32 = 32;
|
|
|
|
uint16_t u16 = 16;
|
|
|
|
uint8_t u8 = 8;
|
|
|
|
int32_t i32 = -32;
|
|
|
|
int16_t i16 = -16;
|
|
|
|
int8_t i8 = -8;
|
2008-03-23 06:35:01 -07:00
|
|
|
test_enum te = test_b;
|
2007-08-08 11:50:28 -07:00
|
|
|
|
2008-05-25 06:46:49 -07:00
|
|
|
sstrcpy(str, original);
|
2007-10-27 07:35:35 -07:00
|
|
|
|
2007-11-25 04:12:03 -08:00
|
|
|
if (dir == PACKET_ENCODE)
|
|
|
|
NETbeginEncode(0, 0);
|
|
|
|
else
|
2008-02-13 12:25:12 -08:00
|
|
|
NETbeginDecode(0);
|
2008-03-24 09:51:17 -07:00
|
|
|
NETbool(&b); assert(b == true);
|
2007-08-08 11:50:28 -07:00
|
|
|
NETuint32_t(&u32); assert(u32 == 32);
|
|
|
|
NETuint16_t(&u16); assert(u16 == 16);
|
|
|
|
NETuint8_t(&u8); assert(u8 == 8);
|
|
|
|
NETint32_t(&i32); assert(i32 == -32);
|
|
|
|
NETint16_t(&i16); assert(i16 == -16);
|
|
|
|
NETint8_t(&i8); assert(i8 == -8);
|
2008-03-23 06:12:58 -07:00
|
|
|
NETstring(str, sizeof(str)); assert(strncmp(str, original, sizeof(str) - 1) == 0);
|
2008-03-23 06:35:01 -07:00
|
|
|
NETenum(&te); assert(te == test_b);
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void NETtest()
|
|
|
|
{
|
|
|
|
NETMSG cmp;
|
|
|
|
|
|
|
|
memset(&cmp, 0, sizeof(cmp));
|
|
|
|
memset(&NetMsg, 0, sizeof(NetMsg));
|
|
|
|
NETcoder(PACKET_ENCODE);
|
|
|
|
memcpy(&cmp, &NetMsg, sizeof(cmp));
|
|
|
|
NETcoder(PACKET_DECODE);
|
|
|
|
ASSERT(memcmp(&cmp, &NetMsg, sizeof(cmp)) == 0, "nettypes unit test failed");
|
2008-03-30 07:36:11 -07:00
|
|
|
fprintf(stdout, "\tNETtypes self-test: PASSED\n");
|
2007-08-08 11:50:28 -07:00
|
|
|
}
|
2008-01-02 09:08:29 -08:00
|
|
|
|
|
|
|
int NETgetSource()
|
|
|
|
{
|
|
|
|
return NetMsg.source;
|
|
|
|
}
|