Browse Source

Fixed multithreading and therefore close() and open() functionality. closes #3. Added ability to close() but keep listeners.

tags/0.1
Ludwig Frühschütz 3 years ago
parent
commit
11e62b8425
3 changed files with 23 additions and 11 deletions
  1. +15
    -9
      OSC2AHK/dllmain.cpp
  2. +1
    -1
      OSC2AHK/dllmain.h
  3. +7
    -1
      msgtest.ahk

+ 15
- 9
OSC2AHK/dllmain.cpp View File

@ -49,15 +49,15 @@ std::thread* oscThread;
* UdpListeningReceiveSocket runs in a blocking loop. [RunUntilSigInt()]*/
void runOscThread(unsigned int port)
{
thePacketListener = new ThePacketListener;
if (!thePacketListener)
thePacketListener = new ThePacketListener;
if (sock) sock->~UdpListeningReceiveSocket();
sock = new UdpListeningReceiveSocket(
IpEndpointName(IpEndpointName::ANY_ADDRESS, port),
thePacketListener);
sock->RunUntilSigInt(); //<<--- this is the loop
//Cleanup after sock exited the loop
sock = NULL;
thePacketListener = NULL;
sock->RunUntilSigInt(); //<<--- this is the loop
}
/* DLL was loaded */
@ -69,9 +69,11 @@ BOOL APIENTRY DllMain( HMODULE hModule,
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
close();
break;
@ -97,15 +99,16 @@ DLLEXPORT int open(HWND targetWindowHandle, unsigned int port)
return 0;
}
DLLEXPORT int close()
DLLEXPORT int close(unsigned int clearListeners)
{
if (sock && sock->IsBound())
if (sock)
{
sock->AsynchronousBreak();
}
oscThread->join();
oscThread = NULL;
hwnd = NULL;
listeners.clear();
if (clearListeners) listeners.clear();
return 0;
}
@ -168,7 +171,10 @@ DLLEXPORT int handleOscMsg(const osc::ReceivedMessage& m)
if (!hwnd || !sock->IsBound())
{
OutputDebugString(L"handleOscMsg: Seems open() was not called yet...\r\n");
OutputDebugString(L"handleOscMsg: Seems open() was not called yet...");
if (!hwnd) OutputDebugString(L" (hwnd)");
if (!sock->IsBound()) OutputDebugString(L" (sock)");
OutputDebugString(L"\r\n");
return -1;
}


+ 1
- 1
OSC2AHK/dllmain.h View File

@ -20,7 +20,7 @@
#define OSC_TYPE_ALL UINT_MAX
extern "C" DLLEXPORT int open(HWND targetWindowHandle, unsigned int port);
extern "C" DLLEXPORT int close();
extern "C" DLLEXPORT int close(unsigned int clearListeners = 1);
extern "C" DLLEXPORT int addListener(LPCSTR address, unsigned int messageID, unsigned int dataType = OSC_TYPE_ALL);
extern "C" DLLEXPORT int removeListener(LPCSTR address);
extern "C" DLLEXPORT int handleOscMsg(const osc::ReceivedMessage & m);


+ 7
- 1
msgtest.ahk View File

@ -99,4 +99,10 @@ do_exit:
ExitApp
return
Esc::GoSub, do_exit
Esc::
ExitApp
^a::
DllCall("OSC2AHK.dll\close", UInt, 0)
;msgbox,esc
DllCall("OSC2AHK.dll\open", UInt, hWnd, UInt, 7001)

Loading…
Cancel
Save