Catch runtime error when port cannot be opened. closes #5

This commit is contained in:
Ludwig Frühschütz 2021-04-21 12:05:39 +02:00
parent 898bd1200d
commit 31c53da91f

View File

@ -63,11 +63,16 @@ void runOscThread(unsigned int port)
thePacketListener = new ThePacketListener; thePacketListener = new ThePacketListener;
if (sock) sock->~UdpListeningReceiveSocket(); if (sock) sock->~UdpListeningReceiveSocket();
sock = new UdpListeningReceiveSocket( try {
IpEndpointName(IpEndpointName::ANY_ADDRESS, port), sock = new UdpListeningReceiveSocket(
thePacketListener); IpEndpointName(IpEndpointName::ANY_ADDRESS, port),
thePacketListener);
sock->RunUntilSigInt(); //<<--- this is the loop sock->RunUntilSigInt(); //<<--- this is the loop
}
catch (std::runtime_error)
{
OutputDebugString(L"Unable to open port!\r\n");
}
} }
/* DLL was loaded */ /* DLL was loaded */
@ -100,11 +105,24 @@ DLLEXPORT int open(HWND targetWindowHandle, unsigned int port)
return 1; return 1;
} }
unsigned int timeout = 500; //timout for port opening, in ms
/*Store handle to Autohotkey window globally*/ /*Store handle to Autohotkey window globally*/
hwnd = targetWindowHandle; hwnd = targetWindowHandle;
/*Start OSC Thread*/ /*Start OSC Thread*/
oscThread = new std::thread(runOscThread, port); 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"); OutputDebugString(L"open: Opened.\r\n");
return 0; return 0;
} }