1 /////////////////////////////////////////////////////////////////////////////
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
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 /////////////////////////////////////////////////////////////////////////////
23 #include "wx/ipcbase.h"
24 #include "wx/socket.h"
25 #include "wx/sckstrm.h"
26 #include "wx/datstrm.h"
29 * Mini-DDE implementation
31 Most transactions involve a topic name and an item name (choose these
32 as befits your application).
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
44 - respond to execute, request, poke and advice start/stop
45 - send advise data to client
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.
54 class wxTCPConnection
: public wxConnectionBase
56 DECLARE_DYNAMIC_CLASS(wxTCPConnection
)
60 wxSocketStream
*m_sockstrm
;
61 wxDataInputStream
*m_codeci
;
62 wxDataOutputStream
*m_codeco
;
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 *);
73 wxTCPConnection(char *buffer
, int size
);
75 virtual ~wxTCPConnection();
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
);
85 // Calls that SERVER can make
86 virtual bool Advise(const wxString
& item
, wxChar
*data
, int size
= -1, wxIPCFormat format
= wxIPC_TEXT
);
88 // Calls that both can make
89 virtual bool Disconnect(void);
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
; };
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
; };
103 // Default behaviour is to delete connection and return TRUE
104 virtual bool OnDisconnect(void) { delete this; return TRUE
; }
106 // To enable the compressor
107 void Compress(bool on
);
112 class wxTCPServer
: public wxServerBase
114 DECLARE_DYNAMIC_CLASS(wxTCPServer
)
117 wxTCPConnection
*topLevelConnection
;
120 virtual ~wxTCPServer();
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
);
127 class wxTCPClient
: public wxClientBase
129 DECLARE_DYNAMIC_CLASS(wxTCPClient
)
133 virtual ~wxTCPClient();
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
);
142 // Tailor this to return own connection.
143 virtual wxConnectionBase
*OnMakeConnection();