From 576ad2b67bced04bde83de91f5e647af868c5fd4 Mon Sep 17 00:00:00 2001 From: fcolin Date: Fri, 12 Mar 2010 16:41:08 +0000 Subject: mise en conformité des messages ping /pong Correction pb a la fermeture d'ivy --- Ivy/Ivy.cxx | 10 ++-- Ivy/Ivy.h | 3 +- Ivy/IvyApplication.cxx | 6 +-- Ivy/IvyApplication.h | 3 +- Ivy/IvySynchroWnd.cxx | 139 +++++++++++++++++++++++++++++++++++++++---------- Ivy/IvySynchroWnd.h | 26 +++++++-- Ivy/ThreadedSocket.cxx | 2 +- 7 files changed, 149 insertions(+), 40 deletions(-) (limited to 'Ivy') diff --git a/Ivy/Ivy.cxx b/Ivy/Ivy.cxx index 95035d1..1640b45 100644 --- a/Ivy/Ivy.cxx +++ b/Ivy/Ivy.cxx @@ -48,7 +48,7 @@ Ivy::~Ivy() delete server; if ( synchronous ) { - delete IvySynchronousCallback::m_synchro; + IvySynchroWnd::Finish(); delete application_callback; } } @@ -59,8 +59,8 @@ Ivy::Ivy(const char* name, const char * ready, IvyApplicationCallback *callback, InitializeCriticalSection( &m_application_cs ); regexp_id = 0; synchronous = Synchronous; - if ( synchronous ) - IvySynchronousCallback::m_synchro = new IvySynchroWnd(); + IvySynchroWnd::Init(synchronous); + ready_message = ready; ApplicationName = name; @@ -413,6 +413,10 @@ void Ivy::SetBindCallback( IvyBindingCallback* bind_callback ) { binding_callback = synchronous ? new IvySynchronousBindingCallback(bind_callback) : bind_callback; } +void Ivy::SetDieCallback( IvyDieCallback* die_cb ) +{ + die_callback = synchronous ? new IvySynchronousDieCallback(die_cb) : die_cb; +} void Ivy::SetFilter(int argc, const char **argv ) { diff --git a/Ivy/Ivy.h b/Ivy/Ivy.h index 7c51f0b..e408d7e 100644 --- a/Ivy/Ivy.h +++ b/Ivy/Ivy.h @@ -95,7 +95,8 @@ protected: ivy::string ready_message; public: - + + void SetDieCallback( IvyDieCallback* die_callback ); void SetBindCallback( IvyBindingCallback* bind_callback ); void SetFilter( int argc, const char **argv ); void SendDieMsg( IvyApplication *app ); diff --git a/Ivy/IvyApplication.cxx b/Ivy/IvyApplication.cxx index 7b3cd2f..8ba70f6 100644 --- a/Ivy/IvyApplication.cxx +++ b/Ivy/IvyApplication.cxx @@ -5,7 +5,7 @@ #include "IvyApplication.h" #include "IvyBinding.h" - +#include "IvySynchroWnd.h" //#define IVY_DEBUG @@ -272,11 +272,9 @@ void IvyApplication::OnReceive(char * line) #ifdef IVY_DEBUG TRACE("Die Message id=%d msg='%s'\n", id, arg); #endif //IVY_DEBUG - if ( bus->CallDieCallback( this, id, arg ) ) { - PostMessage( NULL, WM_CLOSE, 0, 0); - exit(-1); + IvySynchroWnd::PostQuit(); } break; case Ping: diff --git a/Ivy/IvyApplication.h b/Ivy/IvyApplication.h index cd9b123..375ac25 100644 --- a/Ivy/IvyApplication.h +++ b/Ivy/IvyApplication.h @@ -5,10 +5,11 @@ // #include "BufferedSocket.h" #include "Ivy.h" -#include "IvyBinding.h" +//#include "IvyBinding.h" ///////////////////////////////////////////////////////////////////////////// // IvyApplication command target +class IvyBinding; class IvyApplication : public CBufferedSocket { diff --git a/Ivy/IvySynchroWnd.cxx b/Ivy/IvySynchroWnd.cxx index 2c84ad5..374538c 100644 --- a/Ivy/IvySynchroWnd.cxx +++ b/Ivy/IvySynchroWnd.cxx @@ -2,15 +2,10 @@ // #include "IvyStdAfx.h" - - #include "IvySynchroWnd.h" - #define WM_IVY_CB WM_USER + 1001 - -IvySynchroWnd* IvySynchronousCallback::m_synchro = NULL; IvySynchroWnd* IvySynchroWnd::m_synchro = NULL; ///////////////////////////////////////////////////////////////////////////// @@ -19,22 +14,22 @@ IvySynchroWnd* IvySynchroWnd::m_synchro = NULL; IvySynchroWnd::IvySynchroWnd() { m_hWnd = NULL; - m_synchro = this; + //m_synchro = this; WNDCLASS wc; // Fill in the window class structure with parameters // that describe the main window. - wc.style = 0; // noredraw if size changes - wc.lpfnWndProc = WindowProc; // points to window procedure - wc.cbClsExtra = 0; // no extra class memory - wc.cbWndExtra = 0; // no extra window memory - wc.hInstance = 0; // handle to instance - wc.hIcon = NULL; // predefined app. icon - wc.hCursor = NULL; // predefined arrow - wc.hbrBackground = 0; // white background brush - wc.lpszMenuName = NULL; // no menu + wc.style = 0; // noredraw if size changes + wc.lpfnWndProc = WindowProc; // points to window procedure + wc.cbClsExtra = 0; // no extra class memory + wc.cbWndExtra = 0; // no extra window memory + wc.hInstance = 0; // handle to instance + wc.hIcon = NULL; // predefined app. icon + wc.hCursor = NULL; // predefined arrow + wc.hbrBackground = 0; // white background brush + wc.lpszMenuName = NULL; // no menu wc.lpszClassName = TEXT("IvySynchroClass"); // name of window class // Register the window class. @@ -59,7 +54,16 @@ IvySynchroWnd::IvySynchroWnd() IvySynchroWnd::~IvySynchroWnd() { } - +void IvySynchroWnd::Init(bool synchronous) +{ + if ( synchronous && m_synchro == NULL ) + m_synchro = new IvySynchroWnd(); +} +void IvySynchroWnd::Finish() +{ + if ( m_synchro == NULL ) + delete m_synchro; +} LRESULT CALLBACK IvySynchroWnd::WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { @@ -102,12 +106,57 @@ LRESULT IvySynchroWnd::OnIvyCB(WPARAM wParam, LPARAM lParam) } void IvySynchroWnd::PostIvyCB( IvySynchronousCallback *cb ) { + BOOL doPost = FALSE; EnterCriticalSection( &m_CritSection ); - if ( m_synchro->callbacklist.empty() ) - ASSERT(PostMessage(m_synchro->m_hWnd, WM_IVY_CB, 0, 0 )); - m_synchro->callbacklist.push_back( cb ); + doPost = callbacklist.empty(); + callbacklist.push_back( cb ); LeaveCriticalSection( &m_CritSection ); + if ( doPost ) + ASSERT(PostMessage(m_hWnd, WM_IVY_CB, 0, 0 )); + // give a change to process messages + SwitchToThread(); + } +BOOL CALLBACK MyQuitWindows( HWND hwnd, LPARAM lParam ) +{ + BOOL *posted = (BOOL*)lParam; + DWORD dwTheardId; + DWORD pid; + DWORD mypid = GetCurrentProcessId(); + dwTheardId = ::GetWindowThreadProcessId( hwnd,&pid); + if ( pid == mypid ) + { + // give a change to User for closing application safely, save file etc... + *posted = PostMessage( hwnd, WM_CLOSE, 0,0 ); + } + return TRUE; +} + +void IvySynchroWnd::PostQuit() +{ + BOOL posted = FALSE; + BOOL canceled = FALSE; + EnumWindows(MyQuitWindows, (LPARAM)&posted ); + // give a change to process messages + if ( !SwitchToThread() ) + { + Sleep( 200 ); + } + if ( !posted ) + { + PostQuitMessage( -1 ); + // give a change to process messages + if ( !SwitchToThread() ) + { + Sleep( 100 ); + } + // ultimate close + exit(-1); + } +} + + + //exit(-1); // // IvySynchronousMessageCallback::IvySynchronousMessageCallback( IvyMessageCallback *cb ) @@ -128,7 +177,6 @@ IvySynchronousMessageCallback::~IvySynchronousMessageCallback() } void IvySynchronousMessageCallback::OnMessage(IvyApplication *app, int argc, const char **argv ) { - static int msg_count = 0; // duplicate on the Message Queue IvySynchronousMessageCallback *param = new IvySynchronousMessageCallback(target); param->app = app; @@ -142,12 +190,17 @@ void IvySynchronousMessageCallback::OnMessage(IvyApplication *app, int argc, con param->argv[i] = _strdup( argv[i]); #endif } -// TRACE( "IvySynchronousMessageCallback::OnMessage msg count %d\n",wParam); - m_synchro->PostIvyCB( param ); +#ifdef IVY_DEBUG + TRACE( "IvySynchronousMessageCallback::OnMessage Push callback %s argc=%d\n",typeid( *target).name(), argc); +#endif + IvySynchroWnd::m_synchro->PostIvyCB( param ); } void IvySynchronousMessageCallback::CallCallback() { +#ifdef IVY_DEBUG + TRACE( "IvySynchronousMessageCallback::OnMessage Real callback %s argc=%d\n",typeid( *target).name(), argc); +#endif target->OnMessage( app, argc, (const char **) argv ); for( int i = 0; i < argc ; i++ ) delete argv[i]; @@ -165,7 +218,7 @@ void IvySynchronousApplicationCallback::OnApplicationConnected( IvyApplication * IvySynchronousApplicationCallback *param = new IvySynchronousApplicationCallback(target); param->type = CONNECTED_CB; param->app = app; - m_synchro->PostIvyCB( param ); + IvySynchroWnd::m_synchro->PostIvyCB( param ); } void IvySynchronousApplicationCallback::OnApplicationDisconnected( IvyApplication *app) @@ -174,7 +227,7 @@ void IvySynchronousApplicationCallback::OnApplicationDisconnected( IvyApplicati IvySynchronousApplicationCallback *param = new IvySynchronousApplicationCallback(target); param->type = DISCONNECTED_CB; param->app = app; - m_synchro->PostIvyCB( param ); + IvySynchroWnd::m_synchro->PostIvyCB( param ); } void IvySynchronousApplicationCallback::CallCallback() { @@ -203,7 +256,7 @@ void IvySynchronousBindingCallback::OnAddBind( IvyApplication *app, int id, cons param->app = app; param->id = id; param->regexp = _strdup(regexp); - m_synchro->PostIvyCB( param ); + IvySynchroWnd::m_synchro->PostIvyCB( param ); } void IvySynchronousBindingCallback::OnRemoveBind( IvyApplication *app, int id, const char *regexp) @@ -214,7 +267,7 @@ void IvySynchronousBindingCallback::OnRemoveBind( IvyApplication *app, int id, param->app = app; param->id = id; param->regexp = _strdup(regexp); - m_synchro->PostIvyCB( param ); + IvySynchroWnd::m_synchro->PostIvyCB( param ); } void IvySynchronousBindingCallback::OnFilterBind( IvyApplication *app, int id, const char *regexp) { @@ -224,7 +277,7 @@ void IvySynchronousBindingCallback::OnFilterBind( IvyApplication *app, int id, param->app = app; param->id = id; param->regexp = _strdup(regexp); - m_synchro->PostIvyCB( param ); + IvySynchroWnd::m_synchro->PostIvyCB( param ); } void IvySynchronousBindingCallback::CallCallback() { @@ -244,4 +297,36 @@ void IvySynchronousBindingCallback::CallCallback() } } delete regexp; +} + +IvySynchronousDieCallback::IvySynchronousDieCallback( IvyDieCallback *cb ) +{ + target = cb; +} +bool IvySynchronousDieCallback::OnDie(IvyApplication *app, int id, const char *arg ) +{ + static int msg_count = 0; + // duplicate on the Message Queue + IvySynchronousDieCallback *param = new IvySynchronousDieCallback(target); + param->app = app; + param->id = id; + +#ifdef UNDER_CE + param->arg = _strdup( argv); +#else + param->arg = _strdup( arg); +#endif + +// TRACE( "IvySynchronousMessageCallback::OnMessage msg count %d\n",wParam); + IvySynchroWnd::m_synchro->PostIvyCB( param ); + return FALSE; // do not exit now wait for Quit on MessageQueue +} +void IvySynchronousDieCallback::CallCallback() +{ + // duplicate on the Message Queue + if ( !target ) return; + if ( target->OnDie ( app, id, arg ) ) + { + IvySynchroWnd::m_synchro->PostQuit(); + } } \ No newline at end of file diff --git a/Ivy/IvySynchroWnd.h b/Ivy/IvySynchroWnd.h index ffa44ba..b90e2a0 100644 --- a/Ivy/IvySynchroWnd.h +++ b/Ivy/IvySynchroWnd.h @@ -13,7 +13,7 @@ class IvySynchroWnd { // Construction public: - IvySynchroWnd(); + // Attributes public: @@ -21,7 +21,10 @@ public: // Operations public: + static void PostQuit(); void PostIvyCB( IvySynchronousCallback *cb ); + static void Init(bool synchronous); + static void Finish(); // Implementation public: @@ -30,8 +33,9 @@ protected: HWND m_hWnd; protected: + IvySynchroWnd(); // Unique instance of this class - static IvySynchroWnd *m_synchro; + static IvySynchroWnd *m_synchro; // for synchronous Windowed // Generated message map functions static LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window @@ -49,13 +53,14 @@ protected: LRESULT OnIvyCB(WPARAM wParam, LPARAM lParam); friend class IvySynchronousMessageCallback; friend class IvySynchronousApplicationCallback; + friend class IvySynchronousBindingCallback; + friend class IvySynchronousDieCallback; }; class IvySynchronousCallback { protected: IvyApplication *app; - static IvySynchroWnd *m_synchro; friend class Ivy; public: virtual void CallCallback() = 0; @@ -107,3 +112,18 @@ protected: friend class IvySynchroWnd; }; + +class IvySynchronousDieCallback: public IvySynchronousCallback, public IvyDieCallback +{ +public: + IvySynchronousDieCallback( IvyDieCallback *cb ); + virtual void CallCallback(); + virtual bool OnDie (IvyApplication *app, int id, const char *arg ); +protected: + IvyDieCallback *target; + + int id; + const char *arg; + + friend class IvySynchroWnd; +}; \ No newline at end of file diff --git a/Ivy/ThreadedSocket.cxx b/Ivy/ThreadedSocket.cxx index 99caa80..147cb69 100644 --- a/Ivy/ThreadedSocket.cxx +++ b/Ivy/ThreadedSocket.cxx @@ -110,7 +110,7 @@ SOCKET CThreadedSocket::Accept(CThreadedSocket& rConnectedSocket, else { rConnectedSocket.m_hSocket = hTemp; - rConnectedSocket.send_count = CreateSemaphore( NULL, 0, 100, NULL); // unnamed semaphore + rConnectedSocket.send_count = CreateSemaphore( NULL, 0, 200, NULL); // unnamed semaphore if (rConnectedSocket.send_count == NULL) return SOCKET_ERROR; -- cgit v1.1