Skip to content

Commit 131a577

Browse files
committed
force network byte order for dmdserver
1 parent c7530fb commit 131a577

File tree

3 files changed

+76
-6
lines changed

3 files changed

+76
-6
lines changed

include/DMDUtil/DMD.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,42 @@ class DMDUTILAPI DMD
122122
uint8_t b;
123123
uint16_t width;
124124
uint16_t height;
125+
126+
void convertToHostByteOrder()
127+
{
128+
mode = static_cast<Mode>(ntohl(static_cast<uint32_t>(mode)));
129+
layout = static_cast<AlphaNumericLayout>(ntohl(static_cast<uint32_t>(layout)));
130+
depth = ntohl(depth);
131+
for (size_t i = 0; i < 256 * 64; i++)
132+
{
133+
segData[i] = ntohs(segData[i]);
134+
}
135+
for (size_t i = 0; i < 128; i++)
136+
{
137+
segData2[i] = ntohs(segData2[i]);
138+
}
139+
width = ntohs(width);
140+
height = ntohs(height);
141+
}
142+
143+
Update toNetworkByteOrder() const
144+
{
145+
Update copy = *this;
146+
copy.mode = static_cast<Mode>(htonl(static_cast<uint32_t>(mode)));
147+
copy.layout = static_cast<AlphaNumericLayout>(htonl(static_cast<uint32_t>(layout)));
148+
copy.depth = htonl(depth);
149+
for (size_t i = 0; i < 256 * 64; i++)
150+
{
151+
copy.segData[i] = htons(segData[i]);
152+
}
153+
for (size_t i = 0; i < 128; i++)
154+
{
155+
copy.segData2[i] = htons(segData2[i]);
156+
}
157+
copy.width = htons(width);
158+
copy.height = htons(height);
159+
return copy;
160+
}
125161
};
126162

127163
struct StreamHeader
@@ -134,6 +170,28 @@ class DMDUTILAPI DMD
134170
uint8_t buffered = 0; // 0 => unbuffered, 1 => buffered
135171
uint8_t disconnectOthers = 0; // 0 => no, 1 => yes
136172
uint32_t length = 0;
173+
174+
void convertToHostByteOrder()
175+
{
176+
version = ntohs(version);
177+
mode = static_cast<Mode>(ntohl(static_cast<uint32_t>(mode)));
178+
width = ntohs(width);
179+
height = ntohs(height);
180+
length = ntohl(length);
181+
buffered = ntohs(buffered);
182+
disconnectOthers = ntohs(disconnectOthers);
183+
}
184+
185+
void convertToNetworkByteOrder()
186+
{
187+
version = htons(version);
188+
mode = static_cast<Mode>(htonl(static_cast<uint32_t>(mode)));
189+
width = htons(width);
190+
height = htons(height);
191+
length = htonl(length);
192+
buffered = htons(buffered);
193+
disconnectOthers = htons(disconnectOthers);
194+
}
137195
};
138196

139197
struct PathsHeader
@@ -142,6 +200,10 @@ class DMDUTILAPI DMD
142200
char name[DMDUTIL_MAX_NAME_SIZE] = {0};
143201
char altColorPath[DMDUTIL_MAX_PATH_SIZE] = {0};
144202
char pupVideosPath[DMDUTIL_MAX_PATH_SIZE] = {0};
203+
204+
void convertToHostByteOrder() {}
205+
206+
void convertToNetworkByteOrder() {}
145207
};
146208
#pragma pack(pop) // Reset to default packing
147209

src/DMD.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,13 +396,16 @@ void DMD::QueueUpdate(const std::shared_ptr<Update> dmdUpdate, bool buffered)
396396
StreamHeader streamHeader;
397397
streamHeader.buffered = (uint8_t)buffered;
398398
streamHeader.disconnectOthers = (uint8_t)m_dmdServerDisconnectOthers;
399+
streamHeader.convertToNetworkByteOrder();
399400
m_pDMDServerConnector->Write(&streamHeader, sizeof(StreamHeader));
400401
PathsHeader pathsHeader;
401402
strcpy(pathsHeader.name, m_romName);
402403
strcpy(pathsHeader.altColorPath, m_altColorPath);
403404
strcpy(pathsHeader.pupVideosPath, m_pupVideosPath);
405+
pathsHeader.convertToNetworkByteOrder();
404406
m_pDMDServerConnector->Write(&pathsHeader, sizeof(PathsHeader));
405-
m_pDMDServerConnector->Write(dmdUpdate.get(), sizeof(Update));
407+
Update dmdUpdateNetwork = dmdUpdate->toNetworkByteOrder();
408+
m_pDMDServerConnector->Write(&dmdUpdateNetwork, sizeof(Update));
406409

407410
if (streamHeader.disconnectOthers != 0) m_dmdServerDisconnectOthers = false;
408411
}

src/dmdServer.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,8 @@ void run(sockpp::tcp_socket sock, uint32_t threadId)
9494

9595
if (n == sizeof(DMDUtil::DMD::StreamHeader))
9696
{
97-
// At the moment the server only listens on localhost.
98-
// Therefore, we don't have to take care about litte vs. big endian and can use memcpy.
9997
memcpy(pStreamHeader, buffer, n);
98+
pStreamHeader->convertToHostByteOrder();
10099
if (strcmp(pStreamHeader->header, "DMDStream") == 0 && pStreamHeader->version == 1)
101100
{
102101
if (opt_verbose)
@@ -124,6 +123,7 @@ void run(sockpp::tcp_socket sock, uint32_t threadId)
124123
{
125124
DMDUtil::DMD::PathsHeader pathsHeader;
126125
memcpy(&pathsHeader, buffer, n);
126+
pathsHeader.convertToHostByteOrder();
127127

128128
if (strcmp(pathsHeader.header, "Paths") == 0 &&
129129
(n = sock.read_n(buffer, sizeof(DMDUtil::DMD::Update))) == sizeof(DMDUtil::DMD::Update) &&
@@ -135,6 +135,7 @@ void run(sockpp::tcp_socket sock, uint32_t threadId)
135135
pathsHeader.name, pathsHeader.altColorPath, pathsHeader.pupVideosPath);
136136
auto data = std::make_shared<DMDUtil::DMD::Update>();
137137
memcpy(data.get(), buffer, n);
138+
data->convertToHostByteOrder();
138139

139140
if (data->width <= DMDSERVER_MAX_WIDTH && data->height <= DMDSERVER_MAX_HEIGHT)
140141
{
@@ -165,9 +166,13 @@ void run(sockpp::tcp_socket sock, uint32_t threadId)
165166
threadId == currentThreadId && pStreamHeader->width <= DMDSERVER_MAX_WIDTH &&
166167
pStreamHeader->height <= DMDSERVER_MAX_HEIGHT)
167168
{
168-
// At the moment the server only listens on localhost.
169-
// Therefore, we don't have to take care about litte vs. big endian and can use the buffer as uint16_t as
170-
// it is.
169+
uint16_t* pixelData = (uint16_t*)buffer;
170+
size_t pixelCount = pStreamHeader->length / sizeof(uint16_t);
171+
for (size_t i = 0; i < pixelCount; i++)
172+
{
173+
pixelData[i] = ntohs(pixelData[i]);
174+
}
175+
171176
pDmd->UpdateRGB16Data((uint16_t*)buffer, pStreamHeader->width, pStreamHeader->height,
172177
pStreamHeader->buffered == 1);
173178
}

0 commit comments

Comments
 (0)