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

Unified Diff: src/shared/Communication.cpp

Issue 10774005: Avoid duplication between AdblockPlus and AdblockPlusEngine (Closed)
Patch Set: Created May 24, 2013, 3:48 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« src/shared/AutoHandle.cpp ('K') | « src/shared/Communication.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/shared/Communication.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/src/shared/Communication.cpp
@@ -0,0 +1,72 @@
+#include PRECOMPILED_HEADER_FILE
+
+#include <Lmcons.h>
+#include <sstream>
+
+#include "Communication.h"
+
+namespace
+{
+ std::wstring GetUserName()
+ {
+ const DWORD maxLength = UNLEN + 1;
+ std::auto_ptr<wchar_t> buffer(new wchar_t[maxLength]);
+ DWORD length = maxLength;
+ if (!::GetUserName(buffer.get(), &length))
+ {
+ std::stringstream stream;
+ stream << "Failed to get the current user's name (Error code: " << GetLastError() << ")";
+ throw std::runtime_error("Failed to get the current user's name");
+ }
+ return std::wstring(buffer.get(), length);
+ }
+}
+
+const std::wstring Communication::pipeName = L"\\\\.\\pipe\\adblockplusengine_" + GetUserName();
+
+std::string Communication::MarshalStrings(const std::vector<std::string>& strings)
+{
+ // TODO: This is some pretty hacky marshalling, replace it with something more robust
+ std::string marshalledStrings;
+ for (std::vector<std::string>::const_iterator it = strings.begin(); it != strings.end(); it++)
+ marshalledStrings += *it + ';';
+ return marshalledStrings;
+}
+
+std::vector<std::string> Communication::UnmarshalStrings(const std::string& message)
+{
+ std::stringstream stream(message);
+ std::vector<std::string> strings;
+ std::string string;
+ while (std::getline(stream, string, ';'))
+ strings.push_back(string);
+ return strings;
+}
+
+std::string Communication::ReadMessage(HANDLE pipe)
+{
+ std::stringstream stream;
+ std::auto_ptr<char> buffer(new char[bufferSize]);
+ bool doneReading = false;
+ while (!doneReading)
+ {
+ DWORD bytesRead;
+ if (ReadFile(pipe, buffer.get(), bufferSize * sizeof(char), &bytesRead, 0))
+ doneReading = true;
+ else if (GetLastError() != ERROR_MORE_DATA)
+ {
+ std::stringstream stream;
+ stream << "Error reading from pipe: " << GetLastError();
+ throw std::runtime_error(stream.str());
+ }
+ stream << std::string(buffer.get(), bytesRead);
+ }
+ return stream.str();
+}
+
+void Communication::WriteMessage(HANDLE pipe, const std::string& message)
+{
+ DWORD bytesWritten;
+ if (!WriteFile(pipe, message.c_str(), message.length(), &bytesWritten, 0))
+ throw std::runtime_error("Failed to write to pipe");
+}
« 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