diff options
author | Aki <please@ignore.pl> | 2022-03-31 19:03:27 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-03-31 19:03:27 +0200 |
commit | 8951abb7e436c6aabd2832ce4abea95ecae8192e (patch) | |
tree | 99df63a046ad57ab4482cea6c4d85578ded1cea1 /NetEx/NetPeer.cpp | |
parent | a7bba97ddbb49f626951c4cb7fb72a9ba879f64c (diff) | |
download | starshatter-8951abb7e436c6aabd2832ce4abea95ecae8192e.zip starshatter-8951abb7e436c6aabd2832ce4abea95ecae8192e.tar.gz starshatter-8951abb7e436c6aabd2832ce4abea95ecae8192e.tar.bz2 |
Brought NetPeer closer to standard
Diffstat (limited to 'NetEx/NetPeer.cpp')
-rw-r--r-- | NetEx/NetPeer.cpp | 127 |
1 files changed, 68 insertions, 59 deletions
diff --git a/NetEx/NetPeer.cpp b/NetEx/NetPeer.cpp index 6adbe71..34adbbc 100644 --- a/NetEx/NetPeer.cpp +++ b/NetEx/NetPeer.cpp @@ -3,44 +3,56 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - One side of a UDP net link connection + OVERVIEW + ======== + One side of a UDP net link connection */ -#include <windows.h> #include "NetPeer.h" + +#include <atomic> +#include <cstdint> +#include <cstring> +#include <mutex> + +#include "List.h" +#include "NetAddr.h" #include "NetGram.h" -#include "NetMsg.h" #include "NetLayer.h" +#include "NetMsg.h" -#include <stdio.h> - -// +-------------------------------------------------------------------+ const int MULTIPART_CHUNKSIZE = 232; const int MULTIPART_HEADER = 16; const int UDP_HEADER_SIZE = 34; -static NetMsgMultipart multi_part_buffer; -static DWORD multi_msg_sequence = 1; - -// +-------------------------------------------------------------------+ - -NetPeer::NetPeer(const NetAddr& a, DWORD id) - : addr(a), netid(id), sequence(0), pps(0), bps(0), max_qsize(0), - status(OK), hist_indx(0), send_size(0), recv_size(0), - chunk_size(MULTIPART_CHUNKSIZE) +thread_local NetMsgMultipart multi_part_buffer; +static std::atomic<std::uint32_t> multi_msg_sequence {1}; + + +NetPeer::NetPeer(const NetAddr& a, std::uint32_t id) : + addr(a), + netid(id), + sequence(0), + pps(0), + bps(0), + max_qsize(0), + status(OK), + hist_indx(0), + send_size(0), + recv_size(0), + chunk_size(MULTIPART_CHUNKSIZE) { - ZeroMemory(hist_time, sizeof(hist_time)); - ZeroMemory(hist_size, sizeof(hist_size)); + std::memset(hist_time, 0, sizeof(hist_time)); + std::memset(hist_size, 0, sizeof(hist_size)); last_recv_time = NetLayer::GetUTC(); } + NetPeer::~NetPeer() { send_list.destroy(); @@ -50,7 +62,6 @@ NetPeer::~NetPeer() multi_recv_list.destroy(); } -// +-------------------------------------------------------------------+ bool NetPeer::SendMessage(NetMsg* msg) @@ -79,8 +90,8 @@ NetPeer::SendMessage(NetMsg* msg) if (msg->IsScatter()) list = &multi_send_list; - DWORD nparts = msg->Length() / chunk_size; - DWORD extra = msg->Length() % chunk_size; + std::uint32_t nparts = msg->Length() / chunk_size; + std::uint32_t extra = msg->Length() % chunk_size; if (extra > 0) nparts++; @@ -88,20 +99,21 @@ NetPeer::SendMessage(NetMsg* msg) multi_part_buffer.msgid = multi_msg_sequence++; multi_part_buffer.nparts = nparts; - DWORD header_size = (DWORD) (&multi_part_buffer.payload) - - (DWORD) (&multi_part_buffer); + auto header_size = static_cast<std::uint32_t>( + reinterpret_cast<std::uint8_t*>(&multi_part_buffer.payload) - + reinterpret_cast<std::uint8_t*>(&multi_part_buffer)); - const BYTE* p = msg->Data(); + const std::uint8_t* p = msg->Data(); - for (DWORD i = 0; i < nparts; i++) { + for (std::uint32_t i = 0; i < nparts; i++) { multi_part_buffer.partno = i; NetMsg* part = 0; - DWORD part_size = chunk_size; + std::uint32_t part_size = chunk_size; if (i == nparts-1 && extra > 0) // last partial payload part_size = extra; - CopyMemory(multi_part_buffer.payload, p, part_size); + std::memcpy(multi_part_buffer.payload, p, part_size); p += part_size; part = new NetMsg(msg->NetID(), &multi_part_buffer, @@ -121,7 +133,6 @@ NetPeer::SendMessage(NetMsg* msg) return false; } -// +-------------------------------------------------------------------+ NetMsg* NetPeer::GetMessage() @@ -135,7 +146,6 @@ NetPeer::GetMessage() return 0; } -// +-------------------------------------------------------------------+ NetGram* NetPeer::ComposeGram() @@ -203,12 +213,12 @@ NetPeer::ComposeGram() } if (xmit_size > 0 && nmsg > 0) { - BYTE* buffer = new BYTE[xmit_size]; - BYTE* p = buffer; + std::uint8_t* buffer = new std::uint8_t[xmit_size]; + std::uint8_t* p = buffer; if (multi_msg) { if (buffer) { - CopyMemory(p, multi_msg->Data(), multi_msg->Length()); + std::memcpy(p, multi_msg->Data(), multi_msg->Length()); p[1] = multi_msg->Length(); p += multi_msg->Length(); } @@ -221,7 +231,7 @@ NetPeer::ComposeGram() if (msg) { if (msg->IsReliable()) reliable = true; if (buffer) { - CopyMemory(p, msg->Data(), msg->Length()); + std::memcpy(p, msg->Data(), msg->Length()); p[1] = msg->Length(); p += msg->Length(); } @@ -264,7 +274,6 @@ NetPeer::ComposeGram() return g; } -// +-------------------------------------------------------------------+ bool NetPeer::ReceiveGram(NetGram* g, List<NetMsg>* q) @@ -280,11 +289,11 @@ NetPeer::ReceiveGram(NetGram* g, List<NetMsg>* q) recv_size += g->Size() - NET_GRAM_HEADER_SIZE; // PARSE THE BLOCKS: - BYTE* p = g->UserData(); + std::uint8_t* p = g->UserData(); while (p < g->Data() + g->Size()) { - BYTE block_type = p[0]; - BYTE block_size = p[1]; + std::uint8_t block_type = p[0]; + std::uint8_t block_size = p[1]; if (!block_type || !block_size) break; @@ -321,17 +330,16 @@ NetPeer::ReceiveGram(NetGram* g, List<NetMsg>* q) return false; } -// +-------------------------------------------------------------------+ bool NetPeer::OKtoSend() const { if (pps || bps) { - DWORD hist_total = 0; - DWORD hist_count = 0; - DWORD now = NetLayer::GetTime(); - DWORD hist_oldest = now; - DWORD hist_newest = 0; + std::uint32_t hist_total = 0; + std::uint32_t hist_count = 0; + std::uint32_t now = NetLayer::GetTime(); + std::uint32_t hist_oldest = now; + std::uint32_t hist_newest = 0; for (int i = 0; i < HIST_SIZE; i++) { if (hist_size[i] > 0) { @@ -348,39 +356,40 @@ NetPeer::OKtoSend() const } } - if (now - hist_newest < (DWORD) pps) + if (now - hist_newest < (std::uint32_t) pps) return false; - DWORD delta = now - hist_oldest; - DWORD avg_bps = hist_total / delta; + std::uint32_t delta = now - hist_oldest; + std::uint32_t avg_bps = hist_total / delta; - if (bps > 0 && avg_bps > (DWORD) bps) + if (bps > 0 && avg_bps > (std::uint32_t) bps) return false; } return true; } -// +-------------------------------------------------------------------+ struct PacketAssembly { - DWORD msgid; - DWORD netid; + std::uint32_t msgid; + std::uint32_t netid; int nreq; int nparts; int nbytes; }; + void NetPeer::CheckMultiRecv(List<NetMsg>* q) { const int MAX_SIMULTANEOUS_MULTI_SEQUENCES = 8; PacketAssembly assy[MAX_SIMULTANEOUS_MULTI_SEQUENCES]; - ZeroMemory(assy, sizeof(assy)); + std::memset(assy, 0, sizeof(assy)); - DWORD header_size = (DWORD) (&multi_part_buffer.payload) - - (DWORD) (&multi_part_buffer); + auto header_size = static_cast<std::uint32_t>( + reinterpret_cast<std::uint8_t*>(&multi_part_buffer.payload) - + reinterpret_cast<std::uint8_t*>(&multi_part_buffer)); // Catalog how much of each multipart sequence has been received: for (int i = 0; i < multi_recv_list.size(); i++) { @@ -406,9 +415,9 @@ NetPeer::CheckMultiRecv(List<NetMsg>* q) // is this sequence complete? if (a->msgid && a->nparts == a->nreq) { - BYTE* buffer = new BYTE[a->nbytes]; - BYTE* p = buffer; - WORD nid = 0; + std::uint8_t* buffer = new std::uint8_t[a->nbytes]; + std::uint8_t* p = buffer; + std::uint16_t nid = 0; ListIter<NetMsg> iter = multi_recv_list; while (++iter) { @@ -418,7 +427,7 @@ NetPeer::CheckMultiRecv(List<NetMsg>* q) // found part of the sequence if (m->msgid == a->msgid && netid == a->netid) { // copy it into the buffer - CopyMemory(p, m->payload, m->len - header_size); + std::memcpy(p, m->payload, m->len - header_size); p += m->len - header_size; delete iter.removeItem(); |