From 31c53da91fe7ccaa46b8039cb14cd9bfbaa32c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludwig=20Fr=C3=BChsch=C3=BCtz?= Date: Wed, 21 Apr 2021 12:05:39 +0200 Subject: [PATCH] Catch runtime error when port cannot be opened. closes #5 --- OSC2AHK/dllmain.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/OSC2AHK/dllmain.cpp b/OSC2AHK/dllmain.cpp index e46ca9b..dbeda60 100644 --- a/OSC2AHK/dllmain.cpp +++ b/OSC2AHK/dllmain.cpp @@ -63,11 +63,16 @@ void runOscThread(unsigned int port) thePacketListener = new ThePacketListener; if (sock) sock->~UdpListeningReceiveSocket(); - sock = new UdpListeningReceiveSocket( - IpEndpointName(IpEndpointName::ANY_ADDRESS, port), - thePacketListener); - - sock->RunUntilSigInt(); //<<--- this is the loop + try { + sock = new UdpListeningReceiveSocket( + IpEndpointName(IpEndpointName::ANY_ADDRESS, port), + thePacketListener); + sock->RunUntilSigInt(); //<<--- this is the loop + } + catch (std::runtime_error) + { + OutputDebugString(L"Unable to open port!\r\n"); + } } /* DLL was loaded */ @@ -100,11 +105,24 @@ DLLEXPORT int open(HWND targetWindowHandle, unsigned int port) return 1; } + unsigned int timeout = 500; //timout for port opening, in ms + /*Store handle to Autohotkey window globally*/ hwnd = targetWindowHandle; /*Start OSC Thread*/ oscThread = new std::thread(runOscThread, port); + while ((!sock || !sock->IsBound()) && timeout) + { + timeout--; + Sleep(1); + } + if (!sock || !sock->IsBound()) + { + OutputDebugString(L"open: cannot open port!!\r\n"); + return 2; + } + OutputDebugString(L"open: Opened.\r\n"); return 0; }