Fixed a memory leak in wxTCPServer

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/branches/WX_2_2_BRANCH@6894 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Guillermo Rodriguez Garcia
2000-03-21 20:34:06 +00:00
parent c26fc0be88
commit ad269e4c7c
3 changed files with 71 additions and 34 deletions

View File

@@ -48,7 +48,8 @@ class WXDLLEXPORT wxClientBase;
class WXDLLEXPORT wxConnectionBase: public wxObject class WXDLLEXPORT wxConnectionBase: public wxObject
{ {
DECLARE_CLASS(wxConnectionBase) DECLARE_CLASS(wxConnectionBase)
public:
public:
inline wxConnectionBase(void) {} inline wxConnectionBase(void) {}
inline ~wxConnectionBase(void) {} inline ~wxConnectionBase(void) {}
@@ -102,8 +103,7 @@ class WXDLLEXPORT wxConnectionBase: public wxObject
wxIPCFormat WXUNUSED(format) ) wxIPCFormat WXUNUSED(format) )
{ return FALSE; }; { return FALSE; };
// Callbacks to BOTH // Callbacks to BOTH - override at will
// Default behaviour is to delete connection and return TRUE // Default behaviour is to delete connection and return TRUE
virtual bool OnDisconnect(void) = 0; virtual bool OnDisconnect(void) = 0;
}; };
@@ -111,28 +111,35 @@ class WXDLLEXPORT wxConnectionBase: public wxObject
class WXDLLEXPORT wxServerBase: public wxObject class WXDLLEXPORT wxServerBase: public wxObject
{ {
DECLARE_CLASS(wxServerBase) DECLARE_CLASS(wxServerBase)
public:
public:
inline wxServerBase(void) {} inline wxServerBase(void) {}
inline ~wxServerBase(void) {}; inline ~wxServerBase(void) {};
virtual bool Create(const wxString& serverName) = 0; // Returns FALSE if can't create server (e.g. port
// number is already in use)
virtual wxConnectionBase *OnAcceptConnection(const wxString& topic) = 0;
// Returns FALSE on error (e.g. port number is already in use)
virtual bool Create(const wxString& serverName) = 0;
// Callbacks to SERVER - override at will
virtual wxConnectionBase *OnAcceptConnection(const wxString& topic) = 0;
}; };
class WXDLLEXPORT wxClientBase: public wxObject class WXDLLEXPORT wxClientBase: public wxObject
{ {
DECLARE_CLASS(wxClientBase) DECLARE_CLASS(wxClientBase)
public:
public:
inline wxClientBase(void) {}; inline wxClientBase(void) {};
inline ~wxClientBase(void) {}; inline ~wxClientBase(void) {};
virtual bool ValidHost(const wxString& host) = 0;
virtual wxConnectionBase *MakeConnection(const wxString& host, const wxString& server, const wxString& topic) = 0;
// Call this to make a connection.
// Returns NULL if cannot.
virtual wxConnectionBase *OnMakeConnection(void) = 0; // Tailor this to return own connection.
virtual bool ValidHost(const wxString& host) = 0;
// Call this to make a connection. Returns NULL if cannot.
virtual wxConnectionBase *MakeConnection(const wxString& host,
const wxString& server,
const wxString& topic) = 0;
// Callbacks to CLIENT - override at will
virtual wxConnectionBase *OnMakeConnection(void) = 0;
}; };
#endif #endif

View File

