Advertisement
yorath

pipe_client

Dec 6th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.06 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <string>
  3. #include <stdio.h>
  4. #include <boost/property_tree/ptree.hpp>
  5. #include <boost/property_tree/json_parser.hpp>
  6.  
  7. const LPWSTR kAdblockPipeName = L"\\\\.\\pipe\\adblock";
  8. const LPWSTR kServerPipeName = kAdblockPipeName;
  9. const DWORD kPipeTimeout = 5000;
  10. const DWORD kClientPipeTimeout = 2000;
  11.  
  12. #define SAFE_CLOSE_HANDLE(handle)                         \
  13.   if (handle != NULL && handle != INVALID_HANDLE_VALUE) { \
  14.     CloseHandle(handle);                                  \
  15.     handle = NULL;                                        \
  16.   }
  17.  
  18. #define SAFE_SET_EVENT(event)                           \
  19.   if (event != NULL && event != INVALID_HANDLE_VALUE) { \
  20.     SetEvent(event);                                    \
  21.   }
  22.  
  23. typedef enum _IPC_CMD {
  24.   UNKNOWN = 0,
  25.   ENABLE_ADBLOCK,
  26.   DISABLE_ADBLOCK,
  27.   ADD_DOMAIN_TO_EXCEPTION_LIST,
  28.   REMOVE_DOMAIN_FROM_EXCEPTION_LIST
  29. } IPC_CMD;
  30.  
  31. static void PRINT(const WCHAR* fmt, ...) {
  32.   WCHAR buffer[512] = {0};
  33.   va_list args;
  34.  
  35.   va_start(args, fmt);
  36.   _vsnwprintf(buffer, _countof(buffer), fmt, args);
  37.   va_end(args);
  38.  
  39.   OutputDebugStringW(buffer);
  40. }
  41.  
  42. static void PRINT(const CHAR* fmt, ...) {
  43.   CHAR buffer[512] = {0};
  44.   va_list args;
  45.  
  46.   va_start(args, fmt);
  47.   _vsnprintf(buffer, _countof(buffer), fmt, args);
  48.   va_end(args);
  49.  
  50.   OutputDebugStringA(buffer);
  51. }
  52.  
  53. class IPCClient {
  54.  public:
  55.   IPCClient();
  56.   ~IPCClient();
  57.   bool Init();
  58.   void Send(const std::wstring& msg);
  59.  
  60.  private:
  61.   HANDLE pipe_;
  62. };
  63.  
  64. IPCClient::IPCClient() : pipe_(NULL) {}
  65.  
  66. IPCClient::~IPCClient() { SAFE_CLOSE_HANDLE(pipe_); }
  67.  
  68. bool IPCClient::Init() {
  69.   while (true) {
  70.     pipe_ = CreateFileW(kServerPipeName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  71.                         OPEN_EXISTING, 0, NULL);
  72.     if (pipe_ != INVALID_HANDLE_VALUE) {
  73.       break;
  74.     }
  75.  
  76.     // Exit if an error other than ERROR_PIPE_BUSY occurs.
  77.     if (GetLastError() != ERROR_PIPE_BUSY) {
  78.       PRINT("Could not open pipe: 0x%08x\n", GetLastError());
  79.       return false;
  80.     }
  81.  
  82.     // All pipe instances are busy, so wait for 2 seconds.
  83.     if (!WaitNamedPipeW(kServerPipeName, kClientPipeTimeout)) {
  84.       PRINT("Could not open pipe: %d seconds wait timed out",
  85.             kClientPipeTimeout);
  86.       return false;
  87.     }
  88.   }
  89.   return true;
  90. }
  91.  
  92. void IPCClient::Send(const std::wstring& msg) {
  93.   DWORD bytes_written = 0;
  94.   if (!WriteFile(pipe_, msg.c_str(), sizeof(WCHAR) * (msg.size() + 1),
  95.                  &bytes_written, NULL)) {
  96.     PRINT("Write data to pipe failed: 0x%08x\n", GetLastError());
  97.   }
  98. }
  99.  
  100. int main() {
  101.   IPCClient ipc_client;
  102.   ipc_client.Init();
  103.  
  104.   {
  105.     boost::property_tree::wptree root;
  106.     std::wstringstream ss;
  107.     root.put<int>(L"cmd", int(DISABLE_ADBLOCK));
  108.     boost::property_tree::write_json(ss, root, false);
  109.     ipc_client.Send(ss.str());
  110.   }
  111.  
  112.   {
  113.     boost::property_tree::wptree root;
  114.     std::wstringstream ss;
  115.     root.put<int>(L"cmd", int(ENABLE_ADBLOCK));
  116.     boost::property_tree::write_json(ss, root, false);
  117.     ipc_client.Send(ss.str());
  118.   }
  119.  
  120.   return 0;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement