From 77a3554616362939d78649fd257f65e464312e68 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Wed, 8 Sep 2021 17:11:01 +0300 Subject: fixes #2897 (EMLan: hangup on exit) --- protocols/EmLanProto/src/lan.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'protocols/EmLanProto') diff --git a/protocols/EmLanProto/src/lan.cpp b/protocols/EmLanProto/src/lan.cpp index deff7edd18..42787169d2 100644 --- a/protocols/EmLanProto/src/lan.cpp +++ b/protocols/EmLanProto/src/lan.cpp @@ -80,8 +80,8 @@ void CLan::StopListen() if (m_hListenThread) { shutdown(m_income, SD_BOTH); - WaitForSingleObject(m_hListenThread, INFINITE); - m_hListenThread = nullptr; + while (m_hListenThread) + SleepEx(100, TRUE); } if (m_hAcceptTCPThread) { TerminateThread(m_hAcceptTCPThread, 0); @@ -176,22 +176,23 @@ void __cdecl CLan::ListenProc(void *lpParameter) void CLan::Listen() { + if (m_mode != LM_LISTEN) + return; + MThreadHandle threadLock(m_hListenThread); + mir_ptr buf((char*)mir_alloc(65536)); - if (m_mode == LM_LISTEN) { - char buf[65536]; - while (true) { - sockaddr_in addr; - int addrLen = sizeof(addr); - Sleep(20); - int recLen = recvfrom(m_income, buf, 65536, 0, (sockaddr*)&addr, &addrLen); - if (recLen == SOCKET_ERROR) - break; - - OnRecvPacket((u_char*)buf, recLen, addr.sin_addr); - } + while (true) { + Sleep(20); + + sockaddr_in addr; + int addrLen = sizeof(addr); + int recLen = recvfrom(m_income, buf, 65536, 0, (sockaddr*)&addr, &addrLen); + if (recLen == SOCKET_ERROR) + break; + + OnRecvPacket((u_char*)buf.get(), recLen, addr.sin_addr); } - m_hListenThread = nullptr; } void CLan::SendPacketBroadcast(const u_char* mes, int len) -- cgit v1.2.3