wxWidgets/samples/ipc/server.cpp
Julian Smart f010ad4812 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
2002-09-01 14:48:16 +00:00

252 lines
7.0 KiB
C++

/////////////////////////////////////////////////////////////////////////////
// Name: server.cpp
// Purpose: IPC sample: server
// Author: Julian Smart
// Modified by:
// Created: 25/01/99
// RCS-ID: $Id$
// Copyright: (c) Julian Smart
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
// Settings common to both executables: determines whether
// we're using TCP/IP or real DDE.
#include "ddesetup.h"
#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
#include "mondrian.xpm"
#endif
#include "server.h"
// ----------------------------------------------------------------------------
// wxWin macros
// ----------------------------------------------------------------------------
IMPLEMENT_APP(MyApp)
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU (SERVER_EXIT, MyFrame::OnExit)
EVT_LISTBOX(SERVER_LISTBOX, MyFrame::OnListBoxClick)
END_EVENT_TABLE()
BEGIN_EVENT_TABLE(IPCDialogBox, wxDialog)
EVT_BUTTON(SERVER_QUIT_BUTTON, IPCDialogBox::OnQuit)
END_EVENT_TABLE()
// ----------------------------------------------------------------------------
// global variables
// ----------------------------------------------------------------------------
MyConnection *the_connection = NULL;
// ============================================================================
// implementation
// ============================================================================
// ----------------------------------------------------------------------------
// MyApp
// ----------------------------------------------------------------------------
bool MyApp::OnInit()
{
// Create the main frame window
(new MyFrame(NULL, "Server"))->Show(TRUE);
// service name (DDE classes) or port number (TCP/IP based classes)
wxString service = IPC_SERVICE;
if (argc > 1)
service = argv[1];
// Create a new server
m_server = new MyServer;
m_server->Create(service);
return TRUE;
}
int MyApp::OnExit()
{
delete m_server;
return 0;
}
// ----------------------------------------------------------------------------
// MyFrame
// ----------------------------------------------------------------------------
// Define my frame constructor
MyFrame::MyFrame(wxFrame *frame, const wxString& title)
: wxFrame(frame, -1, title, wxDefaultPosition, wxSize(350, 250))
{
CreateStatusBar();
// Give it an icon
SetIcon(wxICON(mondrian));
// Make a menubar
wxMenu *file_menu = new wxMenu;
file_menu->Append(SERVER_EXIT, "&Quit\tCtrl-Q");
wxMenuBar *menu_bar = new wxMenuBar;
menu_bar->Append(file_menu, "&File");
// Associate the menu bar with the frame
SetMenuBar(menu_bar);
// Make a listbox
wxListBox *list = new wxListBox(this, SERVER_LISTBOX, wxPoint(5, 5));
list->Append("Apple");
list->Append("Pear");
list->Append("Orange");
list->Append("Banana");
list->Append("Fruit");
}
// Set the client process's listbox to this item
void MyFrame::OnListBoxClick(wxCommandEvent& WXUNUSED(event))
{
wxListBox* listBox = (wxListBox*) FindWindow(SERVER_LISTBOX);
if (listBox)
{
wxString value = listBox->GetStringSelection();
/* Because the_connection only holds one connection, in this sample only
one connection can receive advise messages */
if (the_connection)
{
the_connection->Advise(IPC_ADVISE_NAME, (wxChar *)value.c_str());
}
}
}
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
// ----------------------------------------------------------------------------
// IPCDialogBox
// ----------------------------------------------------------------------------
IPCDialogBox::IPCDialogBox(wxWindow *parent, const wxString& title,
const wxPoint& pos, const wxSize& size,
MyConnection *connection)
: wxDialog(parent, -1, title, pos, size)
{
m_connection = connection;
(void)new wxButton(this, SERVER_QUIT_BUTTON, "Quit this connection",
wxPoint(5, 5));
Fit();
}
IPCDialogBox::~IPCDialogBox( )
{
// wxWindows exit code destroys dialog before destroying the connection in
// OnExit, so make sure connection won't try to delete the dialog later.
if (m_connection)
m_connection->dialog = NULL;
}
void IPCDialogBox::OnQuit(wxCommandEvent& event)
{
m_connection->Disconnect();
delete m_connection;
}
// ----------------------------------------------------------------------------
// MyServer
// ----------------------------------------------------------------------------
wxConnectionBase *MyServer::OnAcceptConnection(const wxString& topic)
{
if ( topic == IPC_TOPIC )
return new MyConnection();
// unknown topic
return NULL;
}
// ----------------------------------------------------------------------------
// MyConnection
// ----------------------------------------------------------------------------
MyConnection::MyConnection()
: wxConnection()
{
dialog = new IPCDialogBox(wxTheApp->GetTopWindow(), "Connection",
wxPoint(100, 100), wxSize(500, 500), this);
dialog->Show(TRUE);
the_connection = this;
}
MyConnection::~MyConnection()
{
if (the_connection)
{
if (dialog)
{
dialog->m_connection = NULL;
dialog->Destroy();
}
the_connection = NULL;
}
}
bool MyConnection::OnExecute(const wxString& WXUNUSED(topic),
char *data,
int WXUNUSED(size),
wxIPCFormat WXUNUSED(format))
{
wxLogStatus(wxT("Execute command: %s"), data);
return TRUE;
}
bool MyConnection::OnPoke(const wxString& WXUNUSED(topic),
const wxString& item,
char *data,
int WXUNUSED(size),
wxIPCFormat WXUNUSED(format))
{
wxLogStatus(wxT("Poke command: %s = %s"), item.c_str(), data);
return TRUE;
}
char *MyConnection::OnRequest(const wxString& WXUNUSED(topic),
const wxString& WXUNUSED(item),
int * WXUNUSED(size),
wxIPCFormat WXUNUSED(format))
{
return "Here, have your data, client!";
}
bool MyConnection::OnStartAdvise(const wxString& WXUNUSED(topic),
const wxString& WXUNUSED(item))
{
return TRUE;
}