sckipc.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/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. // (callbacks deprecated) Mar 2000
  8. // Created: 1993
  9. // Copyright: (c) Julian Smart 1993
  10. // (c) Guilhem Lavaux 1997, 1998
  11. // (c) 2000 Guillermo Rodriguez <guille@iies.es>
  12. // Licence: wxWindows licence
  13. /////////////////////////////////////////////////////////////////////////////
  14. #ifndef _WX_SCKIPC_H
  15. #define _WX_SCKIPC_H
  16. #include "wx/defs.h"
  17. #if wxUSE_SOCKETS && wxUSE_IPC
  18. #include "wx/ipcbase.h"
  19. #include "wx/socket.h"
  20. #include "wx/sckstrm.h"
  21. #include "wx/datstrm.h"
  22. /*
  23. * Mini-DDE implementation
  24. Most transactions involve a topic name and an item name (choose these
  25. as befits your application).
  26. A client can:
  27. - ask the server to execute commands (data) associated with a topic
  28. - request data from server by topic and item
  29. - poke data into the server
  30. - ask the server to start an advice loop on topic/item
  31. - ask the server to stop an advice loop
  32. A server can:
  33. - respond to execute, request, poke and advice start/stop
  34. - send advise data to client
  35. Note that this limits the server in the ways it can send data to the
  36. client, i.e. it can't send unsolicited information.
  37. *
  38. */
  39. class WXDLLIMPEXP_FWD_NET wxTCPServer;
  40. class WXDLLIMPEXP_FWD_NET wxTCPClient;
  41. class wxIPCSocketStreams;
  42. class WXDLLIMPEXP_NET wxTCPConnection : public wxConnectionBase
  43. {
  44. public:
  45. wxTCPConnection() { Init(); }
  46. wxTCPConnection(void *buffer, size_t size)
  47. : wxConnectionBase(buffer, size)
  48. {
  49. Init();
  50. }
  51. virtual ~wxTCPConnection();
  52. // implement base class pure virtual methods
  53. virtual const void *Request(const wxString& item,
  54. size_t *size = NULL,
  55. wxIPCFormat format = wxIPC_TEXT);
  56. virtual bool StartAdvise(const wxString& item);
  57. virtual bool StopAdvise(const wxString& item);
  58. virtual bool Disconnect(void);
  59. // Will be used in the future to enable the compression but does nothing
  60. // for now.
  61. void Compress(bool on);
  62. protected:
  63. virtual bool DoExecute(const void *data, size_t size, wxIPCFormat format);
  64. virtual bool DoPoke(const wxString& item, const void *data, size_t size,
  65. wxIPCFormat format);
  66. virtual bool DoAdvise(const wxString& item, const void *data, size_t size,
  67. wxIPCFormat format);
  68. // notice that all the members below are only initialized once the
  69. // connection is made, i.e. in MakeConnection() for the client objects and
  70. // after OnAcceptConnection() in the server ones
  71. // the underlying socket (wxSocketClient for IPC client and wxSocketServer
  72. // for IPC server)
  73. wxSocketBase *m_sock;
  74. // various streams that we use
  75. wxIPCSocketStreams *m_streams;
  76. // the topic of this connection
  77. wxString m_topic;
  78. private:
  79. // common part of both ctors
  80. void Init();
  81. friend class wxTCPServer;
  82. friend class wxTCPClient;
  83. friend class wxTCPEventHandler;
  84. wxDECLARE_NO_COPY_CLASS(wxTCPConnection);
  85. DECLARE_DYNAMIC_CLASS(wxTCPConnection)
  86. };
  87. class WXDLLIMPEXP_NET wxTCPServer : public wxServerBase
  88. {
  89. public:
  90. wxTCPServer();
  91. virtual ~wxTCPServer();
  92. // Returns false on error (e.g. port number is already in use)
  93. virtual bool Create(const wxString& serverName);
  94. virtual wxConnectionBase *OnAcceptConnection(const wxString& topic);
  95. protected:
  96. wxSocketServer *m_server;
  97. #ifdef __UNIX_LIKE__
  98. // the name of the file associated to the Unix domain socket, may be empty
  99. wxString m_filename;
  100. #endif // __UNIX_LIKE__
  101. wxDECLARE_NO_COPY_CLASS(wxTCPServer);
  102. DECLARE_DYNAMIC_CLASS(wxTCPServer)
  103. };
  104. class WXDLLIMPEXP_NET wxTCPClient : public wxClientBase
  105. {
  106. public:
  107. wxTCPClient();
  108. virtual bool ValidHost(const wxString& host);
  109. // Call this to make a connection. Returns NULL if cannot.
  110. virtual wxConnectionBase *MakeConnection(const wxString& host,
  111. const wxString& server,
  112. const wxString& topic);
  113. // Callbacks to CLIENT - override at will
  114. virtual wxConnectionBase *OnMakeConnection();
  115. private:
  116. DECLARE_DYNAMIC_CLASS(wxTCPClient)
  117. };
  118. #endif // wxUSE_SOCKETS && wxUSE_IPC
  119. #endif // _WX_SCKIPC_H