Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/shared/Communication.cpp

Issue 10774005: Avoid duplication between AdblockPlus and AdblockPlusEngine (Closed)
Patch Set: Created May 24, 2013, 3:48 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« src/shared/AutoHandle.cpp ('K') | « src/shared/Communication.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include PRECOMPILED_HEADER_FILE
2
3 #include <Lmcons.h>
4 #include <sstream>
5
6 #include "Communication.h"
7
8 namespace
9 {
10 std::wstring GetUserName()
11 {
12 const DWORD maxLength = UNLEN + 1;
13 std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]);
14 DWORD length = maxLength;
15 if (!::GetUserName(buffer.get(), &length))
16 {
17 std::stringstream stream;
18 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");
20 }
21 return std::wstring(buffer.get(), length);
22 }
23 }
24
25 const std::wstring Communication::pipeName = L"\\\\.\\pipe\\adblockplusengine_" + GetUserName();
26
27 std::string Communication::MarshalStrings(const std::vector<std::string>& string s)
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 + ';';
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, ';'))
42 strings.push_back(string);
43 return strings;
44 }
45
46 std::string Communication::ReadMessage(HANDLE pipe)
47 {
48 std::stringstream stream;
49 std::auto_ptr<char> buffer(new char[bufferSize]);
50 bool doneReading = false;
51 while (!doneReading)
52 {
53 DWORD bytesRead;
54 if (ReadFile(pipe, buffer.get(), bufferSize * sizeof(char), &bytesRead, 0))
55 doneReading = true;
56 else if (GetLastError() != ERROR_MORE_DATA)
57 {
58 std::stringstream stream;
59 stream << "Error reading from pipe: " << GetLastError();
60 throw std::runtime_error(stream.str());
61 }
62 stream << std::string(buffer.get(), bytesRead);
63 }
64 return stream.str();
65 }
66
67 void Communication::WriteMessage(HANDLE pipe, const std::string& message)
68 {
69 DWORD bytesWritten;
70 if (!WriteFile(pipe, message.c_str(), message.length(), &bytesWritten, 0))
71 throw std::runtime_error("Failed to write to pipe");
72 }
OLDNEW
« src/shared/AutoHandle.cpp ('K') | « src/shared/Communication.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld