Rewrote large parts of the non-DDE IPC stuff (wxTCPServer, wxTCPClient,
[wxWidgets.git] / include / wx / sckipc.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sckipc.h
3 // Purpose: Interprocess communication implementation (wxSocket version)
4 // Author: Julian Smart
5 // Modified by: Guilhem Lavaux (big rewrite) May 1997, 1998
6 // Guillermo Rodriguez (updated for wxSocket v2) Jan 2000
7 // Created: 1993
8 // RCS-ID: $Id$
9 // Copyright: (c) Julian Smart 1993
10 // (c) Guilhem Lavaux 1997, 1998
11 // (c) 2000 Guillermo Rodriguez <guille@iies.es>
12 // Licence: wxWindows license
13 /////////////////////////////////////////////////////////////////////////////
14 #ifndef _WX_SCKIPC_H
15 #define _WX_SCKIPC_H
16
17 #ifdef __GNUG__
18 #pragma interface
19 #endif
20
21 #include "wx/defs.h"
22 #include "wx/setup.h"
23 #include "wx/ipcbase.h"
24 #include "wx/socket.h"
25 #include "wx/sckstrm.h"
26 #include "wx/datstrm.h"
27
28 /*
29 * Mini-DDE implementation
30
31 Most transactions involve a topic name and an item name (choose these
32 as befits your application).
33
34 A client can:
35
36 - ask the server to execute commands (data) associated with a topic
37 - request data from server by topic and item
38 - poke data into the server
39 - ask the server to start an advice loop on topic/item
40 - ask the server to stop an advice loop
41
42 A server can:
43
44 - respond to execute, request, poke and advice start/stop
45 - send advise data to client
46
47 Note that this limits the server in the ways it can send data to the
48 client, i.e. it can't send unsolicited information.
49 *
50 */
51
52 class wxTCPServer;
53 class wxTCPClient;
54 class wxTCPConnection: public wxConnectionBase
55 {
56 DECLARE_DYNAMIC_CLASS(wxTCPConnection)
57
58 protected:
59 wxSocketBase *m_sock;
60 wxSocketStream *m_sockstrm;
61 wxDataInputStream *m_codeci;
62 wxDataOutputStream *m_codeco;
63 wxString m_topic;
64
65 friend class wxTCPServer;
66 friend class wxTCPClient;
67 friend void Client_OnRequest(wxSocketBase&,
68 wxSocketNotify, char *);
69 friend void Server_OnRequest(wxSocketServer&,
70 wxSocketNotify, char *);
71 public:
72
73 wxTCPConnection(char *buffer, int size);
74 wxTCPConnection();
75 virtual ~wxTCPConnection();
76
77 // Calls that CLIENT can make
78 virtual bool Execute(const wxChar *data, int size = -1, wxIPCFormat format = wxIPC_TEXT);
79 virtual bool Execute(const wxString& str) { return Execute(str, -1, wxIPC_TEXT); }
80 virtual char *Request(const wxString& item, int *size = NULL, wxIPCFormat format = wxIPC_TEXT);
81 virtual bool Poke(const wxString& item, wxChar *data, int size = -1, wxIPCFormat format = wxIPC_TEXT);
82 virtual bool StartAdvise(const wxString& item);
83 virtual bool StopAdvise(const wxString& item);
84
85 // Calls that SERVER can make
86 virtual bool Advise(const wxString& item, wxChar *data, int size = -1, wxIPCFormat format = wxIPC_TEXT);
87
88 // Calls that both can make
89 virtual bool Disconnect(void);
90
91 // Callbacks to SERVER - override at will
92 virtual bool OnExecute(const wxString& topic, char *data, int size, wxIPCFormat format) { return FALSE; };
93 virtual char *OnRequest(const wxString& topic, const wxString& item, int *size, wxIPCFormat format) { return NULL; };
94 virtual bool OnPoke(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) { return FALSE; };
95 virtual bool OnStartAdvise(const wxString& topic, const wxString& item) { return FALSE; };
96 virtual bool OnStopAdvise(const wxString& topic, const wxString& item) { return FALSE; };
97
98 // Callbacks to CLIENT - override at will
99 virtual bool OnAdvise(const wxString& topic, const wxString& item, char *data, int size, wxIPCFormat format) { return FALSE; };
100
101 // Callbacks to BOTH
102
103 // Default behaviour is to delete connection and return TRUE
104 virtual bool OnDisconnect(void) { delete this; return TRUE; }
105
106 // To enable the compressor
107 void Compress(bool on);
108
109 private:
110 };
111
112 class wxTCPServer: public wxServerBase
113 {
114 DECLARE_DYNAMIC_CLASS(wxTCPServer)
115
116 public:
117 wxTCPConnection *topLevelConnection;
118
119 wxTCPServer();
120 virtual ~wxTCPServer();
121
122 // Returns FALSE if can't create server (e.g. port number is already in use)
123 virtual bool Create(const wxString& server_name);
124 virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
125 };
126
127 class wxTCPClient: public wxClientBase
128 {
129 DECLARE_DYNAMIC_CLASS(wxTCPClient)
130
131 public:
132 wxTCPClient();
133 virtual ~wxTCPClient();
134
135 virtual bool ValidHost(const wxString& host);
136 // Call this to make a connection.
137 // Returns NULL if cannot.
138 virtual wxConnectionBase *MakeConnection(const wxString& host,
139 const wxString& server,
140 const wxString& topic);
141
142 // Tailor this to return own connection.
143 virtual wxConnectionBase *OnMakeConnection();
144 };
145
146 #endif // ipcsock.h