OLD | NEW |
1 #include PRECOMPILED_HEADER_FILE | 1 #include "..\engine\stdafx.h" |
2 | 2 |
3 #include <Lmcons.h> | 3 #include <Lmcons.h> |
4 #include <sstream> | |
5 | 4 |
6 #include "Communication.h" | 5 #include "Communication.h" |
7 | 6 |
8 namespace | 7 namespace |
9 { | 8 { |
10 std::wstring GetUserName() | 9 std::wstring GetUserName() |
11 { | 10 { |
12 const DWORD maxLength = UNLEN + 1; | 11 const DWORD maxLength = UNLEN + 1; |
13 std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]); | 12 std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]); |
14 DWORD length = maxLength; | 13 DWORD length = maxLength; |
15 if (!::GetUserName(buffer.get(), &length)) | 14 if (!::GetUserName(buffer.get(), &length)) |
16 { | 15 { |
17 std::stringstream stream; | 16 std::stringstream stream; |
18 stream << "Failed to get the current user's name (Error code: " << GetLast
Error() << ")"; | 17 stream << "Failed to get the current user's name (Error code: " << GetLast
Error() << ")"; |
19 throw std::runtime_error("Failed to get the current user's name"); | 18 throw std::runtime_error("Failed to get the current user's name"); |
20 } | 19 } |
21 return std::wstring(buffer.get(), length); | 20 return std::wstring(buffer.get(), length); |
22 } | 21 } |
23 } | 22 } |
24 | 23 |
25 const std::wstring Communication::pipeName = L"\\\\.\\pipe\\adblockplusengine_"
+ GetUserName(); | 24 const std::wstring Communication::pipeName = L"\\\\.\\pipe\\adblockplusengine_"
+ GetUserName(); |
26 | 25 |
27 std::string Communication::MarshalStrings(const std::vector<std::string>& string
s) | 26 Communication::InputBuffer Communication::ReadMessage(HANDLE pipe) |
28 { | |
29 // TODO: This is some pretty hacky marshalling, replace it with something more
robust | |
30 std::string marshalledStrings; | |
31 for (std::vector<std::string>::const_iterator it = strings.begin(); it != stri
ngs.end(); it++) | |
32 marshalledStrings += *it + '\0'; | |
33 return marshalledStrings; | |
34 } | |
35 | |
36 std::vector<std::string> Communication::UnmarshalStrings(const std::string& mess
age) | |
37 { | |
38 std::stringstream stream(message); | |
39 std::vector<std::string> strings; | |
40 std::string string; | |
41 while (std::getline(stream, string, '\0')) | |
42 strings.push_back(string); | |
43 return strings; | |
44 } | |
45 | |
46 std::string Communication::ReadMessage(HANDLE pipe) | |
47 { | 27 { |
48 std::stringstream stream; | 28 std::stringstream stream; |
49 std::auto_ptr<char> buffer(new char[bufferSize]); | 29 std::auto_ptr<char> buffer(new char[bufferSize]); |
50 bool doneReading = false; | 30 bool doneReading = false; |
51 while (!doneReading) | 31 while (!doneReading) |
52 { | 32 { |
53 DWORD bytesRead; | 33 DWORD bytesRead; |
54 if (ReadFile(pipe, buffer.get(), bufferSize * sizeof(char), &bytesRead, 0)) | 34 if (ReadFile(pipe, buffer.get(), bufferSize * sizeof(char), &bytesRead, 0)) |
55 doneReading = true; | 35 doneReading = true; |
56 else if (GetLastError() != ERROR_MORE_DATA) | 36 else if (GetLastError() != ERROR_MORE_DATA) |
57 { | 37 { |
58 std::stringstream stream; | 38 std::stringstream stream; |
59 stream << "Error reading from pipe: " << GetLastError(); | 39 stream << "Error reading from pipe: " << GetLastError(); |
60 throw std::runtime_error(stream.str()); | 40 throw std::runtime_error(stream.str()); |
61 } | 41 } |
62 stream << std::string(buffer.get(), bytesRead); | 42 stream << std::string(buffer.get(), bytesRead); |
63 } | 43 } |
64 return stream.str(); | 44 return Communication::InputBuffer(stream.str()); |
65 } | 45 } |
66 | 46 |
67 void Communication::WriteMessage(HANDLE pipe, const std::string& message) | 47 void Communication::WriteMessage(HANDLE pipe, Communication::OutputBuffer& messa
ge) |
68 { | 48 { |
69 DWORD bytesWritten; | 49 DWORD bytesWritten; |
70 if (!WriteFile(pipe, message.c_str(), message.length(), &bytesWritten, 0)) | 50 std::string data = message.Get(); |
| 51 if (!WriteFile(pipe, data.c_str(), data.length(), &bytesWritten, 0)) |
71 throw std::runtime_error("Failed to write to pipe"); | 52 throw std::runtime_error("Failed to write to pipe"); |
72 } | 53 } |
OLD | NEW |