Index: src/engine/main.cpp |
=================================================================== |
--- a/src/engine/main.cpp |
+++ b/src/engine/main.cpp |
@@ -66,33 +66,31 @@ namespace |
for (int32_t i = 0; i < length; i++) |
response << selectors[i]; |
} |
return response; |
} |
DWORD WINAPI ClientThread(LPVOID param) |
{ |
- HANDLE pipe = static_cast<HANDLE>(param); |
+ std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(param)); |
try |
{ |
- Communication::InputBuffer message = Communication::ReadMessage(pipe); |
+ Communication::InputBuffer message = pipe->ReadMessage(); |
Communication::OutputBuffer response = HandleRequest(message); |
- Communication::WriteMessage(pipe, response); |
+ pipe->WriteMessage(response); |
} |
catch (const std::exception& e) |
{ |
LogException(e); |
} |
// TODO: Keep the pipe open until the client disconnects |
- FlushFileBuffers(pipe); |
- DisconnectNamedPipe(pipe); |
- CloseHandle(pipe); |
+ |
Felix Dahlke
2013/06/03 12:18:47
Shouldn't we delete pipe here?
Wladimir Palant
2013/06/03 14:00:26
It's an auto_ptr.
Felix Dahlke
2013/06/04 06:39:39
Oops, then let's not delete it :)
|
return 0; |
} |
bool IsWindowsVistaOrLater() |
{ |
OSVERSIONINFOEX osvi; |
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); |
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
@@ -127,67 +125,44 @@ std::auto_ptr<AdblockPlus::FilterEngine> |
// TODO: Pass appInfo in, which should be sent by the client |
AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); |
std::string dataPath = ToUtf8String(GetAppDataPath()); |
dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get())->SetBasePath(dataPath); |
std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterEngine(jsEngine)); |
return filterEngine; |
} |
-HANDLE CreatePipe(const std::wstring& pipeName) |
-{ |
- SECURITY_ATTRIBUTES sa; |
- memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); |
- sa.nLength = sizeof(SECURITY_ATTRIBUTES); |
- |
- // Low mandatory label. See http://msdn.microsoft.com/en-us/library/bb625958.aspx |
- LPCWSTR accessControlEntry = L"S:(ML;;NW;;;LW)"; |
- PSECURITY_DESCRIPTOR securitydescriptor; |
- ConvertStringSecurityDescriptorToSecurityDescriptor(accessControlEntry, SDDL_REVISION_1, &securitydescriptor, 0); |
- |
- sa.lpSecurityDescriptor = securitydescriptor; |
- sa.bInheritHandle = TRUE; |
- |
- HANDLE pipe = CreateNamedPipe(pipeName.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, |
- PIPE_UNLIMITED_INSTANCES, Communication::bufferSize, Communication::bufferSize, 0, &sa); |
- LocalFree(securitydescriptor); |
- return pipe; |
-} |
- |
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) |
{ |
// TODO: Attempt to create the pipe first, and exit immediately if this |
// fails. Since multiple instances of the engine could be running, |
// this may need named mutices to avoid race conditions. |
// Note that as soon as the pipe is created first, we can reduce the |
// client timeout after CreateProcess(), but should increase the one |
// in WaitNamedPipe(). |
filterEngine = CreateFilterEngine(); |
for (;;) |
{ |
- HANDLE pipe = CreatePipe(Communication::pipeName); |
- if (pipe == INVALID_HANDLE_VALUE) |
+ try |
{ |
- LogLastError("CreateNamedPipe failed"); |
- return 1; |
+ Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeName, |
+ Communication::Pipe::MODE_CREATE); |
+ |
+ // TODO: Count established connections, kill the engine when none are left |
+ AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pipe), 0, 0)); |
+ if (!thread.get()) |
+ { |
+ delete pipe; |
+ LogLastError("CreateThread failed"); |
+ return 1; |
+ } |
} |
- |
- if (!ConnectNamedPipe(pipe, 0)) |
+ catch (std::runtime_error e) |
{ |
- LogLastError("Client failed to connect"); |
- CloseHandle(pipe); |
- continue; |
- } |
- |
- // TODO: Count established connections, kill the engine when none are left |
- |
- AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pipe), 0, 0)); |
- if (!thread.get()) |
- { |
- LogLastError("CreateThread failed"); |
+ LogException(e); |
return 1; |
} |
} |
return 0; |
} |