1999-01-27 10:01:14 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: client.cpp
|
|
|
|
// Purpose: DDE sample: client
|
|
|
|
// Author: Julian Smart
|
|
|
|
// Modified by:
|
|
|
|
// Created: 25/01/99
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) Julian Smart
|
|
|
|
// Licence: wxWindows licence
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
// ============================================================================
|
|
|
|
// declarations
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// headers
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-01-27 10:01:14 +00:00
|
|
|
// For compilers that support precompilation, includes "wx.h".
|
|
|
|
#include "wx/wxprec.h"
|
|
|
|
|
|
|
|
#ifdef __BORLANDC__
|
2000-01-15 22:34:29 +00:00
|
|
|
#pragma hdrstop
|
1999-01-27 10:01:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef WX_PRECOMP
|
2000-01-15 22:34:29 +00:00
|
|
|
#include "wx/wx.h"
|
1999-01-27 10:01:14 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Settings common to both executables: determines whether
|
|
|
|
// we're using TCP/IP or real DDE.
|
|
|
|
#include "ddesetup.h"
|
|
|
|
|
2002-03-21 10:32:18 +00:00
|
|
|
#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
|
1999-01-27 10:01:14 +00:00
|
|
|
#include "mondrian.xpm"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "client.h"
|
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxWin macros
|
|
|
|
// ----------------------------------------------------------------------------
|
1999-01-27 10:01:14 +00:00
|
|
|
|
|
|
|
IMPLEMENT_APP(MyApp)
|
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
|
|
|
|
EVT_MENU(CLIENT_QUIT, MyFrame::OnExit)
|
|
|
|
EVT_MENU(CLIENT_EXECUTE, MyFrame::OnExecute)
|
|
|
|
EVT_MENU(CLIENT_POKE, MyFrame::OnPoke)
|
|
|
|
EVT_MENU(CLIENT_REQUEST, MyFrame::OnRequest)
|
|
|
|
END_EVENT_TABLE()
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// globals
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-01-27 10:01:14 +00:00
|
|
|
wxListBox *the_list = NULL;
|
|
|
|
|
|
|
|
MyConnection *the_connection = NULL;
|
2002-03-17 14:16:03 +00:00
|
|
|
MyClient *my_client;
|
1999-01-27 10:01:14 +00:00
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
// ============================================================================
|
|
|
|
// implementation
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// MyApp
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-01-27 10:01:14 +00:00
|
|
|
// The `main program' equivalent, creating the windows and returning the
|
|
|
|
// main frame
|
|
|
|
bool MyApp::OnInit()
|
|
|
|
{
|
2000-07-15 19:51:35 +00:00
|
|
|
// service name (DDE classes) or port number (TCP/IP based classes)
|
2002-04-15 18:24:46 +00:00
|
|
|
wxString service = IPC_SERVICE;
|
2000-07-15 19:51:35 +00:00
|
|
|
|
|
|
|
// ignored under DDE, host name in TCP/IP based classes
|
2002-12-15 17:25:36 +00:00
|
|
|
wxString hostName = _T("localhost");
|
1999-01-27 10:01:14 +00:00
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
if (argc > 1)
|
2000-07-15 19:51:35 +00:00
|
|
|
service = argv[1];
|
2000-01-15 22:34:29 +00:00
|
|
|
if (argc > 2)
|
|
|
|
hostName = argv[2];
|
1999-01-27 10:01:14 +00:00
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
// Create a new client
|
|
|
|
my_client = new MyClient;
|
1999-01-27 10:01:14 +00:00
|
|
|
|
2000-03-11 00:47:45 +00:00
|
|
|
// suppress the log messages from MakeConnection()
|
2000-01-15 22:34:29 +00:00
|
|
|
{
|
2000-03-11 00:47:45 +00:00
|
|
|
wxLogNull nolog;
|
2002-04-15 18:24:46 +00:00
|
|
|
the_connection = (MyConnection *)
|
|
|
|
my_client->MakeConnection(hostName, service, IPC_TOPIC);
|
2000-03-11 00:47:45 +00:00
|
|
|
|
|
|
|
while ( !the_connection )
|
2000-01-15 22:34:29 +00:00
|
|
|
{
|
2002-12-15 17:25:36 +00:00
|
|
|
if ( wxMessageBox(_T("Failed to make connection to server.\nRetry?"),
|
|
|
|
_T("Client Demo Error"),
|
2000-03-11 00:47:45 +00:00
|
|
|
wxICON_ERROR | wxYES_NO | wxCANCEL ) != wxYES )
|
|
|
|
{
|
|
|
|
// no server
|
2004-06-02 17:03:20 +00:00
|
|
|
return false;
|
2000-03-11 00:47:45 +00:00
|
|
|
}
|
|
|
|
|
2002-12-15 17:25:36 +00:00
|
|
|
the_connection = (MyConnection *)my_client->MakeConnection(hostName, service, _T("IPC TEST"));
|
2000-01-15 22:34:29 +00:00
|
|
|
}
|
|
|
|
}
|
1999-01-27 10:01:14 +00:00
|
|
|
|
2002-04-15 18:24:46 +00:00
|
|
|
if (!the_connection->StartAdvise(IPC_ADVISE_NAME))
|
2002-12-15 17:25:36 +00:00
|
|
|
wxMessageBox(_T("StartAdvise failed"), _T("Client Demo Error"));
|
1999-01-27 10:01:14 +00:00
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
// Create the main frame window
|
2004-06-02 17:03:20 +00:00
|
|
|
(new MyFrame(NULL, _T("Client")))->Show(true);
|
2000-01-06 01:21:26 +00:00
|
|
|
|
2004-06-02 17:03:20 +00:00
|
|
|
return true;
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int MyApp::OnExit()
|
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
// will delete the connection too
|
2002-03-17 14:16:03 +00:00
|
|
|
// Update: Seems it didn't delete the_connection, because there's a leak.
|
|
|
|
// Deletion is now explicitly done a few lines up.
|
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
|
|
|
// another Update: in fact it's because OnDisconnect should delete it, but
|
|
|
|
// it wasn't
|
2000-01-15 22:34:29 +00:00
|
|
|
delete my_client;
|
1999-01-27 10:01:14 +00:00
|
|
|
|
2002-03-17 14:16:03 +00:00
|
|
|
|
|
|
|
|
1999-01-27 10:01:14 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define my frame constructor
|
2000-01-15 22:34:29 +00:00
|
|
|
MyFrame::MyFrame(wxFrame *frame, const wxString& title)
|
2004-06-02 17:03:20 +00:00
|
|
|
: wxFrame(frame, wxID_ANY, title, wxDefaultPosition, wxSize(300, 200))
|
1999-01-27 10:01:14 +00:00
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
// Give it an icon
|
|
|
|
SetIcon(wxICON(mondrian));
|
|
|
|
|
|
|
|
// Make a menubar
|
|
|
|
wxMenu *file_menu = new wxMenu;
|
|
|
|
|
2002-12-15 17:25:36 +00:00
|
|
|
file_menu->Append(CLIENT_EXECUTE, _T("&Execute\tCtrl-E"));
|
|
|
|
file_menu->Append(CLIENT_REQUEST, _T("&Request\tCtrl-R"));
|
|
|
|
file_menu->Append(CLIENT_POKE, _T("&Poke\tCtrl-P"));
|
|
|
|
file_menu->Append(CLIENT_QUIT, _T("&Quit\tCtrl-Q"));
|
2000-01-15 22:34:29 +00:00
|
|
|
|
|
|
|
wxMenuBar *menu_bar = new wxMenuBar;
|
|
|
|
|
2002-12-15 17:25:36 +00:00
|
|
|
menu_bar->Append(file_menu, _T("&File"));
|
2000-01-15 22:34:29 +00:00
|
|
|
|
|
|
|
// Associate the menu bar with the frame
|
|
|
|
SetMenuBar(menu_bar);
|
|
|
|
|
2002-03-27 19:00:49 +00:00
|
|
|
// Make a listbox which shows the choices made in the server
|
|
|
|
the_list = new wxListBox(this, CLIENT_LISTBOX, wxPoint(5, 5));
|
2002-12-15 17:25:36 +00:00
|
|
|
the_list->Append(_T("Apple"));
|
|
|
|
the_list->Append(_T("Pear"));
|
|
|
|
the_list->Append(_T("Orange"));
|
|
|
|
the_list->Append(_T("Banana"));
|
|
|
|
the_list->Append(_T("Fruit"));
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-09 20:24:34 +00:00
|
|
|
void MyFrame::OnExecute(wxCommandEvent& WXUNUSED(event))
|
1999-01-27 10:01:14 +00:00
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
if (the_connection)
|
2002-12-15 17:25:36 +00:00
|
|
|
if (!the_connection->Execute(_T("Hello from the client!")))
|
|
|
|
wxMessageBox(_T("Execute failed"), _T("Client Demo Error"));
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-09 20:24:34 +00:00
|
|
|
void MyFrame::OnPoke(wxCommandEvent& WXUNUSED(event))
|
1999-01-27 10:01:14 +00:00
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
if (the_connection)
|
2002-12-15 17:25:36 +00:00
|
|
|
if (!the_connection->Poke(_T("An item"), _T("Some data to poke at the server!")))
|
|
|
|
wxMessageBox(_T("Poke failed"), _T("Client Demo Error"));
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-09 20:24:34 +00:00
|
|
|
void MyFrame::OnRequest(wxCommandEvent& WXUNUSED(event))
|
1999-01-27 10:01:14 +00:00
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
if (the_connection)
|
|
|
|
{
|
2002-12-15 17:25:36 +00:00
|
|
|
wxChar *data = the_connection->Request(_T("An item"));
|
1999-01-27 10:01:14 +00:00
|
|
|
if (data)
|
2002-12-15 17:25:36 +00:00
|
|
|
wxMessageBox(data, _T("Client: Request"), wxOK);
|
2000-01-06 01:21:26 +00:00
|
|
|
else
|
2002-12-15 17:25:36 +00:00
|
|
|
wxMessageBox(_T("Request failed"), _T("Client Demo Error"));
|
2000-01-15 22:34:29 +00:00
|
|
|
}
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-09 20:24:34 +00:00
|
|
|
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
|
1999-01-27 10:01:14 +00:00
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
Close();
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
2000-01-15 22:34:29 +00:00
|
|
|
wxConnectionBase *MyClient::OnMakeConnection()
|
1999-01-27 10:01:14 +00:00
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
return new MyConnection;
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
2003-09-09 20:24:34 +00:00
|
|
|
bool MyConnection::OnAdvise(const wxString& WXUNUSED(topic), const wxString& WXUNUSED(item), wxChar *data, int WXUNUSED(size), wxIPCFormat WXUNUSED(format))
|
1999-01-27 10:01:14 +00:00
|
|
|
{
|
2000-01-15 22:34:29 +00:00
|
|
|
if (the_list)
|
|
|
|
{
|
|
|
|
int n = the_list->FindString(data);
|
2004-06-02 17:03:20 +00:00
|
|
|
if (n > wxNOT_FOUND)
|
2000-01-15 22:34:29 +00:00
|
|
|
the_list->SetSelection(n);
|
|
|
|
}
|
2004-06-02 17:03:20 +00:00
|
|
|
return true;
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool MyConnection::OnDisconnect()
|
|
|
|
{
|
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
|
|
|
// when connection is terminated, quit whole program
|
2000-01-15 22:34:29 +00:00
|
|
|
wxWindow *win = wxTheApp->GetTopWindow();
|
|
|
|
if ( win )
|
|
|
|
win->Destroy();
|
1999-01-27 10:01:14 +00:00
|
|
|
|
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
|
|
|
// delete self
|
|
|
|
the_connection = NULL;
|
|
|
|
return wxConnection::OnDisconnect();
|
1999-01-27 10:01:14 +00:00
|
|
|
}
|
|
|
|
|