Skip to content

Commit

Permalink
Enable packing for network structs
Browse files Browse the repository at this point in the history
Adds conversion functions to convert objects from/to packed form.
  • Loading branch information
arbruijn committed Jun 17, 2022
1 parent 4ed0b95 commit 7e6aca7
Show file tree
Hide file tree
Showing 9 changed files with 519 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ set(menus_sources menus/cockpitmenu.cpp menus/configmenu.cpp menus/detailsmenu.c
set(misc_sources misc/error.cpp misc/hash.cpp misc/strio.cpp misc/strutil.cpp)
set(model_sources models/aseread.cpp models/hiresmodels.cpp models/oofmath.cpp models/oofread.cpp models/pof.cpp models/polymodel.cpp)
if(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(network_sources network/linux/ipx_kali.cpp network/linux/ukali.cpp network/linux/ipx_mcast4.cpp network/linux/linuxnet.cpp network/linux/ipx_bsd.cpp network/autodl.cpp network/banlist.cpp network/multi.cpp network/multibot.cpp network/multimsg.cpp network/netmisc.cpp network/network.cpp network/network_handshake.cpp network/network_init.cpp network/network_join.cpp network/network_lib.cpp network/network_phandler.cpp network/network_process.cpp network/network_read.cpp network/network_send.cpp network/network_sync.cpp network/network_thread.cpp network/tracker.cpp network/udp_interface.cpp)
set(network_sources network/linux/ipx_kali.cpp network/linux/ukali.cpp network/linux/ipx_mcast4.cpp network/linux/linuxnet.cpp network/linux/ipx_bsd.cpp network/autodl.cpp network/banlist.cpp network/multi.cpp network/multibot.cpp network/multimsg.cpp network/netmisc.cpp network/network.cpp network/network_handshake.cpp network/network_init.cpp network/network_join.cpp network/network_lib.cpp network/network_phandler.cpp network/network_process.cpp network/network_read.cpp network/network_send.cpp network/network_sync.cpp network/network_thread.cpp network/tracker.cpp network/udp_interface.cpp network/objbufio.cpp)
else()
set(network_sources network/win32/ipx_mcast4.cpp network/win32/ipx_win.cpp network/win32/winnet.cpp network/autodl.cpp network/banlist.cpp network/multi.cpp network/multibot.cpp network/multimsg.cpp network/netmisc.cpp network/network.cpp network/network_handshake.cpp network/network_init.cpp network/network_join.cpp network/network_lib.cpp network/network_phandler.cpp network/network_process.cpp network/network_read.cpp network/network_send.cpp network/network_sync.cpp network/network_thread.cpp network/tracker.cpp network/udp_interface.cpp)
set(network_sources network/win32/ipx_mcast4.cpp network/win32/ipx_win.cpp network/win32/winnet.cpp network/autodl.cpp network/banlist.cpp network/multi.cpp network/multibot.cpp network/multimsg.cpp network/netmisc.cpp network/network.cpp network/network_handshake.cpp network/network_init.cpp network/network_join.cpp network/network_lib.cpp network/network_phandler.cpp network/network_process.cpp network/network_read.cpp network/network_send.cpp network/network_sync.cpp network/network_thread.cpp network/tracker.cpp network/udp_interface.cpp network/objbufio.cpp)
endif()
set(objects_sources objects/boss.cpp objects/createobject.cpp objects/criticalhit.cpp objects/effectobject.cpp objects/hostage.cpp objects/marker.cpp objects/object.cpp objects/objectio.cpp objects/object_physics.cpp objects/player.cpp objects/playerdeath.cpp objects/powerup.cpp objects/reactor.cpp objects/robot.cpp objects/updateobject.cpp)

Expand Down
4 changes: 4 additions & 0 deletions include/multi.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ enum compType {DOS,WIN_32,WIN_95,MAC} __pack__ ;
// the server and node members since I cannot change the order ot these
// members.

#pragma pack(push, 1)

typedef struct tNetPlayerInfo {
char callsign [CALLSIGN_LEN+1];
CNetworkInfo network;
Expand Down Expand Up @@ -472,6 +474,8 @@ typedef struct tNetGameInfo : tNetGameInfoLite {
} versionSpecific;
} __pack__ tNetGameInfo;

#pragma pack(pop)

class CNetGameInfo {
public:
tNetGameInfo m_info;
Expand Down
4 changes: 4 additions & 0 deletions include/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ extern int32_t nNetworkGameType;
extern int32_t nNetworkGameSubType;
#endif

#pragma pack(push, 1)

typedef struct tPlayerSyncData {
uint8_t nType;
int32_t nSecurity;
Expand Down Expand Up @@ -357,6 +359,8 @@ typedef struct tNetworkObjInfo {

extern tExtraGameInfo extraGameInfo [3];

#pragma pack(pop)

// -----------------------------------------------------------------------------

#if 1
Expand Down
12 changes: 12 additions & 0 deletions include/objbufio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef OBJBUFIO_H_
#define OBJBUFIO_H_
#include "object.h"

#define OBJECT_BUFFER_SIZE 0x108

// Only supports network sync'ed objects: robot, hostage, player, weapon, powerup, reactor, ghost, marker

void ObjectWriteToBuffer(CObject *pObj, void *pBuffer);
void ObjectReadFromBuffer(CObject *pObj, void *pBuffer);

#endif
4 changes: 4 additions & 0 deletions include/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ typedef struct tLongPos {
int16_t nSegment;
} __pack__ tLongPos;

#pragma pack(push, 1)

// A compressed form for sending crucial data about via slow devices,
// such as modems and buggies.
typedef struct tShortPos {
Expand All @@ -192,6 +194,8 @@ typedef struct tShortPos {
int16_t vel [3];
} __pack__ tShortPos;

#pragma pack(pop)

class CShortPos {
private:
tShortPos m_pos;
Expand Down
4 changes: 2 additions & 2 deletions network/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ libnetwork_a_SOURCES = autodl.cpp banlist.cpp network_handshake.cpp \
multibot.cpp multimsg.cpp netmisc.cpp network.cpp \
network_init.cpp network_join.cpp network_lib.cpp network_phandler.cpp \
network_process.cpp network_read.cpp network_send.cpp network_sync.cpp \
tracker.cpp udp_interface.cpp win32/winnet.cpp
tracker.cpp udp_interface.cpp win32/winnet.cpp objbufio.cpp
else
libnetwork_a_SOURCES = ${NETWORK_SRCS} ${IPX_SRCS} ${KALI_SRCS} \
autodl.cpp banlist.cpp multi.cpp multibot.cpp multimsg.cpp netmisc.cpp \
network.cpp network_handshake.cpp network_init.cpp network_join.cpp network_lib.cpp \
network_phandler.cpp network_process.cpp network_read.cpp network_send.cpp \
network_sync.cpp network_thread.cpp tracker.cpp udp_interface.cpp
network_sync.cpp network_thread.cpp tracker.cpp udp_interface.cpp objbufio.cpp
endif


13 changes: 7 additions & 6 deletions network/network_read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include "objeffects.h"
#include "physics.h"
#include "console.h"
#include "objbufio.h"

#ifdef WIN32
# define ZLIB_WINAPI
Expand Down Expand Up @@ -705,24 +706,24 @@ if (((m_nObjOwner == N_LOCALPLAYER) || (m_nObjOwner == -1)) && (m_nLocalObj != m

CObject* pObj = OBJECT (m_nLocalObj);

uLongf nUncompressedSize = (uLongf) sizeof (tBaseObject);
uint8_t uncompressedBuffer [sizeof (tBaseObject)];
uLongf nUncompressedSize = (uLongf) OBJECT_BUFFER_SIZE;
uint8_t uncompressedBuffer [OBJECT_BUFFER_SIZE];
uint8_t nCompressedSize;
int32_t i;

NW_GET_BYTE (m_data, m_bufI, nCompressedSize);

if (nCompressedSize == sizeof (tBaseObject)) {
NW_GET_BYTES (m_data, m_bufI, &pObj->info, sizeof (tBaseObject));
if (nCompressedSize == sizeof (uncompressedBuffer)) {
ObjectReadFromBuffer(pObj, m_data + m_bufI);
m_bufI += sizeof (uncompressedBuffer);
}
else {
if ((i = uncompress (uncompressedBuffer, &nUncompressedSize, (uint8_t*) m_data + m_bufI, (uLong) nCompressedSize)) != Z_OK) {
// PrintLog (0, "CObjectSynchronizer::Sync: decompression error #%d\n", i);
RequestResync ();
return -1;
}
i = 0;
NW_GET_BYTES (uncompressedBuffer, i, &pObj->info, sizeof (tBaseObject));
ObjectReadFromBuffer(pObj, uncompressedBuffer);
m_bufI += nCompressedSize;
}
if (pObj->info.nType != OBJ_NONE) {
Expand Down
8 changes: 6 additions & 2 deletions network/network_sync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include "monsterball.h"
#include "text.h"
#include "console.h"
#include "objbufio.h"

#ifdef WIN32
# define ZLIB_WINAPI
Expand Down Expand Up @@ -73,6 +74,7 @@ for (int16_t i = 0; i < networkData.nJoining; )
//------------------------------------------------------------------------------

static int32_t objFilter [] = {1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1};
// only robot, hostage, player, weapon, powerup, reactor, ghost, marker

static inline int32_t NetworkFilterObject (CObject *pObj)
{
Expand Down Expand Up @@ -153,9 +155,11 @@ for (nLocalObj = pSyncInfo->objs.nCurrent; nLocalObj <= gameData.objData.nLastOb
if (SendObject (pSyncInfo->objs.nMode, nLocalObj, nPlayer)) {
int8_t nObjOwner;
int16_t nRemoteObj = GetRemoteObjNum (int16_t (nLocalObj), nObjOwner);
uint8_t objBuf[OBJECT_BUFFER_SIZE];

uLongf nCompressedSize = sizeof (tBaseObject);
if ((compress (compressedBuffer, &nCompressedSize, (uint8_t*) &OBJECT (nLocalObj)->info, sizeof (tBaseObject)) == Z_OK) && (nCompressedSize < sizeof (tBaseObject))) {
uLongf nCompressedSize = sizeof(objBuf);
ObjectWriteToBuffer(OBJECT (nLocalObj), objBuf);
if ((compress (compressedBuffer, &nCompressedSize, objBuf, sizeof (objBuf)) == Z_OK) && (nCompressedSize < sizeof (tBaseObject))) {
if ((MAX_PAYLOAD_SIZE - bufI - 1) < int32_t (nCompressedSize) + 6)
break; // Not enough room for another object
NW_SET_SHORT (objBuf, bufI, nLocalObj);
Expand Down
Loading

0 comments on commit 7e6aca7

Please sign in to comment.