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

Unified Diff: src/shared/Communication.cpp

Issue 10824027: Implement better marshaling (Closed)
Patch Set: Created May 31, 2013, 8:26 a.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/Communication.h ('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
===================================================================
--- a/src/shared/Communication.cpp
+++ b/src/shared/Communication.cpp
@@ -1,12 +1,11 @@
-#include PRECOMPILED_HEADER_FILE
+#include "..\engine\stdafx.h"
#include <Lmcons.h>
-#include <sstream>
#include "Communication.h"
namespace
{
std::wstring GetUserName()
{
const DWORD maxLength = UNLEN + 1;
@@ -19,36 +18,17 @@ namespace
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 + '\0';
- 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, '\0'))
- strings.push_back(string);
- return strings;
-}
-
-std::string Communication::ReadMessage(HANDLE pipe)
+Communication::InputBuffer 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))
@@ -56,17 +36,18 @@ std::string Communication::ReadMessage(H
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();
+ return Communication::InputBuffer(stream.str());
}
-void Communication::WriteMessage(HANDLE pipe, const std::string& message)
+void Communication::WriteMessage(HANDLE pipe, Communication::OutputBuffer& message)
{
DWORD bytesWritten;
- if (!WriteFile(pipe, message.c_str(), message.length(), &bytesWritten, 0))
+ std::string data = message.Get();
+ if (!WriteFile(pipe, data.c_str(), data.length(), &bytesWritten, 0))
throw std::runtime_error("Failed to write to pipe");
}
« src/shared/Communication.h ('K') | « src/shared/Communication.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld