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 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 <boost/algorithm/string.hpp>
  5. #include <boost/interprocess/ipc/message_queue.hpp>
  6. #include <boost/tokenizer.hpp>
  7. #include <boost/date_time/posix_time/posix_time.hpp>
  8. #include "headers.h"
  9. using namespace boost::interprocess;
  10. using namespace boost::posix_time;
  11. using namespace boost;
  12. using namespace std;
  13. void ipcShutdown()
  14. {
  15. message_queue::remove("BitcoinURL");
  16. }
  17. void ipcThread(void* parg)
  18. {
  19. message_queue* mq = (message_queue*)parg;
  20. char strBuf[257];
  21. size_t nSize;
  22. unsigned int nPriority;
  23. loop
  24. {
  25. ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(100);
  26. if(mq->timed_receive(&strBuf, sizeof(strBuf), nSize, nPriority, d))
  27. {
  28. ThreadSafeHandleURL(std::string(strBuf, nSize));
  29. Sleep(1000);
  30. }
  31. if (fShutdown)
  32. {
  33. ipcShutdown();
  34. break;
  35. }
  36. }
  37. ipcShutdown();
  38. }
  39. void ipcInit()
  40. {
  41. #ifdef MAC_OSX
  42. // TODO: implement bitcoin: URI handling the Mac Way
  43. return;
  44. #endif
  45. #ifdef WIN32
  46. // TODO: THOROUGHLY test boost::interprocess fix,
  47. // and make sure there are no Windows argument-handling exploitable
  48. // problems.
  49. return;
  50. #endif
  51. message_queue* mq;
  52. char strBuf[257];
  53. size_t nSize;
  54. unsigned int nPriority;
  55. try {
  56. mq = new message_queue(open_or_create, "BitcoinURL", 2, 256);
  57. // Make sure we don't lose any bitcoin: URIs
  58. for (int i = 0; i < 2; i++)
  59. {
  60. ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(1);
  61. if(mq->timed_receive(&strBuf, sizeof(strBuf), nSize, nPriority, d))
  62. {
  63. ThreadSafeHandleURL(std::string(strBuf, nSize));
  64. }
  65. else
  66. break;
  67. }
  68. // Make sure only one bitcoin instance is listening
  69. message_queue::remove("BitcoinURL");
  70. mq = new message_queue(open_or_create, "BitcoinURL", 2, 256);
  71. }
  72. catch (interprocess_exception &ex) {
  73. return;
  74. }
  75. if (!CreateThread(ipcThread, mq))
  76. {
  77. delete mq;
  78. }
  79. }