| OLD | NEW | 
|    1 #include <Windows.h> |    1 #include <Windows.h> | 
|    2 #include <Lmcons.h> |    2 #include <Lmcons.h> | 
|    3 #include <Sddl.h> |    3 #include <Sddl.h> | 
|    4  |    4  | 
|    5 #include "Communication.h" |    5 #include "Communication.h" | 
|    6  |    6  | 
|    7 namespace |    7 namespace | 
|    8 { |    8 { | 
 |    9   const int bufferSize = 1024; | 
 |   10  | 
|    9   std::string AppendErrorCode(const std::string& message) |   11   std::string AppendErrorCode(const std::string& message) | 
|   10   { |   12   { | 
|   11     std::stringstream stream; |   13     std::stringstream stream; | 
|   12     stream << message << " (Error code: " << GetLastError() << ")"; |   14     stream << message << " (Error code: " << GetLastError() << ")"; | 
|   13     return stream.str(); |   15     return stream.str(); | 
|   14   } |   16   } | 
|   15  |   17  | 
|   16   std::wstring GetUserName() |   18   std::wstring GetUserName() | 
|   17   { |   19   { | 
|   18     const DWORD maxLength = UNLEN + 1; |   20     const DWORD maxLength = UNLEN + 1; | 
|   19     std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]); |   21     std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]); | 
|   20     DWORD length = maxLength; |   22     DWORD length = maxLength; | 
|   21     if (!::GetUserNameW(buffer.get(), &length)) |   23     if (!::GetUserNameW(buffer.get(), &length)) | 
|   22       throw std::runtime_error(AppendErrorCode("Failed to get the current user's
      name")); |   24       throw std::runtime_error(AppendErrorCode("Failed to get the current user's
      name")); | 
|   23     return std::wstring(buffer.get(), length); |   25     return std::wstring(buffer.get(), length); | 
|   24   } |   26   } | 
|   25 } |   27 } | 
|   26  |   28  | 
|   27 const std::wstring Communication::pipeName = L"\\\\.\\pipe\\adblockplusengine_" 
     + GetUserName(); |   29 const std::wstring Communication::pipeName = L"\\\\.\\pipe\\adblockplusengine_" 
     + GetUserName(); | 
|   28  |   30  | 
 |   31 void Communication::InputBuffer::CheckType(Communication::ValueType expectedType
     ) | 
 |   32 { | 
 |   33   if (!hasType) | 
 |   34     ReadBinary(currentType); | 
 |   35  | 
 |   36   if (currentType != expectedType) | 
 |   37   { | 
 |   38     // Make sure we don't attempt to read the type again | 
 |   39     hasType = true; | 
 |   40     throw new std::runtime_error("Unexpected type found in input buffer"); | 
 |   41   } | 
 |   42   else | 
 |   43     hasType = false; | 
 |   44 } | 
 |   45  | 
|   29 Communication::PipeConnectionError::PipeConnectionError() |   46 Communication::PipeConnectionError::PipeConnectionError() | 
|   30     : std::runtime_error(AppendErrorCode("Unable to connect to a named pipe")) |   47     : std::runtime_error(AppendErrorCode("Unable to connect to a named pipe")) | 
|   31 { |   48 { | 
|   32 } |   49 } | 
|   33  |   50  | 
|   34 Communication::Pipe::Pipe(const std::wstring& pipeName, Communication::Pipe::Mod
     e mode) |   51 Communication::Pipe::Pipe(const std::wstring& pipeName, Communication::Pipe::Mod
     e mode) | 
|   35 { |   52 { | 
|   36   pipe = INVALID_HANDLE_VALUE; |   53   pipe = INVALID_HANDLE_VALUE; | 
|   37   if (mode == MODE_CREATE) |   54   if (mode == MODE_CREATE) | 
|   38   { |   55   { | 
|   39     SECURITY_ATTRIBUTES sa; |   56     SECURITY_ATTRIBUTES sa; | 
|   40     memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); |   57     memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); | 
|   41     sa.nLength = sizeof(SECURITY_ATTRIBUTES); |   58     sa.nLength = sizeof(SECURITY_ATTRIBUTES); | 
|   42  |   59  | 
|   43     // Low mandatory label. See http://msdn.microsoft.com/en-us/library/bb625958
     .aspx |   60     // Low mandatory label. See http://msdn.microsoft.com/en-us/library/bb625958
     .aspx | 
|   44     LPCWSTR accessControlEntry = L"S:(ML;;NW;;;LW)"; |   61     LPCWSTR accessControlEntry = L"S:(ML;;NW;;;LW)"; | 
|   45     PSECURITY_DESCRIPTOR securitydescriptor; |   62     PSECURITY_DESCRIPTOR securitydescriptor; | 
|   46     ConvertStringSecurityDescriptorToSecurityDescriptorW(accessControlEntry, SDD
     L_REVISION_1, &securitydescriptor, 0); |   63     ConvertStringSecurityDescriptorToSecurityDescriptorW(accessControlEntry, SDD
     L_REVISION_1, &securitydescriptor, 0); | 
|   47  |   64  | 
|   48     sa.lpSecurityDescriptor = securitydescriptor; |   65     sa.lpSecurityDescriptor = securitydescriptor; | 
|   49     sa.bInheritHandle = TRUE; |   66     sa.bInheritHandle = TRUE; | 
|   50  |   67  | 
|   51     pipe = CreateNamedPipeW (pipeName.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MES
     SAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, |   68     pipe = CreateNamedPipeW (pipeName.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MES
     SAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, | 
|   52                                   PIPE_UNLIMITED_INSTANCES, Communication::buffe
     rSize, Communication::bufferSize, 0, &sa); |   69                                   PIPE_UNLIMITED_INSTANCES, bufferSize, bufferSi
     ze, 0, &sa); | 
|   53     LocalFree(securitydescriptor); |   70     LocalFree(securitydescriptor); | 
|   54   } |   71   } | 
|   55   else |   72   else | 
|   56   { |   73   { | 
|   57     if (WaitNamedPipeW(pipeName.c_str(), 5000)) |   74     if (WaitNamedPipeW(pipeName.c_str(), 5000)) | 
|   58       pipe = CreateFileW(pipeName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, O
     PEN_EXISTING, 0, 0); |   75       pipe = CreateFileW(pipeName.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, O
     PEN_EXISTING, 0, 0); | 
|   59   } |   76   } | 
|   60  |   77  | 
|   61   if (pipe == INVALID_HANDLE_VALUE) |   78   if (pipe == INVALID_HANDLE_VALUE) | 
|   62     throw PipeConnectionError(); |   79     throw PipeConnectionError(); | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|   95   return Communication::InputBuffer(stream.str()); |  112   return Communication::InputBuffer(stream.str()); | 
|   96 } |  113 } | 
|   97  |  114  | 
|   98 void Communication::Pipe::WriteMessage(Communication::OutputBuffer& message) |  115 void Communication::Pipe::WriteMessage(Communication::OutputBuffer& message) | 
|   99 { |  116 { | 
|  100   DWORD bytesWritten; |  117   DWORD bytesWritten; | 
|  101   std::string data = message.Get(); |  118   std::string data = message.Get(); | 
|  102   if (!WriteFile(pipe, data.c_str(), data.length(), &bytesWritten, 0)) |  119   if (!WriteFile(pipe, data.c_str(), data.length(), &bytesWritten, 0)) | 
|  103     throw std::runtime_error("Failed to write to pipe"); |  120     throw std::runtime_error("Failed to write to pipe"); | 
|  104 } |  121 } | 
| OLD | NEW |