- Uses wxSocketBase::Destroy() now.

- Fixed a memory leak when connection requests failed.
- Maybe some other (small) bugfix as well.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@6574 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Guillermo Rodriguez Garcia
2000-03-10 00:21:25 +00:00
parent 557e701169
commit 3adb47a937

View File

@@ -27,7 +27,7 @@
#include "wx/defs.h" #include "wx/defs.h"
#endif #endif
#if wxUSE_SOCKETS #if wxUSE_SOCKETS && wxUSE_IPC
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@@ -124,12 +124,7 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
if (connection) if (connection)
{ {
if (!connection->IsKindOf(CLASSINFO(wxTCPConnection))) if (connection->IsKindOf(CLASSINFO(wxTCPConnection)))
{
delete connection;
// and fall through to delete everything else
}
else
{ {
connection->m_topic = topic; connection->m_topic = topic;
connection->m_sock = client; connection->m_sock = client;
@@ -142,15 +137,21 @@ wxConnectionBase *wxTCPClient::MakeConnection (const wxString& host,
client->Notify(TRUE); client->Notify(TRUE);
return connection; return connection;
} }
else
{
delete connection;
// and fall through to delete everything else
}
} }
} }
} }
// something went wrong // Something went wrong, delete everything
delete data_is; delete data_is;
delete data_os; delete data_os;
delete stream; delete stream;
delete client; client->Destroy();
return NULL; return NULL;
} }
@@ -211,10 +212,11 @@ wxTCPConnection::wxTCPConnection(char * WXUNUSED(buffer), int WXUNUSED(size))
wxTCPConnection::~wxTCPConnection () wxTCPConnection::~wxTCPConnection ()
{ {
wxDELETE(m_sock);
wxDELETE(m_codeci); wxDELETE(m_codeci);
wxDELETE(m_codeco); wxDELETE(m_codeco);
wxDELETE(m_sockstrm); wxDELETE(m_sockstrm);
if (m_sock) m_sock->Destroy();
} }
void wxTCPConnection::Compress(bool WXUNUSED(on)) void wxTCPConnection::Compress(bool WXUNUSED(on))
@@ -227,6 +229,7 @@ bool wxTCPConnection::Disconnect ()
{ {
// Send the the disconnect message to the peer. // Send the the disconnect message to the peer.
m_codeco->Write8(IPC_DISCONNECT); m_codeco->Write8(IPC_DISCONNECT);
m_sock->Callback(NULL);
m_sock->Close(); m_sock->Close();
return TRUE; return TRUE;
@@ -354,7 +357,8 @@ bool wxTCPConnection::Advise (const wxString& item,
return TRUE; return TRUE;
} }
void Client_OnRequest(wxSocketBase& sock, wxSocketNotify evt, void Client_OnRequest(wxSocketBase& sock,
wxSocketNotify evt,
char *cdata) char *cdata)
{ {
int msg = 0; int msg = 0;
@@ -368,6 +372,7 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketNotify evt,
// The socket handler signals us that we lost the connection: destroy all. // The socket handler signals us that we lost the connection: destroy all.
if (evt == wxSOCKET_LOST) if (evt == wxSOCKET_LOST)
{ {
sock.Callback(NULL);
sock.Close(); sock.Close();
connection->OnDisconnect(); connection->OnDisconnect();
return; return;
@@ -483,6 +488,8 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketNotify evt,
} }
case IPC_DISCONNECT: case IPC_DISCONNECT:
{ {
wxLogDebug("IPC_DISCONNECT");
sock.Callback(NULL);
sock.Close(); sock.Close();
connection->OnDisconnect(); connection->OnDisconnect();
break; break;
@@ -494,7 +501,8 @@ void Client_OnRequest(wxSocketBase& sock, wxSocketNotify evt,
} }
void Server_OnRequest(wxSocketServer& server, void Server_OnRequest(wxSocketServer& server,
wxSocketNotify evt, char *cdata) wxSocketNotify evt,
char *cdata)
{ {
wxTCPServer *ipcserv = (wxTCPServer *)cdata; wxTCPServer *ipcserv = (wxTCPServer *)cdata;
wxSocketStream *stream; wxSocketStream *stream;
@@ -504,10 +512,13 @@ void Server_OnRequest(wxSocketServer& server,
if (evt != wxSOCKET_CONNECTION) if (evt != wxSOCKET_CONNECTION)
return; return;
/* Accept the connection, getting a new socket */ // Accept the connection, getting a new socket
wxSocketBase *sock = server.Accept(); wxSocketBase *sock = server.Accept();
if (!sock->Ok()) if (!sock->Ok())
{
sock->Destroy();
return; return;
}
stream = new wxSocketStream(*sock); stream = new wxSocketStream(*sock);
codeci = new wxDataInputStream(*stream); codeci = new wxDataInputStream(*stream);
@@ -521,17 +532,13 @@ void Server_OnRequest(wxSocketServer& server,
wxString topic_name; wxString topic_name;
topic_name = codeci->ReadString(); topic_name = codeci->ReadString();
/* Register new socket with the notifier */
wxTCPConnection *new_connection = wxTCPConnection *new_connection =
(wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name); (wxTCPConnection *)ipcserv->OnAcceptConnection (topic_name);
if (new_connection) if (new_connection)
{ {
if (!new_connection->IsKindOf(CLASSINFO(wxTCPConnection))) if (new_connection->IsKindOf(CLASSINFO(wxTCPConnection)))
{ {
delete new_connection;
codeco->Write8(IPC_FAIL);
return;
}
// Acknowledge success // Acknowledge success
codeco->Write8(IPC_CONNECT); codeco->Write8(IPC_CONNECT);
new_connection->m_topic = topic_name; new_connection->m_topic = topic_name;
@@ -543,14 +550,24 @@ void Server_OnRequest(wxSocketServer& server,
sock->CallbackData((char *)new_connection); sock->CallbackData((char *)new_connection);
sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG); sock->SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_LOST_FLAG);
sock->Notify(TRUE); sock->Notify(TRUE);
return;
} }
else else
{ {
// Send failure message delete new_connection;
// and fall through to delete everything else
}
}
}
// Something went wrong, send failure message and delete everything
codeco->Write8(IPC_FAIL); codeco->Write8(IPC_FAIL);
}
} delete codeco;
delete codeci;
delete stream;
sock->Destroy();
} }
#endif #endif
// wxUSE_SOCKETS // wxUSE_SOCKETS && wxUSE_IPC