Skip to content

Commit f3aa0a0

Browse files
authored
misc: moved network fuctions from header to cpp file (#81)
1 parent b8389b3 commit f3aa0a0

File tree

2 files changed

+86
-81
lines changed

2 files changed

+86
-81
lines changed

include/DMDUtil/DMD.h

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
#define DMDUTIL_MAX_PATH_SIZE 256
1616
#define DMDUTIL_MAX_TRANSITIONAL_FRAME_DURATION 25
1717

18-
#if defined(_WIN32) || defined(_WIN64)
19-
#include <winsock2.h> // Windows byte-order functions
20-
#else
21-
#include <arpa/inet.h> // Linux/macOS byte-order functions
22-
#endif
23-
2418
#include <atomic>
2519
#include <condition_variable>
2620
#include <cstdint>
@@ -129,43 +123,8 @@ class DMDUTILAPI DMD
129123
uint16_t width;
130124
uint16_t height;
131125

132-
void convertToHostByteOrder()
133-
{
134-
// uint8_t and bool are not converted, as they are already in host byte order.
135-
mode = static_cast<Mode>(ntohl(static_cast<uint32_t>(mode)));
136-
layout = static_cast<AlphaNumericLayout>(ntohl(static_cast<uint32_t>(layout)));
137-
depth = ntohl(depth);
138-
for (size_t i = 0; i < 256 * 64; i++)
139-
{
140-
segData[i] = ntohs(segData[i]);
141-
}
142-
for (size_t i = 0; i < 128; i++)
143-
{
144-
segData2[i] = ntohs(segData2[i]);
145-
}
146-
width = ntohs(width);
147-
height = ntohs(height);
148-
}
149-
150-
Update toNetworkByteOrder() const
151-
{
152-
// uint8_t and bool are not converted, as they are already in network byte order.
153-
Update copy = *this;
154-
copy.mode = static_cast<Mode>(htonl(static_cast<uint32_t>(mode)));
155-
copy.layout = static_cast<AlphaNumericLayout>(htonl(static_cast<uint32_t>(layout)));
156-
copy.depth = htonl(depth);
157-
for (size_t i = 0; i < 256 * 64; i++)
158-
{
159-
copy.segData[i] = htons(segData[i]);
160-
}
161-
for (size_t i = 0; i < 128; i++)
162-
{
163-
copy.segData2[i] = htons(segData2[i]);
164-
}
165-
copy.width = htons(width);
166-
copy.height = htons(height);
167-
return copy;
168-
}
126+
DMDUTILAPI void convertToHostByteOrder();
127+
DMDUTILAPI Update toNetworkByteOrder() const;
169128
};
170129

171130
struct StreamHeader
@@ -179,23 +138,8 @@ class DMDUTILAPI DMD
179138
uint8_t disconnectOthers = 0; // 0 => no, 1 => yes
180139
uint32_t length = 0;
181140

182-
void convertToHostByteOrder()
183-
{
184-
// uint8_t and char are not converted, as they are already in host byte order.
185-
mode = static_cast<Mode>(ntohl(static_cast<uint32_t>(mode)));
186-
width = ntohs(width);
187-
height = ntohs(height);
188-
length = ntohl(length);
189-
}
190-
191-
void convertToNetworkByteOrder()
192-
{
193-
// uint8_t and char are not converted, as they are already in network byte order.
194-
mode = static_cast<Mode>(htonl(static_cast<uint32_t>(mode)));
195-
width = htons(width);
196-
height = htons(height);
197-
length = htonl(length);
198-
}
141+
DMDUTILAPI void convertToHostByteOrder();
142+
DMDUTILAPI void convertToNetworkByteOrder();
199143
};
200144

201145
struct PathsHeader
@@ -206,7 +150,6 @@ class DMDUTILAPI DMD
206150
char pupVideosPath[DMDUTIL_MAX_PATH_SIZE] = {0};
207151

208152
void convertToHostByteOrder() {}
209-
210153
void convertToNetworkByteOrder() {}
211154
};
212155
#pragma pack(pop) // Reset to default packing

src/DMD.cpp

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
#include "DMDUtil/LevelDMD.h"
66
#include "DMDUtil/RGB24DMD.h"
77

