Skip to content

Commit

Permalink
sync SNetReceiveMessage with SNetReceiveTurn
Browse files Browse the repository at this point in the history
  • Loading branch information
pionere committed Nov 19, 2023
1 parent 8b383ab commit f1dfa3d
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Source/dvlnet/abstract_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static constexpr uint8_t PLR_BROADCAST = SNPLAYER_ALL;
class abstract_net {
public:
virtual bool setup_game(_uigamedata* gameData, const char* addrstr, unsigned port, const char* passwd, char (&errorText)[256]) = 0;
virtual bool SNetReceiveMessage(int* sender, BYTE** data, unsigned* size) = 0;
virtual SNetMsgPkt* SNetReceiveMessage() = 0;
virtual void SNetSendMessage(int receiver, const BYTE* data, unsigned size) = 0;
virtual SNetTurnPkt* SNetReceiveTurn(unsigned (&status)[MAX_PLRS]) = 0;
virtual void SNetSendTurn(turn_t turn, const BYTE* data, unsigned size) = 0;
Expand Down
23 changes: 15 additions & 8 deletions Source/dvlnet/base_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,17 +153,25 @@ void base_client::recv_local(packet& pkt)
}
}

bool base_client::SNetReceiveMessage(int* sender, BYTE** data, unsigned* size)
SNetMsgPkt* base_client::SNetReceiveMessage()
{
poll();

SNetMsgPkt* pkt = NULL;
SNetMessage* pm;
unsigned dwLen;

if (message_queue.empty())
return false;
message_last = message_queue.front();
return pkt;
pm = &message_queue.front();

dwLen = pm->payload.size();
pkt = (SNetMsgPkt*)DiabloAllocPtr(dwLen + sizeof(SNetMsgPkt) - sizeof(pkt->data));
pkt->nmpPlr = pm->sender;
pkt->nmpLen = dwLen;
memcpy(pkt->data, pm->payload.data(), dwLen);
message_queue.pop_front();
*sender = message_last.sender;
*size = message_last.payload.size();
*data = message_last.payload.data();
return true;
return pkt;
}

