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