1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Base classes for IPC
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_IPCBASEH__
12 #define _WX_IPCBASEH__
15 #include "wx/object.h"
16 #include "wx/string.h"
21 wxIPC_TEXT
= 1, /* CF_TEXT */
22 wxIPC_BITMAP
= 2, /* CF_BITMAP */
23 wxIPC_METAFILE
= 3, /* CF_METAFILEPICT */
27 wxIPC_OEMTEXT
= 7, /* CF_OEMTEXT */
28 wxIPC_DIB
= 8, /* CF_DIB */
33 wxIPC_UTF16TEXT
= 13, /* CF_UNICODE */
34 wxIPC_ENHMETAFILE
= 14,
35 wxIPC_FILENAME
= 15, /* CF_HDROP */
39 #if SIZEOF_WCHAR_T == 2
40 wxIPC_UNICODETEXT
= wxIPC_UTF16TEXT
,
41 #elif SIZEOF_WCHAR_T == 4
42 wxIPC_UNICODETEXT
= wxIPC_UTF32TEXT
,
44 # error "Unknown wchar_t size"
49 class WXDLLIMPEXP_FWD_BASE wxServerBase
;
50 class WXDLLIMPEXP_FWD_BASE wxClientBase
;
52 class WXDLLIMPEXP_BASE wxConnectionBase
: public wxObject
55 wxConnectionBase(void *buffer
, size_t size
); // use external buffer
56 wxConnectionBase(); // use internal, adaptive buffer
57 wxConnectionBase(const wxConnectionBase
& copy
);
58 virtual ~wxConnectionBase();
60 void SetConnected( bool c
) { m_connected
= c
; }
61 bool GetConnected() { return m_connected
; }
63 // Calls that CLIENT can make
64 bool Execute(const void *data
, size_t size
, wxIPCFormat fmt
= wxIPC_PRIVATE
)
65 { return DoExecute(data
, size
, fmt
); }
66 bool Execute(const char *s
, size_t size
= wxNO_LEN
)
67 { return DoExecute(s
, size
== wxNO_LEN
? strlen(s
) + 1
68 : size
, wxIPC_TEXT
); }
69 bool Execute(const wchar_t *ws
, size_t size
= wxNO_LEN
)
70 { return DoExecute(ws
, size
== wxNO_LEN
? (wcslen(ws
) + 1)*sizeof(wchar_t)
71 : size
, wxIPC_UNICODETEXT
); }
72 bool Execute(const wxString
& s
)
74 const wxScopedCharBuffer buf
= s
.utf8_str();
75 return DoExecute(buf
, strlen(buf
) + 1, wxIPC_UTF8TEXT
);
77 bool Execute(const wxCStrData
& cs
)
78 { return Execute(cs
.AsString()); }
80 virtual const void *Request(const wxString
& item
,
82 wxIPCFormat format
= wxIPC_TEXT
) = 0;
84 bool Poke(const wxString
& item
, const void *data
, size_t size
,
85 wxIPCFormat fmt
= wxIPC_PRIVATE
)
86 { return DoPoke(item
, data
, size
, fmt
); }
87 bool Poke(const wxString
& item
, const char *s
, size_t size
= wxNO_LEN
)
88 { return DoPoke(item
, s
, size
== wxNO_LEN
? strlen(s
) + 1
89 : size
, wxIPC_TEXT
); }
90 bool Poke(const wxString
& item
, const wchar_t *ws
, size_t size
= wxNO_LEN
)
91 { return DoPoke(item
, ws
,
92 size
== wxNO_LEN
? (wcslen(ws
) + 1)*sizeof(wchar_t)
93 : size
, wxIPC_UNICODETEXT
); }
94 bool Poke(const wxString
& item
, const wxString s
)
96 const wxScopedCharBuffer buf
= s
.utf8_str();
97 return DoPoke(item
, buf
, strlen(buf
) + 1, wxIPC_UTF8TEXT
);
99 bool Poke(const wxString
& item
, const wxCStrData
& cs
)
100 { return Poke(item
, cs
.AsString()); }
102 virtual bool StartAdvise(const wxString
& item
) = 0;
103 virtual bool StopAdvise(const wxString
& item
) = 0;
105 // Calls that SERVER can make
106 bool Advise(const wxString
& item
, const void *data
, size_t size
,
107 wxIPCFormat fmt
= wxIPC_PRIVATE
)
108 { return DoAdvise(item
, data
, size
, fmt
); }
109 bool Advise(const wxString
& item
, const char *s
, size_t size
= wxNO_LEN
)
110 { return DoAdvise(item
, s
, size
== wxNO_LEN
? strlen(s
) + 1
111 : size
, wxIPC_TEXT
); }
112 bool Advise(const wxString
& item
, const wchar_t *ws
, size_t size
= wxNO_LEN
)
113 { return DoAdvise(item
, ws
,
114 size
== wxNO_LEN
? (wcslen(ws
) + 1)*sizeof(wchar_t)
115 : size
, wxIPC_UNICODETEXT
); }
116 bool Advise(const wxString
& item
, const wxString s
)
118 const wxScopedCharBuffer buf
= s
.utf8_str();
119 return DoAdvise(item
, buf
, strlen(buf
) + 1, wxIPC_UTF8TEXT
);
121 bool Advise(const wxString
& item
, const wxCStrData
& cs
)
122 { return Advise(item
, cs
.AsString()); }
124 // Calls that both can make
125 virtual bool Disconnect() = 0;
128 // Callbacks to SERVER - override at will
129 virtual bool OnExec(const wxString
& WXUNUSED(topic
),
130 const wxString
& WXUNUSED(data
))
132 wxFAIL_MSG( "This method shouldn't be called, if it is, it probably "
133 "means that you didn't update your old code overriding "
134 "OnExecute() to use the new parameter types (\"const void *\" "
135 "instead of \"wxChar *\" and \"size_t\" instead of \"int\"), "
136 "you must do it or your code wouldn't be executed at all!" );
140 // deprecated function kept for backwards compatibility: usually you will
141 // want to override OnExec() above instead which receives its data in a more
143 virtual bool OnExecute(const wxString
& topic
,
147 { return OnExec(topic
, GetTextFromData(data
, size
, format
)); }
149 virtual const void *OnRequest(const wxString
& WXUNUSED(topic
),
150 const wxString
& WXUNUSED(item
),
152 wxIPCFormat
WXUNUSED(format
))
153 { *size
= 0; return NULL
; }
155 virtual bool OnPoke(const wxString
& WXUNUSED(topic
),
156 const wxString
& WXUNUSED(item
),
157 const void *WXUNUSED(data
),
158 size_t WXUNUSED(size
),
159 wxIPCFormat
WXUNUSED(format
))
162 virtual bool OnStartAdvise(const wxString
& WXUNUSED(topic
),
163 const wxString
& WXUNUSED(item
))
166 virtual bool OnStopAdvise(const wxString
& WXUNUSED(topic
),
167 const wxString
& WXUNUSED(item
))
170 // Callbacks to CLIENT - override at will
171 virtual bool OnAdvise(const wxString
& WXUNUSED(topic
),
172 const wxString
& WXUNUSED(item
),
173 const void *WXUNUSED(data
),
174 size_t WXUNUSED(size
),
175 wxIPCFormat
WXUNUSED(format
))
179 virtual bool OnDisconnect() { delete this; return true; }
182 // return true if this is one of the formats used for textual data
184 static bool IsTextFormat(wxIPCFormat format
)
186 return format
== wxIPC_TEXT
||
187 format
== wxIPC_UTF8TEXT
||
188 format
== wxIPC_UTF16TEXT
||
189 format
== wxIPC_UTF32TEXT
;
192 // converts from the data and format into a wxString automatically
194 // this function accepts data in all of wxIPC_TEXT, wxIPC_UNICODETEXT, and
195 // wxIPC_UTF8TEXT formats but asserts if the format is anything else (i.e.
196 // such that IsTextFormat(format) doesn't return true)
198 // notice that the size parameter here contains the total size of the data,
199 // including the terminating '\0' or L'\0'
201 wxString
GetTextFromData(const void *data
, size_t size
, wxIPCFormat format
);
204 // return a buffer at least this size, reallocating buffer if needed
205 // returns NULL if using an inadequate user buffer which can't be resized
206 void *GetBufferAtLeast(size_t bytes
);
209 virtual bool DoExecute(const void *data
, size_t size
, wxIPCFormat format
) = 0;
210 virtual bool DoPoke(const wxString
& item
, const void *data
, size_t size
,
211 wxIPCFormat format
) = 0;
212 virtual bool DoAdvise(const wxString
& item
, const void *data
, size_t size
,
213 wxIPCFormat format
) = 0;
219 bool m_deletebufferwhendone
;
224 wxDECLARE_NO_ASSIGN_CLASS(wxConnectionBase
);
225 DECLARE_CLASS(wxConnectionBase
)
229 class WXDLLIMPEXP_BASE wxServerBase
: public wxObject
233 virtual ~wxServerBase() { }
235 // Returns false on error (e.g. port number is already in use)
236 virtual bool Create(const wxString
& serverName
) = 0;
238 // Callbacks to SERVER - override at will
239 virtual wxConnectionBase
*OnAcceptConnection(const wxString
& topic
) = 0;
241 DECLARE_CLASS(wxServerBase
)
244 class WXDLLIMPEXP_BASE wxClientBase
: public wxObject
248 virtual ~wxClientBase() { }
250 virtual bool ValidHost(const wxString
& host
) = 0;
252 // Call this to make a connection. Returns NULL if cannot.
253 virtual wxConnectionBase
*MakeConnection(const wxString
& host
,
254 const wxString
& server
,
255 const wxString
& topic
) = 0;
257 // Callbacks to CLIENT - override at will
258 virtual wxConnectionBase
*OnMakeConnection() = 0;
260 DECLARE_CLASS(wxClientBase
)
263 #endif // _WX_IPCBASEH__