You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

qtipcserver.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright (c) 2009-2012 The Bitcoin developers
  2. // Distributed under the MIT/X11 software license, see the accompanying
  3. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  4. #include "qtipcserver.h"
  5. #include "guiconstants.h"
  6. #include "ui_interface.h"
  7. #include "util.h"
  8. #include <boost/date_time/posix_time/posix_time.hpp>
  9. #include <boost/interprocess/ipc/message_queue.hpp>
  10. #include <boost/version.hpp>
  11. #if defined(WIN32) && (!defined(BOOST_INTERPROCESS_HAS_WINDOWS_KERNEL_BOOTTIME) || !defined(BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME) || BOOST_VERSION < 104900)
  12. #warning Compiling without BOOST_INTERPROCESS_HAS_WINDOWS_KERNEL_BOOTTIME and BOOST_INTERPROCESS_HAS_KERNEL_BOOTTIME uncommented in boost/interprocess/detail/tmp_dir_helpers.hpp or using a boost version before 1.49 may have unintended results see svn.boost.org/trac/boost/ticket/5392
  13. #endif
  14. using namespace boost;
  15. using namespace boost::interprocess;
  16. using namespace boost::posix_time;
  17. static void ipcThread2(void* pArg);
  18. #ifdef MAC_OSX
  19. // URI handling not implemented on OSX yet
  20. void ipcInit() { }
  21. #else
  22. static void ipcThread(void* pArg)
  23. {
  24. IMPLEMENT_RANDOMIZE_STACK(ipcThread(pArg));
  25. // Make this thread recognisable as the GUI-IPC thread
  26. RenameThread("bitcoin-gui-ipc");
  27. try
  28. {
  29. ipcThread2(pArg);
  30. }
  31. catch (std::exception& e) {
  32. PrintExceptionContinue(&e, "ipcThread()");
  33. } catch (...) {
  34. PrintExceptionContinue(NULL, "ipcThread()");
  35. }
  36. printf("ipcThread exited\n");
  37. }
  38. static void ipcThread2(void* pArg)
  39. {
  40. printf("ipcThread started\n");
  41. message_queue* mq = (message_queue*)pArg;
  42. char buffer[MAX_URI_LENGTH + 1] = "";
  43. size_t nSize = 0;
  44. unsigned int nPriority = 0;
  45. loop
  46. {
  47. ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(100);
  48. if (mq->timed_receive(&buffer, sizeof(buffer), nSize, nPriority, d))
  49. {
  50. uiInterface.ThreadSafeHandleURI(std::string(buffer, nSize));
  51. Sleep(1000);
  52. }
  53. if (fShutdown)
  54. break;
  55. }
  56. // Remove message queue
  57. message_queue::remove(BITCOINURI_QUEUE_NAME);
  58. // Cleanup allocated memory
  59. delete mq;
  60. }
  61. void ipcInit()
  62. {
  63. message_queue* mq = NULL;
  64. char buffer[MAX_URI_LENGTH + 1] = "";
  65. size_t nSize = 0;
  66. unsigned int nPriority = 0;
  67. try {
  68. mq = new message_queue(open_or_create, BITCOINURI_QUEUE_NAME, 2, MAX_URI_LENGTH);
  69. // Make sure we don't lose any bitcoin: URIs
  70. for (int i = 0; i < 2; i++)
  71. {
  72. ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(1);
  73. if (mq->timed_receive(&buffer, sizeof(buffer), nSize, nPriority, d))
  74. {
  75. uiInterface.ThreadSafeHandleURI(std::string(buffer, nSize));
  76. }
  77. else
  78. break;
  79. }
  80. // Make sure only one bitcoin instance is listening
  81. message_queue::remove(BITCOINURI_QUEUE_NAME);
  82. delete mq;
  83. mq = new message_queue(open_or_create, BITCOINURI_QUEUE_NAME, 2, MAX_URI_LENGTH);
  84. }
  85. catch (interprocess_exception &ex) {
  86. printf("ipcInit() - boost interprocess exception #%d: %s\n", ex.get_error_code(), ex.what());
  87. return;
  88. }
  89. if (!CreateThread(ipcThread, mq))
  90. {
  91. delete mq;
  92. return;
  93. }
  94. }
  95. #endif