Applied patch [ 600051 ] DDE and TCP improvements and fixes
By Michael Fielding As discussed on wx-dev. some fixes and improvements for Interprocess Communication (IPC), using DDE and TCP. 1. DDE buffers were using a global buffer 2. TCP buffers were allocated each time needed, and Request would have caused memory leaks had it been used. Fixed these both by using a self-resizing buffer in wxConnectionBase. Changed samples and docs to reflect the improved (but backward compatible) internal buffer management. wxConnectionBase could (in future) use wxMemoryBuffer. 3. IPC sample had trouble closing, causing crash, when closing server using window X button. Because it was (effectively) trying to delete a window in OnExit, when that window was already destroyed. Fixed by making IPCDialog and MyConnection remember if they'd destroyed each other. It's not elegant, but either the connection or the window could be deleted first. 4. Docs for wxDDE... and wxTCP... duplicated eachother, supposed to have same API. Some parts unclear. Patch removes dde and tcp-specific files (including from tipc.tex and classes.tex), and explains how ipc.h selects for you which one to use based on platform. Some other misc clarifications. 6. Client sample was suffering apparent memory leak because of not deleting connection object, and had a hack in there to do that. In fact this was due to the derived OnDisconnect not deleting itself, as it does in base class. Mentioned need to do it in docs, fixed sample so that it does. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@16907 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -133,8 +133,6 @@ static wxList wxAtomTable(wxKEY_STRING);
|
||||
static wxList wxDDEClientObjects;
|
||||
static wxList wxDDEServerObjects;
|
||||
|
||||
char *DDEDefaultIPCBuffer = NULL;
|
||||
int DDEDefaultIPCBufferSize = 0;
|
||||
static bool DDEInitialized = FALSE;
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -198,8 +196,6 @@ void wxDDECleanUp()
|
||||
DdeUninitialize(DDEIdInst);
|
||||
DDEIdInst = 0;
|
||||
}
|
||||
|
||||
delete [] DDEDefaultIPCBuffer;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
@@ -314,6 +310,7 @@ wxDDEServer::~wxDDEServer()
|
||||
{
|
||||
wxDDEConnection *connection = (wxDDEConnection *)node->Data();
|
||||
wxNode *next = node->Next();
|
||||
connection->SetConnected(false);
|
||||
connection->OnDisconnect(); // May delete the node implicitly
|
||||
node = next;
|
||||
}
|
||||
@@ -463,20 +460,8 @@ bool wxDDEClient::DeleteConnection(WXHCONV conv)
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
wxDDEConnection::wxDDEConnection(char *buffer, int size)
|
||||
: wxConnectionBase(buffer, size)
|
||||
{
|
||||
if (buffer == NULL)
|
||||
{
|
||||
if (DDEDefaultIPCBuffer == NULL)
|
||||
DDEDefaultIPCBuffer = new char[DDEDefaultIPCBufferSize];
|
||||
m_bufPtr = DDEDefaultIPCBuffer;
|
||||
m_bufSize = DDEDefaultIPCBufferSize;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_bufPtr = buffer;
|
||||
m_bufSize = size;
|
||||
}
|
||||
|
||||
m_client = NULL;
|
||||
m_server = NULL;
|
||||
|
||||
@@ -485,20 +470,17 @@ wxDDEConnection::wxDDEConnection(char *buffer, int size)
|
||||
}
|
||||
|
||||
wxDDEConnection::wxDDEConnection()
|
||||
: wxConnectionBase()
|
||||
{
|
||||
m_hConv = 0;
|
||||
m_sendingData = NULL;
|
||||
m_server = NULL;
|
||||
m_client = NULL;
|
||||
if (DDEDefaultIPCBuffer == NULL)
|
||||
DDEDefaultIPCBuffer = new char[DDEDefaultIPCBufferSize];
|
||||
|
||||
m_bufPtr = DDEDefaultIPCBuffer;
|
||||
m_bufSize = DDEDefaultIPCBufferSize;
|
||||
}
|
||||
|
||||
wxDDEConnection::~wxDDEConnection()
|
||||
{
|
||||
Disconnect();
|
||||
if (m_server)
|
||||
m_server->GetConnections().DeleteObject(this);
|
||||
else
|
||||
@@ -508,6 +490,9 @@ wxDDEConnection::~wxDDEConnection()
|
||||
// Calls that CLIENT can make
|
||||
bool wxDDEConnection::Disconnect()
|
||||
{
|
||||
if ( !GetConnected() )
|
||||
return true;
|
||||
|
||||
DDEDeleteConnection(GetHConv());
|
||||
|
||||
bool ok = DdeDisconnect(GetHConv()) != 0;
|
||||
@@ -516,6 +501,8 @@ bool wxDDEConnection::Disconnect()
|
||||
DDELogError(_T("Failed to disconnect from DDE server gracefully"));
|
||||
}
|
||||
|
||||
SetConnected( false ); // so we don't try and disconnect again
|
||||
|
||||
return ok;
|
||||
}
|
||||
|
||||
@@ -546,6 +533,7 @@ bool wxDDEConnection::Execute(const wxChar *data, int size, wxIPCFormat format)
|
||||
char *wxDDEConnection::Request(const wxString& item, int *size, wxIPCFormat format)
|
||||
{
|
||||
DWORD result;
|
||||
|
||||
HSZ atom = DDEGetAtom(item);
|
||||
|
||||
HDDEDATA returned_data = DdeClientTransaction(NULL, 0,
|
||||
@@ -561,14 +549,19 @@ char *wxDDEConnection::Request(const wxString& item, int *size, wxIPCFormat form
|
||||
return NULL;
|
||||
}
|
||||
|
||||
DWORD len = DdeGetData(returned_data, (LPBYTE)m_bufPtr, m_bufSize, 0);
|
||||
DWORD len = DdeGetData(returned_data, NULL, 0, 0);
|
||||
|
||||
wxChar *data = GetBufferAtLeast( len );
|
||||
wxASSERT_MSG(data != NULL,
|
||||
_T("Buffer too small in wxDDEConnection::Request") );
|
||||
DdeGetData(returned_data, (LPBYTE)data, len, 0);
|
||||
|
||||
DdeFreeDataHandle(returned_data);
|
||||
|
||||
if (size)
|
||||
*size = (int)len;
|
||||
|
||||
return m_bufPtr;
|
||||
return data;
|
||||
}
|
||||
|
||||
bool wxDDEConnection::Poke(const wxString& item, wxChar *data, int size, wxIPCFormat format)
|
||||
@@ -645,7 +638,7 @@ bool wxDDEConnection::Advise(const wxString& item,
|
||||
|
||||
HSZ item_atom = DDEGetAtom(item);
|
||||
HSZ topic_atom = DDEGetAtom(m_topicName);
|
||||
m_sendingData = data;
|
||||
m_sendingData = data; // mrf: potential for scope problems here?
|
||||
m_dataSize = size;
|
||||
m_dataType = format;
|
||||
|
||||
@@ -718,10 +711,14 @@ _DDECallback(WORD wType,
|
||||
case XTYP_DISCONNECT:
|
||||
{
|
||||
wxDDEConnection *connection = DDEFindConnection(hConv);
|
||||
if (connection && connection->OnDisconnect())
|
||||
if (connection)
|
||||
{
|
||||
DDEDeleteConnection(hConv); // Delete mapping: hConv => connection
|
||||
return (DDERETURN)(DWORD)TRUE;
|
||||
connection->SetConnected( false );
|
||||
if (connection->OnDisconnect())
|
||||
{
|
||||
DDEDeleteConnection(hConv); // Delete mapping: hConv => connection
|
||||
return (DDERETURN)(DWORD)TRUE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -732,13 +729,18 @@ _DDECallback(WORD wType,
|
||||
|
||||
if (connection)
|
||||
{
|
||||
DWORD len = DdeGetData(hData,
|
||||
(LPBYTE)connection->m_bufPtr,
|
||||
connection->m_bufSize,
|
||||
0);
|
||||
DWORD len = DdeGetData(hData, NULL, 0, 0);
|
||||
|
||||
wxChar *data = connection->GetBufferAtLeast( len );
|
||||
wxASSERT_MSG(data != NULL,
|
||||
_T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
|
||||
|
||||
DdeGetData(hData, (LPBYTE)data, len, 0);
|
||||
|
||||
DdeFreeDataHandle(hData);
|
||||
|
||||
if ( connection->OnExecute(connection->m_topicName,
|
||||
connection->m_bufPtr,
|
||||
data,
|
||||
(int)len,
|
||||
(wxIPCFormat) wFmt) )
|
||||
{
|
||||
@@ -788,15 +790,19 @@ _DDECallback(WORD wType,
|
||||
{
|
||||
wxString item_name = DDEStringFromAtom(hsz2);
|
||||
|
||||
DWORD len = DdeGetData(hData,
|
||||
(LPBYTE)connection->m_bufPtr,
|
||||
connection->m_bufSize,
|
||||
0);
|
||||
DWORD len = DdeGetData(hData, NULL, 0, 0);
|
||||
|
||||
wxChar *data = connection->GetBufferAtLeast( len );
|
||||
wxASSERT_MSG(data != NULL,
|
||||
_T("Buffer too small in _DDECallback (XTYP_EXECUTE)") );
|
||||
|
||||
DdeGetData(hData, (LPBYTE)data, len, 0);
|
||||
|
||||
DdeFreeDataHandle(hData);
|
||||
|
||||
connection->OnPoke(connection->m_topicName,
|
||||
item_name,
|
||||
(wxChar*)connection->m_bufPtr,
|
||||
data,
|
||||
(int)len,
|
||||
(wxIPCFormat) wFmt);
|
||||
|
||||
@@ -871,14 +877,18 @@ _DDECallback(WORD wType,
|
||||
{
|
||||
wxString item_name = DDEStringFromAtom(hsz2);
|
||||
|
||||
DWORD len = DdeGetData(hData,
|
||||
(LPBYTE)connection->m_bufPtr,
|
||||
connection->m_bufSize,
|
||||
0);
|
||||
DWORD len = DdeGetData(hData, NULL, 0, 0);
|
||||
|
||||
wxChar *data = connection->GetBufferAtLeast( len );
|
||||
wxASSERT_MSG(data != NULL,
|
||||
_T("Buffer too small in _DDECallback (XTYP_ADVDATA)") );
|
||||
|
||||
DdeGetData(hData, (LPBYTE)data, len, 0);
|
||||
|
||||
DdeFreeDataHandle(hData);
|
||||
if ( connection->OnAdvise(connection->m_topicName,
|
||||
item_name,
|
||||
connection->m_bufPtr,
|
||||
data,
|
||||
(int)len,
|
||||
(wxIPCFormat) wFmt) )
|
||||
{
|
||||
|
Reference in New Issue
Block a user