📄 tipc.tex
字号:
\section{Interprocess communication overview}\label{ipcoverview}Classes: \helpref{wxServer}{wxserver},\helpref{wxConnection}{wxddeconnection},\helpref{wxClient}{wxclient}%\helpref{wxTCPServer}{wxtcpserver}, \helpref{wxTCPConnection}{wxtcpconnection},%\helpref{wxTCPClient}{wxtcpclient}wxWidgets has a number of different classes to help withinterprocess communication and network programming. This sectiononly discusses one family of classes -- the DDE-like protocol --but here's a list of other useful classes:\begin{itemize}\itemsep=0pt\item \helpref{wxSocketEvent}{wxsocketevent},\helpref{wxSocketBase}{wxsocketbase},\helpref{wxSocketClient}{wxsocketclient},\helpref{wxSocketServer}{wxsocketserver}: classes for the low-level TCP/IP API.\item \helpref{wxProtocol}{wxprotocol}, \helpref{wxURL}{wxurl}, \helpref{wxFTP}{wxftp}, \helpref{wxHTTP}{wxhttp}: classesfor programming popular Internet protocols.\end{itemize}wxWidgets' DDE-like protocol is a high-level protocol based onWindows DDE. There are two implementations of this DDE-likeprotocol: one using real DDE running on Windows only, and anotherusing TCP/IP (sockets) that runs on most platforms. Since the APIand virtually all of the behaviour is the same apart from thenames of the classes, you should find it easy to switch betweenthe two implementations.Notice that by including {\tt <wx/ipc.h>} you may defineconvenient synonyms for the IPC classes: {\tt wxServer} for either{\tt wxDDEServer} or {\tt wxTCPServer} depending on whetherDDE-based or socket-based implementation is used and the samething for {\tt wxClient} and {\tt wxConnection}.By default, the DDE implementation is used under Windows. DDE workswithin one computer only. If you want to use IPC betweendifferent workstations you should define {\ttwxUSE\_DDE\_FOR\_IPC} as $0$ before including this header -- thiswill force using TCP/IP implementation even under Windows.The following description refers to wx... but remember that theequivalent wxTCP... and wxDDE... classes can be used in much thesame way.Three classes are central to the DDE-like API:\begin{enumerate}\itemsep=0pt\item wxClient. This represents the client application, and is usedonly within a client program.\item wxServer. This represents the server application, and is usedonly within a server program.\item wxConnection. This represents the connection from theclient to the server - both the client and the server use aninstance of this class, one per connection. Most DDE transactionsoperate on this object.\end{enumerate}Messages between applications are usually identified by threevariables: connection object, topic name and item name. A datastring is a fourth element of some messages. To create aconnection (a conversation in Windows parlance), the clientapplication uses wxClient::MakeConnection to send a message to theserver object, with a string service name to identify the serverand a topic name to identify the topic for the duration of theconnection. Under Unix, the service name may be either an integerport identifier in which case an Internet domain socket will beused for the communications or a valid file name (which shouldn'texist and will be deleted afterwards) in which case a Unix domainsocket is created.{\bf SECURITY NOTE:} Using Internet domain sockets is extremely insecure forIPC as there is absolutely no access control for them, use Unix domain socketswhenever possible!The server then responds and either vetoes the connection orallows it. If allowed, both the server and client objects createwxConnection objects which persist until the connection isclosed. The connection object is then used for sending andreceiving subsequent messages between client and server -overriding virtual functions in your class derived fromwxConnection allows you to handle the DDE messages.To create a working server, the programmer must:\begin{enumerate}\itemsep=0pt\item Derive a class from wxConnection, providing handlers for various messages sent to the serverside of a wxConnection (e.g. OnExecute, OnRequest, OnPoke). Onlythe handlers actually required by the application need to beoverridden.\item Derive a class from wxServer, overriding OnAcceptConnectionto accept or reject a connection on the basis of the topicargument. This member must create and return an instance of thederived connection class if the connection is accepted.\item Create an instance of your server object and call Create toactivate it, giving it a service name.\end{enumerate}To create a working client, the programmer must:\begin{enumerate}\itemsep=0pt\item Derive a class from wxConnection, providing handlers for variousmessages sent to the client side of a wxConnection (e.g.OnAdvise). Only the handlers actually required by the applicationneed to be overridden.\item Derive a class from wxClient, overriding OnMakeConnection tocreate and return an instance of the derived connection class.\item Create an instance of your client object.\item When appropriate, create a new connection using\helpref{wxClient::MakeConnection}{wxclientmakeconnection},with arguments host name (processed in Unix only, use `localhost'for local computer), service name, and topic name for thisconnection. The client object will call\helpref{OnMakeConnection}{wxddeclientonmakeconnection} to createa connection object of the derived class if the connection issuccessful.\item Use the wxConnection member functions to send messages to the server.\end{enumerate}\subsection{Data transfer}\label{datatransfer}These are the ways that data can be transferred from oneapplication to another. These are methods of wxConnection.\begin{itemize}\itemsep=0pt\item {\bf Execute:} the client calls the server with a data string representinga command to be executed. This succeeds or fails, depending on theserver's willingness to answer. If the client wants to find the resultof the Execute command other than success or failure, it has to explicitlycall Request.\item {\bf Request:} the client asks the server for a particular data stringassociated with a given item string. If the server is unwilling toreply, the return value is NULL. Otherwise, the return value is a string(actually a pointer to the connection buffer, so it should not bedeallocated by the application).\item {\bf Poke:} The client sends a data string associated with an itemstring directly to the server. This succeeds or fails.\item {\bf Advise:} The client asks to be advised of any change in dataassociated with a particular item. If the server agrees, the server willsend an OnAdvise message to the client along with the item and data.\end{itemize}The default data type is wxCF\_TEXT (ASCII text), and the default datasize is the length of the null-terminated string. Windows-specific datatypes could also be used on the PC.\subsection{Examples}\label{ipcexamples}See the sample programs {\it server}\/ and {\it client}\/ in the IPCsamples directory. Run the server, then the client. This demonstratesusing the Execute, Request, and Poke commands from the client, togetherwith an Advise loop: selecting an item in the server list box causesthat item to be highlighted in the client list box.\subsection{More DDE details}\label{ddedetails}A wxClient object initiates the client part of a client-serverDDE-like (Dynamic Data Exchange) conversation (available in bothWindows and Unix).To create a client which can communicate with a suitable server,you need to derive a class from wxConnection and another fromwxClient. The custom wxConnection class will receivecommunications in a `conversation' with a server. and the customwxServer is required so that a user-overridden\helpref{wxClient::OnMakeConnection}{wxddeclientonmakeconnection}member can return a wxConnection of the required class, when aconnection is made.For example:\begin{verbatim}class MyConnection: public wxConnection { public: MyConnection(void)::wxConnection() {} ~MyConnection(void) { } bool OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) { wxMessageBox(topic, data); }};class MyClient: public wxClient { public: MyClient(void) {} wxConnectionBase *OnMakeConnection(void) { return new MyConnection; }};\end{verbatim}Here, {\bf MyConnection} will respond to\helpref{OnAdvise}{wxddeconnectiononadvise} messages sent by theserver by displaying a message box.When the client application starts, it must create an instance ofthe derived wxClient. In the following, command line argumentsare used to pass the host name (the name of the machine theserver is running on) and the server name (identifying the serverprocess). Calling\helpref{wxClient::MakeConnection}{wxddeclientmakeconnection}\rtfspimplicitly creates an instance of {\bf MyConnection} if therequest for a connection is accepted, and the client thenrequests an {\it Advise} loop from the server (an Advise loop iswhere the server calls the client when data has changed).\begin{verbatim} wxString server = "4242"; wxString hostName; wxGetHostName(hostName); // Create a new client MyClient *client = new MyClient; connection = (MyConnection *)client->MakeConnection(hostName, server, "IPC TEST"); if (!connection) { wxMessageBox("Failed to make connection to server", "Client Demo Error"); return NULL; } connection->StartAdvise("Item");\end{verbatim}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -