ipcclient.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: tests/benchmarks/ipcclient.cpp
  3. // Purpose: wxIPC client side benchmarks
  4. // Author: Vadim Zeitlin
  5. // Created: 2008-10-13
  6. // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #include "bench.h"
  10. #include "wx/evtloop.h"
  11. // do this before including wx/ipc.h under Windows to use TCP even there
  12. #define wxUSE_DDE_FOR_IPC 0
  13. #include "wx/ipc.h"
  14. #include "../../samples/ipc/ipcsetup.h"
  15. namespace
  16. {
  17. class PokeAdviseConn : public wxConnection
  18. {
  19. public:
  20. PokeAdviseConn() { m_gotAdvised = false; }
  21. bool GotAdvised()
  22. {
  23. if ( !m_gotAdvised )
  24. return false;
  25. m_gotAdvised = false;
  26. return true;
  27. }
  28. const wxString& GetItem() const { return m_item; }
  29. virtual bool OnAdvise(const wxString& topic,
  30. const wxString& item,
  31. const void *data,
  32. size_t size,
  33. wxIPCFormat format)
  34. {
  35. m_gotAdvised = true;
  36. if ( topic != IPC_BENCHMARK_TOPIC ||
  37. item != IPC_BENCHMARK_ITEM ||
  38. !IsTextFormat(format) )
  39. {
  40. m_item = "ERROR";
  41. return false;
  42. }
  43. m_item = GetTextFromData(data, size, format);
  44. return true;
  45. }
  46. private:
  47. wxString m_item;
  48. bool m_gotAdvised;
  49. DECLARE_NO_COPY_CLASS(PokeAdviseConn)
  50. };
  51. class PokeAdviseClient : public wxClient
  52. {
  53. public:
  54. // provide a convenient helper taking care of connecting to the right
  55. // server/service/topic and returning the connection of the derived type
  56. // (or NULL if we failed to connect)
  57. PokeAdviseConn *Connect()
  58. {
  59. wxString host = Bench::GetStringParameter();
  60. if ( host.empty() )
  61. host = IPC_HOST;
  62. wxString service;
  63. int port = Bench::GetNumericParameter();
  64. if ( !port )
  65. service = IPC_SERVICE;
  66. else
  67. service.Printf("%d", port);
  68. return static_cast<PokeAdviseConn *>(
  69. MakeConnection(host, service, IPC_BENCHMARK_TOPIC));
  70. }
  71. // override base class virtual to use a custom connection class
  72. virtual wxConnectionBase *OnMakeConnection()
  73. {
  74. return new PokeAdviseConn;
  75. }
  76. };
  77. class PokeAdvisePersistentConnection
  78. {
  79. public:
  80. PokeAdvisePersistentConnection()
  81. {
  82. m_client = new PokeAdviseClient;
  83. m_conn = m_client->Connect();
  84. if ( m_conn )
  85. m_conn->StartAdvise(IPC_BENCHMARK_ITEM);
  86. }
  87. ~PokeAdvisePersistentConnection()
  88. {
  89. if ( m_conn )
  90. {
  91. m_conn->StopAdvise(IPC_BENCHMARK_ITEM);
  92. m_conn->Disconnect();
  93. }
  94. delete m_client;
  95. }
  96. PokeAdviseConn *Get() const { return m_conn; }
  97. private:
  98. PokeAdviseClient *m_client;
  99. PokeAdviseConn *m_conn;
  100. bool m_initDone;
  101. DECLARE_NO_COPY_CLASS(PokeAdvisePersistentConnection)
  102. };
  103. PokeAdvisePersistentConnection *theConnection = NULL;
  104. bool ConnInit()
  105. {
  106. theConnection = new PokeAdvisePersistentConnection;
  107. if ( !theConnection->Get() )
  108. {
  109. delete theConnection;
  110. theConnection = NULL;
  111. return false;
  112. }
  113. return true;
  114. }
  115. void ConnDone()
  116. {
  117. delete theConnection;
  118. }
  119. } // anonymous namespace
  120. BENCHMARK_FUNC_WITH_INIT(IPCPokeAdvise, ConnInit, ConnDone)
  121. {
  122. wxEventLoop loop;
  123. PokeAdviseConn * const conn = theConnection->Get();
  124. const wxString s(1024, '@');
  125. if ( !conn->Poke(IPC_BENCHMARK_ITEM, s) )
  126. return false;
  127. while ( !conn->GotAdvised() )
  128. loop.Dispatch();
  129. if ( conn->GetItem() != s )
  130. return false;
  131. return true;
  132. }