From c813023a9a19f42efd589e84eb7d5bcfc5e59b7f Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 30 Mar 2022 22:58:06 +0200 Subject: Brought NetGram closer to standard --- NetEx/NetGram.cpp | 78 ++++++++++++++++++++++++++++++------------------------- NetEx/NetGram.h | 41 ++++++++++++++--------------- 2 files changed, 61 insertions(+), 58 deletions(-) (limited to 'NetEx') diff --git a/NetEx/NetGram.cpp b/NetEx/NetGram.cpp index 701d562..5394df2 100644 --- a/NetEx/NetGram.cpp +++ b/NetEx/NetGram.cpp @@ -3,53 +3,64 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Generic Network Packet (Datagram) Implementation + OVERVIEW + ======== + Generic Network Packet (Datagram) Implementation */ #include "NetGram.h" + +#include +#include +#include + +#include "NetAddr.h" #include "NetLayer.h" -// +-------------------------------------------------------------------+ -static DWORD net_gram_sequence = 1; +static std::atomic net_gram_sequence {1}; -// +-------------------------------------------------------------------+ /** * NetGram constructor for ACK packets */ -NetGram::NetGram() - : retries(0), packet_id(0), send_time(0) -{ } +NetGram::NetGram() : + retries(0), + packet_id(0), + send_time(0) +{ +} + /** * NetGram constructor for receiving packets from remote hosts */ -NetGram::NetGram(const NetAddr& src, Text msg) - : addr(src), retries(0), send_time(0) +NetGram::NetGram(const NetAddr& src, Text msg) : + addr(src), + retries(0), + send_time(0) { body = msg; if (body.length() >= NET_GRAM_HEADER_SIZE) { - BYTE* data = (BYTE*) body.data(); - - packet_id = (((DWORD) data[0]) << 24) + - (((DWORD) data[1]) << 16) + - (((DWORD) data[2]) << 8) + - ((DWORD) data[3]); + std::uint8_t* data = (std::uint8_t*) body.data(); + packet_id = + (static_cast(data[0]) << 24) + + (static_cast(data[1]) << 16) + + (static_cast(data[2]) << 8) + + (static_cast(data[3])); } } /** * NetGram constructor for composing packets to send to remote hosts */ -NetGram::NetGram(const NetAddr& dst, Text user_data, int r) - : addr(dst), retries(r) +NetGram::NetGram(const NetAddr& dst, Text user_data, int r) : + addr(dst), + retries(r) { send_time = NetLayer::GetTime(); packet_id = net_gram_sequence++; @@ -57,22 +68,21 @@ NetGram::NetGram(const NetAddr& dst, Text user_data, int r) if (retries) packet_id |= NET_GRAM_RELIABLE; - static BYTE buf[NET_GRAM_MAX_SIZE]; - buf[0] = (BYTE) (packet_id >> 24) & 0xff; - buf[1] = (BYTE) (packet_id >> 16) & 0xff; - buf[2] = (BYTE) (packet_id >> 8) & 0xff; - buf[3] = (BYTE) (packet_id) & 0xff; + static std::uint8_t buf[NET_GRAM_MAX_SIZE]; + buf[0] = static_cast((packet_id >> 24) & 0xff); + buf[1] = static_cast((packet_id >> 16) & 0xff); + buf[2] = static_cast((packet_id >> 8) & 0xff); + buf[3] = static_cast((packet_id) & 0xff); int len = user_data.length(); if (len >= NET_GRAM_MAX_SIZE - NET_GRAM_HEADER_SIZE) len = NET_GRAM_MAX_SIZE - NET_GRAM_HEADER_SIZE - 1; - CopyMemory(buf+NET_GRAM_HEADER_SIZE, user_data.data(), len); + std::memcpy(buf + NET_GRAM_HEADER_SIZE, user_data.data(), len); body = Text((char*) buf, len+NET_GRAM_HEADER_SIZE); } -// +--------------------------------------------------------------------+ void NetGram::Retry() @@ -83,7 +93,6 @@ NetGram::Retry() } } -// +--------------------------------------------------------------------+ NetGram NetGram::Ack() @@ -93,16 +102,13 @@ NetGram::Ack() ack.packet_id = packet_id | NET_GRAM_ACK; ack.send_time = NetLayer::GetTime(); - static BYTE buf[NET_GRAM_HEADER_SIZE]; - buf[0] = (BYTE) (ack.packet_id >> 24) & 0xff; - buf[1] = (BYTE) (ack.packet_id >> 16) & 0xff; - buf[2] = (BYTE) (ack.packet_id >> 8) & 0xff; - buf[3] = (BYTE) (ack.packet_id) & 0xff; + static std::uint8_t buf[NET_GRAM_HEADER_SIZE]; + buf[0] = static_cast((ack.packet_id >> 24) & 0xff); + buf[1] = static_cast((ack.packet_id >> 16) & 0xff); + buf[2] = static_cast((ack.packet_id >> 8) & 0xff); + buf[3] = static_cast((ack.packet_id) & 0xff); ack.body = Text((char*) buf, NET_GRAM_HEADER_SIZE); return ack; } - - - diff --git a/NetEx/NetGram.h b/NetEx/NetGram.h index db2770f..d7a58c9 100644 --- a/NetEx/NetGram.h +++ b/NetEx/NetGram.h @@ -3,33 +3,31 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Datagram (UDP) packet that implements the basic - packet-oriented network protocol. + OVERVIEW + ======== + Datagram (UDP) packet that implements the basic + packet-oriented network protocol. */ - #ifndef NetGram_h #define NetGram_h -#include +#include + #include "NetAddr.h" #include "Text.h" -// +-------------------------------------------------------------------+ -const int NET_GRAM_ACK = 0x80000000; -const int NET_GRAM_RELIABLE = 0x40000000; -const int NET_GRAM_SEQ_MASK = 0x3fffffff; +const std::uint32_t NET_GRAM_ACK = 0x80000000; +const std::uint32_t NET_GRAM_RELIABLE = 0x40000000; +const std::uint32_t NET_GRAM_SEQ_MASK = 0x3fffffff; -const int NET_GRAM_HEADER_SIZE = 4; -const int NET_GRAM_MAX_SIZE = 1024; +const std::uint32_t NET_GRAM_HEADER_SIZE = 4; +const std::uint32_t NET_GRAM_MAX_SIZE = 1024; -// +-------------------------------------------------------------------+ class NetGram { @@ -46,11 +44,11 @@ public: addr == g.addr; } int operator < (const NetGram& g) const { return Sequence() < g.Sequence(); } - DWORD PacketID() const { return packet_id; } - DWORD Sequence() const { return packet_id & NET_GRAM_SEQ_MASK; } - DWORD SendTime() const { return send_time; } - BYTE* Data() const { return (BYTE*) body.data(); } - BYTE* UserData() const { return (BYTE*) body.data() + NET_GRAM_HEADER_SIZE; } + std::uint32_t PacketID() const { return packet_id; } + std::uint32_t Sequence() const { return packet_id & NET_GRAM_SEQ_MASK; } + std::uint32_t SendTime() const { return send_time; } + std::uint8_t* Data() const { return (std::uint8_t*) body.data(); } + std::uint8_t* UserData() const { return (std::uint8_t*) body.data() + NET_GRAM_HEADER_SIZE; } int Size() const { return body.length(); } const Text& Body() const { return body; } const NetAddr& Address() const { return addr; } @@ -68,9 +66,8 @@ protected: NetAddr addr; // network address of remote host int retries; // number of retries remaining (reliable packets only) - DWORD send_time; // time in msec of most recent send attempt - - DWORD packet_id; // copy of packet id from header in body + std::uint32_t send_time; // time in msec of most recent send attempt + std::uint32_t packet_id; // copy of packet id from header in body Text body; // header plus user data }; -- cgit v1.1