@@ -59,6 +59,7 @@ class WXDLLEXPORT wxTCPClient;
class WXDLLEXPORT wxTCPConnection: public wxConnectionBase class WXDLLEXPORT wxTCPConnection: public wxConnectionBase
{ {
DECLARE_DYNAMIC_CLASS(wxTCPConnection) DECLARE_DYNAMIC_CLASS(wxTCPConnection)
public: public:
wxTCPConnection(char *buffer, int size); wxTCPConnection(char *buffer, int size);
wxTCPConnection(); wxTCPConnection();
@@ -77,18 +78,19 @@ public:
// Calls that both can make // Calls that both can make
virtual bool Disconnect(void); virtual bool Disconnect(void);
// Callbacks to BOTH - override at will
// Default behaviour is to delete connection and return TRUE // Default behaviour is to delete connection and return TRUE
virtual bool OnDisconnect(void) { delete this; return TRUE; } virtual bool OnDisconnect(void) { delete this; return TRUE; }
// To enable the compressor // To enable the compressor (NOTE: not implemented!)
void Compress(bool on); void Compress(bool on);
protected: protected:
wxSocketBase *m_sock; wxSocketBase *m_sock;
wxSocketStream *m_sockstrm; wxSocketStream *m_sockstrm;
wxDataInputStream *m_codeci; wxDataInputStream *m_codeci;
wxDataOutputStream *m_codeco; wxDataOutputStream *m_codeco;
wxString m_topic; wxString m_topic;
friend class wxTCPServer; friend class wxTCPServer;
friend class wxTCPClient; friend class wxTCPClient;
@@ -112,9 +114,14 @@ public:
wxTCPServer(); wxTCPServer();
virtual ~wxTCPServer(); virtual ~wxTCPServer();
// Returns FALSE if can't create server (e.g. port number is already in use) // Returns FALSE on error (e.g. port number is already in use)
virtual bool Create(const wxString& server_name); virtual bool Create(const wxString& serverName);
// Callbacks to SERVER - override at will
virtual wxConnectionBase *OnAcceptConnection(const wxString& topic); virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
protected:
wxSocketServer *m_server;
}; };
class wxTCPClient: public wxClientBase class wxTCPClient: public wxClientBase
@@ -126,13 +133,13 @@ public:
virtual ~wxTCPClient(); virtual ~wxTCPClient();
virtual bool ValidHost(const wxString& host); virtual bool ValidHost(const wxString& host);
// Call this to make a connection.
// Returns NULL if cannot. // Call this to make a connection. Returns NULL if cannot.
virtual wxConnectionBase *MakeConnection(const wxString& host, virtual wxConnectionBase *MakeConnection(const wxString& host,
const wxString& server, const wxString& server,
const wxString& topic); const wxString& topic);
// Tailor this to return own connection. // Callbacks to CLIENT - override at will
virtual wxConnectionBase *OnMakeConnection(); virtual wxConnectionBase *OnMakeConnection();
}; };

View File

@@ -200,28 +200,51 @@ wxConnectionBase *wxTCPClient::OnMakeConnection()
wxTCPServer::wxTCPServer () : wxServerBase() wxTCPServer::wxTCPServer () : wxServerBase()
{ {
m_server = NULL;
} }
bool wxTCPServer::Create(const wxString& server_name) bool wxTCPServer::Create(const wxString& serverName)
{ {
wxSocketServer *server; // Destroy previous server, if any
if (m_server)
{
m_server->SetClientData(NULL);
m_server->Destroy();
m_server = NULL;
}
// wxIPV4address defaults to INADDR_ANY:0 // wxIPV4address defaults to INADDR_ANY:0
wxIPV4address addr; wxIPV4address addr;
addr.Service(server_name); addr.Service(serverName);
// Create a socket listening on specified port // Create a socket listening on the specified port
server = new wxSocketServer(addr, SCKIPC_FLAGS); m_server = new wxSocketServer(addr, SCKIPC_FLAGS);
server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID);
server->SetClientData(this);
server->SetNotify(wxSOCKET_CONNECTION_FLAG);
server->Notify(TRUE);
return TRUE; if (m_server->Ok())
{
m_server->SetEventHandler(*gs_handler, _SERVER_ONREQUEST_ID);
m_server->SetClientData(this);
m_server->SetNotify(wxSOCKET_CONNECTION_FLAG);
m_server->Notify(TRUE);
return TRUE;
}
else
{
m_server->Destroy();
m_server = NULL;
return FALSE;
}
} }
wxTCPServer::~wxTCPServer() wxTCPServer::~wxTCPServer()
{ {
if (m_server)
{
m_server->SetClientData(NULL);
m_server->Destroy();
}
} }
wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) ) wxConnectionBase *wxTCPServer::OnAcceptConnection( const wxString& WXUNUSED(topic) )
@@ -555,7 +578,7 @@ void wxTCPEventHandler::Client_OnRequest(wxSocketEvent &event)
void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event) void wxTCPEventHandler::Server_OnRequest(wxSocketEvent &event)
{ {
wxSocketServer *server = (wxSocketServer *) event.GetSocket(); wxSocketServer *server = (wxSocketServer *) event.GetSocket();
wxTCPServer *ipcserv = (wxTCPServer *) event.GetClientData(); wxTCPServer *ipcserv = (wxTCPServer *) server->GetClientData();
// This socket is being deleted; skip this event // This socket is being deleted; skip this event
if (!ipcserv) if (!ipcserv)