From acecd34e8ffe8c77a6e7fb4f0fa9b8375a0d176a Mon Sep 17 00:00:00 2001 From: Aki Date: Wed, 30 Mar 2022 23:42:15 +0200 Subject: Brought NetLink closer to standard --- NetEx/NetLink.cpp | 146 ++++++++++++++++++++++++++++++------------------------ NetEx/NetLink.h | 84 +++++++++++++++---------------- 2 files changed, 120 insertions(+), 110 deletions(-) (limited to 'NetEx') diff --git a/NetEx/NetLink.cpp b/NetEx/NetLink.cpp index 10e817a..37ca318 100644 --- a/NetEx/NetLink.cpp +++ b/NetEx/NetLink.cpp @@ -3,61 +3,84 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Network (IP) Socket Wrapper Implementation + OVERVIEW + ======== + Network (IP) Socket Wrapper Implementation */ #include "NetLink.h" + +#include +#include +#include +#include +#include + +#include "List.h" +#include "NetAddr.h" #include "NetGram.h" -#include "NetMsg.h" -#include "NetPeer.h" #include "NetLayer.h" +#include "NetPeer.h" +#include "NetSock.h" +#include "Text.h" -// +-------------------------------------------------------------------+ -DWORD WINAPI NetLinkProc(LPVOID link); +const std::uint32_t UDP_HEADER_SIZE = 34; -const DWORD UDP_HEADER_SIZE = 34; -// +-------------------------------------------------------------------+ // client-side ctor -NetLink::NetLink() - : hnet(0), shutdown(false), traffic_time(50), resend_time(300), - packets_sent(0), packets_recv(0), bytes_sent(0), bytes_recv(0), retries(0), drops(0), lag(100) +NetLink::NetLink() : + shutdown(false), + traffic_time(50), + resend_time(300), + packets_sent(0), + packets_recv(0), + bytes_sent(0), + bytes_recv(0), + retries(0), + drops(0), + lag(100) { - ZeroMemory(lag_samples, sizeof(lag_samples)); + std::memset(lag_samples, 0, sizeof(lag_samples)); lag_index = 0; - DWORD thread_id = 0; - hnet = CreateThread(0, 4096, NetLinkProc, (LPVOID) this, 0, &thread_id); + hnet = std::thread([&]{ DoSendRecv(); }); } + // server-side ctor -NetLink::NetLink(NetAddr& a) - : addr(a), hnet(0), shutdown(false), traffic_time(50), resend_time(300), - packets_sent(0), packets_recv(0), bytes_sent(0), bytes_recv(0), retries(0), drops(0), lag(100) +NetLink::NetLink(NetAddr& a) : + addr(a), + shutdown(false), + traffic_time(50), + resend_time(300), + packets_sent(0), + packets_recv(0), + bytes_sent(0), + bytes_recv(0), + retries(0), + drops(0), + lag(100) { - ZeroMemory(lag_samples, sizeof(lag_samples)); + std::memset(lag_samples, 0, sizeof(lag_samples)); lag_index = 0; sock.bind(addr); - DWORD thread_id = 0; - hnet = CreateThread(0, 4096, NetLinkProc, (LPVOID) this, 0, &thread_id); + hnet = std::thread([&]{ DoSendRecv(); }); } + NetLink::~NetLink() { if (!shutdown) { shutdown = true; } - if (hnet) { - WaitForSingleObject(hnet, 2000); - CloseHandle(hnet); + if (hnet.joinable()) { + hnet.join(); } send_list.destroy(); // packets waiting to be ack'ed must be destroyed @@ -65,23 +88,25 @@ NetLink::~NetLink() peer_list.destroy(); // but the net link owns the peers! } -// +--------------------------------------------------------------------+ -static DWORD base_netid = 1000; +static std::uint32_t s_base_netid = 1000; -DWORD -NetLink::AddPeer(const char* a, WORD p) + +std::uint32_t +NetLink::AddPeer(const char* a, std::uint16_t p) { return AddPeer(NetAddr(a, p)); } -DWORD -NetLink::AddPeer(DWORD a, WORD p) + +std::uint32_t +NetLink::AddPeer(std::uint32_t a, std::uint16_t p) { return AddPeer(NetAddr(a, p)); } -DWORD + +std::uint32_t NetLink::AddPeer(const NetAddr& a) { if (!a.IPAddr()) @@ -91,7 +116,7 @@ NetLink::AddPeer(const NetAddr& a) const std::lock_guard lock(sync); if (!peer) { - peer = new NetPeer(a, base_netid++); + peer = new NetPeer(a, s_base_netid++); if (peer) peer_list.append(peer); } @@ -102,20 +127,21 @@ NetLink::AddPeer(const NetAddr& a) return 0; } -// +--------------------------------------------------------------------+ bool -NetLink::SendMessage(DWORD nid, void* d, int l, BYTE f) +NetLink::SendMessage(std::uint32_t nid, void* d, int l, std::uint8_t f) { return SendMessage(new NetMsg(nid, d, l, f)); } + bool -NetLink::SendMessage(DWORD nid, BYTE type, const char* text, int len, BYTE f) +NetLink::SendMessage(std::uint32_t nid, std::uint8_t type, const char* text, int len, std::uint8_t f) { return SendMessage(new NetMsg(nid, type, text, len, f)); } + bool NetLink::SendMessage(NetMsg* msg) { @@ -135,10 +161,9 @@ NetLink::SendMessage(NetMsg* msg) return false; } -// +--------------------------------------------------------------------+ NetMsg* -NetLink::GetMessage(DWORD netid) +NetLink::GetMessage(std::uint32_t netid) { NetMsg* msg = 0; @@ -157,7 +182,6 @@ NetLink::GetMessage(DWORD netid) return msg; } -// +--------------------------------------------------------------------+ NetMsg* NetLink::GetMessage() @@ -187,7 +211,6 @@ NetLink::GetMessage() return msg; } -// +--------------------------------------------------------------------+ void NetLink::Shutdown() @@ -195,19 +218,8 @@ NetLink::Shutdown() shutdown = true; } -// +--------------------------------------------------------------------+ -DWORD WINAPI NetLinkProc(LPVOID link) -{ - NetLink* netlink = (NetLink*) link; - - if (netlink) - return netlink->DoSendRecv(); - - return (DWORD) E_POINTER; -} - -DWORD +std::uint32_t NetLink::DoSendRecv() { while (!shutdown) { @@ -226,12 +238,13 @@ NetLink::DoSendRecv() } sync.unlock(); - Sleep(traffic_time); + std::this_thread::sleep_for(std::chrono::milliseconds(traffic_time)); } return 0; } + void NetLink::ReadPackets() { @@ -254,6 +267,7 @@ NetLink::ReadPackets() } } + void NetLink::SendPackets() { @@ -286,7 +300,6 @@ NetLink::SendPackets() } } -// +--------------------------------------------------------------------+ void NetLink::SendNetGram(NetGram* gram) @@ -311,6 +324,7 @@ NetLink::SendNetGram(NetGram* gram) } } + NetGram* NetLink::RecvNetGram() { @@ -323,7 +337,6 @@ NetLink::RecvNetGram() return new NetGram(from, msg); } -// +--------------------------------------------------------------------+ void NetLink::AckNetGram(NetGram* gram) @@ -340,6 +353,7 @@ NetLink::AckNetGram(NetGram* gram) } } + void NetLink::ProcessAck(NetGram* gram) { @@ -353,8 +367,8 @@ NetLink::ProcessAck(NetGram* gram) int sent = send_list.index(gram); if (sent >= 0) { NetGram* orig = send_list.removeIndex(sent); - DWORD time = NetLayer::GetTime(); - DWORD msec = time - orig->SendTime(); + std::uint32_t time = NetLayer::GetTime(); + std::uint32_t msec = time - orig->SendTime(); double dlag = 0.75 * lag + 0.25 * msec; if (lag_index >= 10) lag_index = 0; @@ -368,7 +382,7 @@ NetLink::ProcessAck(NetGram* gram) delete orig; - lag = (DWORD) dlag; + lag = (std::uint32_t) dlag; if (lag > 100) resend_time = 3 * lag; @@ -379,19 +393,20 @@ NetLink::ProcessAck(NetGram* gram) } } + void NetLink::QueueNetGram(NetGram* gram) { if (!shutdown) { - DWORD sequence = 0; - NetPeer* peer = FindPeer(gram->Address()); + std::uint32_t sequence = 0; + NetPeer* peer = FindPeer(gram->Address()); const std::lock_guard lock(sync); if (peer) { sequence = peer->Sequence(); } else { - peer = new NetPeer(gram->Address(), base_netid++); + peer = new NetPeer(gram->Address(), s_base_netid++); if (peer) peer_list.append(peer); } @@ -410,7 +425,6 @@ NetLink::QueueNetGram(NetGram* gram) } } -// +--------------------------------------------------------------------+ void NetLink::DoRetries() @@ -427,8 +441,8 @@ NetLink::DoRetries() // still trying ? if (gram->Retries() > 0) { - DWORD last_send = gram->SendTime(); - DWORD delta = time - last_send; + std::uint32_t last_send = gram->SendTime(); + std::uint32_t delta = time - last_send; if (delta > resend_time) { gram->Retry(); @@ -448,10 +462,9 @@ NetLink::DoRetries() } } -// +--------------------------------------------------------------------+ NetPeer* -NetLink::FindPeer(DWORD netid) +NetLink::FindPeer(std::uint32_t netid) { const std::lock_guard lock(sync); NetPeer* peer = 0; @@ -467,6 +480,7 @@ NetLink::FindPeer(DWORD netid) return peer; } + NetPeer* NetLink::FindPeer(const NetAddr& a) { diff --git a/NetEx/NetLink.h b/NetEx/NetLink.h index 33024f3..5f5d081 100644 --- a/NetEx/NetLink.h +++ b/NetEx/NetLink.h @@ -3,32 +3,28 @@ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors Copyright (c) 1997-2006, Destroyer Studios LLC. - AUTHOR: John DiCamillo + AUTHOR: John DiCamillo - OVERVIEW - ======== - Network Link for Remote Player + OVERVIEW + ======== + Network Link for Remote Player */ - #ifndef NetLink_h #define NetLink_h +#include #include +#include -#include +#include "List.h" #include "NetAddr.h" +#include "NetGram.h" +#include "NetMsg.h" +#include "NetPeer.h" #include "NetSock.h" -#include "List.h" - -// +-------------------------------------------------------------------+ - -class NetGram; -class NetMsg; -class NetPeer; -// +-------------------------------------------------------------------+ class NetLink { @@ -43,35 +39,35 @@ public: const NetAddr& GetAddress() const { return addr; } - DWORD AddPeer(const char* a, WORD p=12345); - DWORD AddPeer(DWORD a, WORD p=12345); - DWORD AddPeer(const NetAddr& a); + std::uint32_t AddPeer(const char* a, std::uint16_t p=12345); + std::uint32_t AddPeer(std::uint32_t a, std::uint16_t p=12345); + std::uint32_t AddPeer(const NetAddr& a); - bool SendMessage(DWORD nid, void* d, int l, BYTE f=0); - bool SendMessage(DWORD nid, BYTE type, const char* text, int len, BYTE f=0); + bool SendMessage(std::uint32_t nid, void* d, int l, std::uint8_t f=0); + bool SendMessage(std::uint32_t nid, std::uint8_t type, const char* text, int len, std::uint8_t f=0); bool SendMessage(NetMsg* msg); NetMsg* GetMessage(); - NetMsg* GetMessage(DWORD netid); + NetMsg* GetMessage(std::uint32_t netid); virtual void Shutdown(); - DWORD DoSendRecv(); + std::uint32_t DoSendRecv(); - DWORD GetResendInterval() const { return resend_time; } - void SetResendInterval(DWORD t) { resend_time = t; } - DWORD GetTrafficInterval() const { return traffic_time; } - void SetTrafficInterval(DWORD t) { traffic_time = t; } + std::uint32_t GetResendInterval() const { return resend_time; } + void SetResendInterval(std::uint32_t t) { resend_time = t; } + std::uint32_t GetTrafficInterval() const { return traffic_time; } + void SetTrafficInterval(std::uint32_t t) { traffic_time = t; } - DWORD GetPacketsSent() const { return packets_sent; } - DWORD GetPacketsRecv() const { return packets_recv; } - DWORD GetBytesSent() const { return bytes_sent; } - DWORD GetBytesRecv() const { return bytes_recv; } - DWORD GetRetries() const { return retries; } - DWORD GetDrops() const { return drops; } - DWORD GetLag() const { return lag; } + std::uint32_t GetPacketsSent() const { return packets_sent; } + std::uint32_t GetPacketsRecv() const { return packets_recv; } + std::uint32_t GetBytesSent() const { return bytes_sent; } + std::uint32_t GetBytesRecv() const { return bytes_recv; } + std::uint32_t GetRetries() const { return retries; } + std::uint32_t GetDrops() const { return drops; } + std::uint32_t GetLag() const { return lag; } NetPeer* FindPeer(const NetAddr& a); - NetPeer* FindPeer(DWORD netid); + NetPeer* FindPeer(std::uint32_t netid); protected: void SendNetGram(NetGram* g); @@ -90,22 +86,22 @@ protected: List recv_list; List peer_list; - HANDLE hnet; + std::thread hnet; bool shutdown; std::mutex sync; - DWORD resend_time; - DWORD traffic_time; + std::uint32_t resend_time; + std::uint32_t traffic_time; - DWORD packets_sent; - DWORD packets_recv; - DWORD bytes_sent; - DWORD bytes_recv; - DWORD retries; - DWORD drops; - DWORD lag; + std::uint32_t packets_sent; + std::uint32_t packets_recv; + std::uint32_t bytes_sent; + std::uint32_t bytes_recv; + std::uint32_t retries; + std::uint32_t drops; + std::uint32_t lag; - DWORD lag_samples[10]; + std::uint32_t lag_samples[10]; int lag_index; }; -- cgit v1.1