void base_client::SNetSendMessage(int receiver, const BYTE* data, unsigned size)
Expand Down Expand Up @@ -390,7 +398,6 @@ void base_client::close()
{
int i;

message_last.payload.clear();
message_queue.clear();
for (i = 0; i < MAX_PLRS; i++)
turn_queue[i].clear();
Expand Down
3 changes: 1 addition & 2 deletions Source/dvlnet/base_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class base_client : public abstract_net {
public:
// bool setup_game(_uigamedata* gameData, const char* addrstr, unsigned port, const char* passwd, char (&errorText)[256]) override;

bool SNetReceiveMessage(int* sender, BYTE** data, unsigned* size) override;
SNetMsgPkt* SNetReceiveMessage() override;
void SNetSendMessage(int receiver, const BYTE* data, unsigned size) override;
SNetTurnPkt* SNetReceiveTurn(unsigned (&status)[MAX_PLRS]) override;
void SNetSendTurn(turn_t turn, const BYTE* data, unsigned size) override;
Expand All @@ -43,7 +43,6 @@ class base_client : public abstract_net {
SEVTHANDLER registered_handlers[NUM_EVT_TYPES] = { };
buffer_t game_init_info;

SNetMessage message_last;
std::deque<SNetMessage> message_queue;
std::deque<SNetTurn> turn_queue[MAX_PLRS] = { };
int connected_table[MAX_PLRS] = { }; // connection_status
Expand Down
22 changes: 14 additions & 8 deletions Source/dvlnet/loopback.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ bool loopback::setup_game(_uigamedata* gameData, const char* addrstr, unsigned p
return true;
}

bool loopback::SNetReceiveMessage(int* sender, BYTE** data, unsigned* size)
SNetMsgPkt* loopback::SNetReceiveMessage()
{
SNetMsgPkt* pkt = NULL;
buffer_t* pm;
unsigned dwLen;

if (message_queue.empty())
return false;
message_last = message_queue.front();
return pkt;
pm = &message_queue.front();

dwLen = pm->size();
pkt = (SNetMsgPkt*)DiabloAllocPtr(dwLen + sizeof(SNetMsgPkt) - sizeof(pkt->data));
pkt->nmpPlr = PLR_SINGLE;
pkt->nmpLen = dwLen;
memcpy(pkt->data, pm->data(), dwLen);
message_queue.pop_front();
*sender = PLR_SINGLE;
*size = message_last.size();
*data = message_last.data();
return true;
return pkt;
}

void loopback::SNetSendMessage(int receiver, const BYTE* data, unsigned size)
Expand Down Expand Up @@ -92,7 +99,6 @@ void loopback::SNetSendTurn(turn_t turn, const BYTE* data, unsigned size)

void loopback::SNetLeaveGame()
{
// message_last.clear(); -- not necessary at the moment
message_queue.clear();
turn_queue.clear();
}
Expand Down
3 changes: 1 addition & 2 deletions Source/dvlnet/loopback.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace net {
class loopback : public abstract_net {
public:
bool setup_game(_uigamedata* gameData, const char* addrstr, unsigned port, const char* passwd, char (&errorText)[256]) override;
bool SNetReceiveMessage(int* sender, BYTE** data, unsigned* size) override;
SNetMsgPkt* SNetReceiveMessage() override;
void SNetSendMessage(int receiver, const BYTE* data, unsigned size) override;
SNetTurnPkt* SNetReceiveTurn(unsigned (&status)[MAX_PLRS]) override;
void SNetSendTurn(turn_t turn, const BYTE* data, unsigned size) override;
Expand All @@ -29,7 +29,6 @@ class loopback : public abstract_net {
void make_default_gamename(char (&gamename)[NET_MAX_GAMENAME_LEN + 1]) override;

private:
buffer_t message_last;
std::deque<buffer_t> message_queue;
std::deque<SNetTurn> turn_queue;
};
Expand Down
37 changes: 24 additions & 13 deletions Source/multi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ static void multi_parse_turns()
// TODO: use pre-allocated space?
SNetTurnPkt* turn = SNetReceiveTurn(player_state);
multi_process_turn(turn);
MemFreeDbg(turn);
mem_free_dbg(turn);
#ifndef NONET
if (guSendGameDelta != 0) {
if (!gbJoinGame) {
Expand Down Expand Up @@ -456,24 +456,35 @@ void multi_pre_process_turn(SNetTurnPkt* turn)
gdwGameLogicTurn = turn->ntpTurn * gbNetUpdateRate;
}

void multi_process_msgs()
static void multi_process_msg(SNetMsgPkt* msg)
{
MsgPktHdr* pkt;
unsigned dwMsgSize, dwReadSize;
int pnum;

dwMsgSize = msg->nmpLen;
if (dwMsgSize < sizeof(MsgPktHdr))
return;
//if (pkt->wCheck != PKT_HDR_CHECK)
// continue;
pkt = (MsgPktHdr*)msg->data;
if (pkt->wLen != dwMsgSize)
return;
pnum = msg->nmpPlr;
// assert((unsigned)pnum < MAX_PLRS || pnum == SNPLAYER_MASTER);
dwMsgSize -= sizeof(MsgPktHdr);
dwReadSize = ParseMsg(pnum, (TCmd*)&pkt[1]);
assert(dwReadSize == dwMsgSize);
}

void multi_process_msgs()
{
SNetMsgPkt* msg;

//multi_process_tmsgs();
while (SNetReceiveMessage(&pnum, (BYTE**)&pkt, &dwMsgSize)) {
if (dwMsgSize < sizeof(MsgPktHdr))
continue;
// assert((unsigned)pnum < MAX_PLRS || pnum == SNPLAYER_MASTER);
//if (pkt->wCheck != PKT_HDR_CHECK)
// continue;
if (pkt->wLen != dwMsgSize)
continue;
dwMsgSize -= sizeof(MsgPktHdr);
dwReadSize = ParseMsg(pnum, (TCmd*)&pkt[1]);
assert(dwReadSize == dwMsgSize);
while ((msg = SNetReceiveMessage()) != NULL) {
multi_process_msg(msg);
mem_free_dbg(msg);
}
}

Expand Down
4 changes: 2 additions & 2 deletions Source/storm/storm_net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ void SNetSendMessage(int receiver, const BYTE* data, unsigned databytes)
dvlnet_inst->SNetSendMessage(receiver, data, databytes);
}

bool SNetReceiveMessage(int* sender, BYTE** data, unsigned* databytes)
SNetMsgPkt* SNetReceiveMessage()
{
#ifdef ZEROTIER
std::lock_guard<std::mutex> lg(storm_net_mutex);
#endif
return dvlnet_inst->SNetReceiveMessage(sender, data, databytes);
return dvlnet_inst->SNetReceiveMessage();
}

void SNetSendTurn(turn_t turn, const BYTE* data, unsigned databytes)
Expand Down
2 changes: 1 addition & 1 deletion Source/storm/storm_net.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void SNetGetGameInfo(const char** name, const char** password);
*
*/
void SNetSendMessage(int receiver, const BYTE* data, unsigned databytes);
bool SNetReceiveMessage(int* sender, BYTE** data, unsigned* databytes);
SNetMsgPkt* SNetReceiveMessage();

/* SNetSendTurn @ 122
*
Expand Down
6 changes: 6 additions & 0 deletions structs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2438,6 +2438,12 @@ typedef struct SNetTurnPkt {
BYTE data[32000]; // size does not matter, the struct is allocated dynamically
} SNetTurnPkt;

typedef struct SNetMsgPkt {
int nmpPlr;
unsigned nmpLen;
BYTE data[32000]; // size does not matter, the struct is allocated dynamically
} SNetMsgPkt;

//////////////////////////////////////////////////
// path
//////////////////////////////////////////////////
Expand Down

0 comments on commit f1dfa3d

Please sign in to comment.