Skip to content

Commit 41eff53

Browse files
committed
fixed thread disconnect
1 parent cdb2dae commit 41eff53

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/DMDServer.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
9393
ssize_t n;
9494
// Disconnect others is only allowed once per client.
9595
bool handleDisconnectOthers = true;
96-
uint32_t disconnectOtherClients = 0;
96+
bool logged = false;
9797

9898
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: New DMD client %d connected", threadId, threadId);
9999

100-
while (threadId == m_currentThreadId || disconnectOtherClients == 0 || disconnectOtherClients <= threadId)
100+
while (threadId == m_currentThreadId || m_disconnectOtherClients == 0 || m_disconnectOtherClients <= threadId)
101101
{
102102
n = sock.read_n(buffer, sizeof(DMDUtil::DMD::StreamHeader));
103103
// If the client disconnects or if a network error ocurres, exit the loop and terminate this thread.
@@ -118,7 +118,7 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
118118
if (handleDisconnectOthers && threadId == m_currentThreadId && pStreamHeader->disconnectOthers)
119119
{
120120
m_threadMutex.lock();
121-
disconnectOtherClients = threadId;
121+
m_disconnectOtherClients = threadId;
122122
m_threadMutex.unlock();
123123
handleDisconnectOthers = false;
124124
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Other clients will be disconnected", threadId);
@@ -143,6 +143,7 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
143143
auto data = std::make_shared<DMDUtil::DMD::Update>();
144144
memcpy(data.get(), buffer, n);
145145
data->convertToHostByteOrder();
146+
logged = false;
146147

147148
if (data->width <= DMDSERVER_MAX_WIDTH && data->height <= DMDSERVER_MAX_HEIGHT)
148149
{
@@ -159,6 +160,11 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
159160
}
160161
else if (threadId != m_currentThreadId)
161162
{
163+
if (!logged)
164+
{
165+
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d blocks the DMD", threadId, m_currentThreadId);
166+
logged = true;
167+
}
162168
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d blocks the DMD", threadId, m_currentThreadId);
163169
}
164170
else
@@ -179,13 +185,17 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
179185
{
180186
pixelData[i] = ntohs(pixelData[i]);
181187
}
182-
188+
logged = false;
183189
m_dmd->UpdateRGB16Data((uint16_t*)buffer, pStreamHeader->width, pStreamHeader->height,
184190
pStreamHeader->buffered == 1);
185191
}
186192
else if (threadId != m_currentThreadId)
187193
{
188-
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d blocks the DMD", threadId, m_currentThreadId);
194+
if (!logged)
195+
{
196+
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d blocks the DMD", threadId, m_currentThreadId);
197+
logged = true;
198+
}
189199
}
190200
else
191201
{
@@ -198,11 +208,16 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
198208
threadId == m_currentThreadId && pStreamHeader->width <= DMDSERVER_MAX_WIDTH &&
199209
pStreamHeader->height <= DMDSERVER_MAX_HEIGHT)
200210
{
211+
logged = false;
201212
m_dmd->UpdateRGB24Data(buffer, pStreamHeader->width, pStreamHeader->height, pStreamHeader->buffered == 1);
202213
}
203214
else if (threadId != m_currentThreadId)
204215
{
205-
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d blocks the DMD", threadId, m_currentThreadId);
216+
if (!logged)
217+
{
218+
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d blocks the DMD", threadId, m_currentThreadId);
219+
logged = true;
220+
}
206221
}
207222
else
208223
{
@@ -222,8 +237,8 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
222237
}
223238
}
224239

225-
if (disconnectOtherClients != 0 && disconnectOtherClients > threadId)
226-
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d requested disconnect", threadId, disconnectOtherClients);
240+
if (m_disconnectOtherClients != 0 && m_disconnectOtherClients > threadId)
241+
DMDUtil::Log(DMDUtil_LogLevel_INFO, "%d: Client %d requested disconnect", threadId, m_disconnectOtherClients);
227242

228243
// Display a buffered frame or clear the display on disconnect of the current thread.
229244
if (threadId == m_currentThreadId && !pStreamHeader->buffered && !m_dmd->QueueBuffer())
@@ -239,7 +254,7 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
239254
m_threads.erase(remove(m_threads.begin(), m_threads.end(), threadId), m_threads.end());
240255
if (threadId == m_currentThreadId)
241256
{
242-
if (disconnectOtherClients == threadId)
257+
if (m_disconnectOtherClients == threadId)
243258
{
244259
// Wait until all other threads ended or a new client connnects in between.
245260
while (m_threads.size() >= 1 && m_currentThreadId == threadId)
@@ -251,7 +266,7 @@ void DMDServer::ClientThread(sockpp::tcp_socket sock, uint32_t threadId)
251266
}
252267

253268
m_currentThreadId = 0;
254-
disconnectOtherClients = 0;
269+
m_disconnectOtherClients = 0;
255270
}
256271
else
257272
{

0 commit comments

Comments
 (0)