8+
#if defined(_WIN32) || defined(_WIN64)
9+
#include <winsock2.h> // Windows byte-order functions
10+
#else
11+
#include <arpa/inet.h> // Linux/macOS byte-order functions
12+
#endif
13+
814
#if !( \
915
(defined(__APPLE__) && ((defined(TARGET_OS_IOS) && TARGET_OS_IOS) || (defined(TARGET_OS_TV) && TARGET_OS_TV))) || \
1016
defined(__ANDROID__))
@@ -71,6 +77,62 @@ class DMDServerConnector
7177

7278
bool DMD::m_finding = false;
7379

80+
void DMD::Update::convertToHostByteOrder()
81+
{
82+
// uint8_t and bool are not converted, as they are already in host byte order.
83+
mode = static_cast<Mode>(ntohl(static_cast<uint32_t>(mode)));
84+
layout = static_cast<AlphaNumericLayout>(ntohl(static_cast<uint32_t>(layout)));
85+
depth = ntohl(depth);
86+
for (size_t i = 0; i < 256 * 64; i++)
87+
{
88+
segData[i] = ntohs(segData[i]);
89+
}
90+
for (size_t i = 0; i < 128; i++)
91+
{
92+
segData2[i] = ntohs(segData2[i]);
93+
}
94+
width = ntohs(width);
95+
height = ntohs(height);
96+
}
97+
98+
DMD::Update DMD::Update::toNetworkByteOrder() const
99+
{
100+
// uint8_t and bool are not converted, as they are already in network byte order.
101+
Update copy = *this;
102+
copy.mode = static_cast<Mode>(htonl(static_cast<uint32_t>(mode)));
103+
copy.layout = static_cast<AlphaNumericLayout>(htonl(static_cast<uint32_t>(layout)));
104+
copy.depth = htonl(depth);
105+
for (size_t i = 0; i < 256 * 64; i++)
106+
{
107+
copy.segData[i] = htons(segData[i]);
108+
}
109+
for (size_t i = 0; i < 128; i++)
110+
{
111+
copy.segData2[i] = htons(segData2[i]);
112+
}
113+
copy.width = htons(width);
114+
copy.height = htons(height);
115+
return copy;
116+
}
117+
118+
void DMD::StreamHeader::convertToHostByteOrder()
119+
{
120+
// uint8_t and char are not converted, as they are already in host byte order.
121+
mode = static_cast<Mode>(ntohl(static_cast<uint32_t>(mode)));
122+
width = ntohs(width);
123+
height = ntohs(height);
124+
length = ntohl(length);
125+
}
126+
127+
void DMD::StreamHeader::convertToNetworkByteOrder()
128+
{
129+
// uint8_t and char are not converted, as they are already in network byte order.
130+
mode = static_cast<Mode>(htonl(static_cast<uint32_t>(mode)));
131+
width = htons(width);
132+
height = htons(height);
133+
length = htonl(length);
134+
}
135+
74136
DMD::DMD()
75137
{
76138
for (uint8_t i = 0; i < DMDUTIL_FRAME_BUFFER_SIZE; i++)
@@ -622,8 +684,8 @@ void DMD::DmdFrameThread()
622684
{
623685
char name[DMDUTIL_MAX_NAME_SIZE] = {0};
624686

625-
m_dmdFrameReady.load(std::memory_order_acquire);
626-
m_stopFlag.load(std::memory_order_acquire);
687+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
688+
(void)m_stopFlag.load(std::memory_order_acquire);
627689

628690
while (true)
629691
{
@@ -666,8 +728,8 @@ void DMD::ZeDMDThread()
666728
uint8_t indexBuffer[256 * 64] = {0};
667729
uint8_t renderBuffer[256 * 64 * 3] = {0};
668730

669-
m_dmdFrameReady.load(std::memory_order_acquire);
670-
m_stopFlag.load(std::memory_order_acquire);
731+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
732+
(void)m_stopFlag.load(std::memory_order_acquire);
671733

672734
Config* const pConfig = Config::GetInstance();
673735
bool showNotColorizedFrames = pConfig->IsShowNotColorizedFrames();
@@ -801,8 +863,8 @@ void DMD::SerumThread()
801863
uint32_t nextRotation = 0;
802864
Update* lastDmdUpdate = nullptr;
803865

804-
m_dmdFrameReady.load(std::memory_order_acquire);
805-
m_stopFlag.load(std::memory_order_acquire);
866+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
867+
(void)m_stopFlag.load(std::memory_order_acquire);
806868

807869
Config* const pConfig = Config::GetInstance();
808870
bool showNotColorizedFrames = pConfig->IsShowNotColorizedFrames();
@@ -1024,8 +1086,8 @@ void DMD::PixelcadeDMDThread()
10241086
uint8_t palette[PALETTE_SIZE] = {0};
10251087
uint16_t rgb565Data[128 * 32] = {0};
10261088

1027-
m_dmdFrameReady.load(std::memory_order_acquire);
1028-
m_stopFlag.load(std::memory_order_acquire);
1089+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
1090+
(void)m_stopFlag.load(std::memory_order_acquire);
10291091

10301092
Config* const pConfig = Config::GetInstance();
10311093
bool showNotColorizedFrames = pConfig->IsShowNotColorizedFrames();
@@ -1196,8 +1258,8 @@ void DMD::LevelDMDThread()
11961258
uint8_t bufferPosition = 0;
11971259
uint8_t renderBuffer[256 * 64] = {0};
11981260

1199-
m_dmdFrameReady.load(std::memory_order_acquire);
1200-
m_stopFlag.load(std::memory_order_acquire);
1261+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
1262+
(void)m_stopFlag.load(std::memory_order_acquire);
12011263

12021264
while (true)
12031265
{
@@ -1243,8 +1305,8 @@ void DMD::RGB24DMDThread()
12431305
uint8_t renderBuffer[256 * 64] = {0};
12441306
uint8_t rgb24Data[256 * 64 * 3] = {0};
12451307

1246-
m_dmdFrameReady.load(std::memory_order_acquire);
1247-
m_stopFlag.load(std::memory_order_acquire);
1308+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
1309+
(void)m_stopFlag.load(std::memory_order_acquire);
12481310

12491311
Config* const pConfig = Config::GetInstance();
12501312
bool showNotColorizedFrames = pConfig->IsShowNotColorizedFrames();
@@ -1391,8 +1453,8 @@ void DMD::ConsoleDMDThread()
13911453
uint8_t bufferPosition = 0;
13921454
uint8_t renderBuffer[256 * 64] = {0};
13931455

1394-
m_dmdFrameReady.load(std::memory_order_acquire);
1395-
m_stopFlag.load(std::memory_order_acquire);
1456+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
1457+
(void)m_stopFlag.load(std::memory_order_acquire);
13961458

13971459
while (true)
13981460
{
@@ -1504,8 +1566,8 @@ void DMD::DumpDMDTxtThread()
15041566
FILE* f = nullptr;
15051567
std::unordered_set<uint64_t> seenHashes;
15061568

1507-
m_dmdFrameReady.load(std::memory_order_acquire);
1508-
m_stopFlag.load(std::memory_order_acquire);
1569+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
1570+
(void)m_stopFlag.load(std::memory_order_acquire);
15091571

15101572
Config* const pConfig = Config::GetInstance();
15111573
bool dumpNotColorizedFrames = pConfig->IsDumpNotColorizedFrames();
@@ -1656,8 +1718,8 @@ void DMD::DumpDMDRawThread()
16561718
std::chrono::steady_clock::time_point start;
16571719
FILE* f = nullptr;
16581720

1659-
m_dmdFrameReady.load(std::memory_order_acquire);
1660-
m_stopFlag.load(std::memory_order_acquire);
1721+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
1722+
(void)m_stopFlag.load(std::memory_order_acquire);
16611723

16621724
while (true)
16631725
{
@@ -1730,8 +1792,8 @@ void DMD::PupDMDThread()
17301792
uint8_t palette[192] = {0};
17311793
char name[DMDUTIL_MAX_NAME_SIZE] = {0};
17321794

1733-
m_dmdFrameReady.load(std::memory_order_acquire);
1734-
m_stopFlag.load(std::memory_order_acquire);
1795+
(void)m_dmdFrameReady.load(std::memory_order_acquire);
1796+
(void)m_stopFlag.load(std::memory_order_acquire);
17351797

17361798
while (true)
17371799
{

0 commit comments

Comments
 (0)