Left: | ||
Right: |
OLD | NEW |
---|---|
1 #include "stdafx.h" | 1 #include "stdafx.h" |
2 | 2 |
3 #include "../shared/AutoHandle.h" | 3 #include "../shared/AutoHandle.h" |
4 #include "../shared/Communication.h" | 4 #include "../shared/Communication.h" |
5 | 5 |
6 namespace | 6 namespace |
7 { | 7 { |
8 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; | 8 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; |
9 | 9 |
10 void Log(const std::string& message) | 10 void Log(const std::string& message) |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 int32_t length = selectors.size(); | 64 int32_t length = selectors.size(); |
65 response << length; | 65 response << length; |
66 for (int32_t i = 0; i < length; i++) | 66 for (int32_t i = 0; i < length; i++) |
67 response << selectors[i]; | 67 response << selectors[i]; |
68 } | 68 } |
69 return response; | 69 return response; |
70 } | 70 } |
71 | 71 |
72 DWORD WINAPI ClientThread(LPVOID param) | 72 DWORD WINAPI ClientThread(LPVOID param) |
73 { | 73 { |
74 HANDLE pipe = static_cast<HANDLE>(param); | 74 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa ram)); |
75 | 75 |
76 try | 76 try |
77 { | 77 { |
78 Communication::InputBuffer message = Communication::ReadMessage(pipe); | 78 Communication::InputBuffer message = pipe->ReadMessage(); |
79 Communication::OutputBuffer response = HandleRequest(message); | 79 Communication::OutputBuffer response = HandleRequest(message); |
80 Communication::WriteMessage(pipe, response); | 80 pipe->WriteMessage(response); |
81 } | 81 } |
82 catch (const std::exception& e) | 82 catch (const std::exception& e) |
83 { | 83 { |
84 LogException(e); | 84 LogException(e); |
85 } | 85 } |
86 | 86 |
87 // TODO: Keep the pipe open until the client disconnects | 87 // TODO: Keep the pipe open until the client disconnects |
88 FlushFileBuffers(pipe); | 88 |
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 :)
| |
89 DisconnectNamedPipe(pipe); | |
90 CloseHandle(pipe); | |
91 return 0; | 89 return 0; |
92 } | 90 } |
93 | 91 |
94 bool IsWindowsVistaOrLater() | 92 bool IsWindowsVistaOrLater() |
95 { | 93 { |
96 OSVERSIONINFOEX osvi; | 94 OSVERSIONINFOEX osvi; |
97 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); | 95 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); |
98 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); | 96 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
99 GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&osvi)); | 97 GetVersionEx(reinterpret_cast<LPOSVERSIONINFO>(&osvi)); |
100 return osvi.dwMajorVersion >= 6; | 98 return osvi.dwMajorVersion >= 6; |
(...skipping 24 matching lines...) Expand all Loading... | |
125 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine() | 123 std::auto_ptr<AdblockPlus::FilterEngine> CreateFilterEngine() |
126 { | 124 { |
127 // TODO: Pass appInfo in, which should be sent by the client | 125 // TODO: Pass appInfo in, which should be sent by the client |
128 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); | 126 AdblockPlus::JsEnginePtr jsEngine = AdblockPlus::JsEngine::New(); |
129 std::string dataPath = ToUtf8String(GetAppDataPath()); | 127 std::string dataPath = ToUtf8String(GetAppDataPath()); |
130 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath); | 128 dynamic_cast<AdblockPlus::DefaultFileSystem*>(jsEngine->GetFileSystem().get()) ->SetBasePath(dataPath); |
131 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine)); | 129 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine(new AdblockPlus::FilterE ngine(jsEngine)); |
132 return filterEngine; | 130 return filterEngine; |
133 } | 131 } |
134 | 132 |
135 HANDLE CreatePipe(const std::wstring& pipeName) | |
136 { | |
137 SECURITY_ATTRIBUTES sa; | |
138 memset(&sa, 0, sizeof(SECURITY_ATTRIBUTES)); | |
139 sa.nLength = sizeof(SECURITY_ATTRIBUTES); | |
140 | |
141 // Low mandatory label. See http://msdn.microsoft.com/en-us/library/bb625958.a spx | |
142 LPCWSTR accessControlEntry = L"S:(ML;;NW;;;LW)"; | |
143 PSECURITY_DESCRIPTOR securitydescriptor; | |
144 ConvertStringSecurityDescriptorToSecurityDescriptor(accessControlEntry, SDDL_R EVISION_1, &securitydescriptor, 0); | |
145 | |
146 sa.lpSecurityDescriptor = securitydescriptor; | |
147 sa.bInheritHandle = TRUE; | |
148 | |
149 HANDLE pipe = CreateNamedPipe(pipeName.c_str(), PIPE_ACCESS_DUPLEX, PIPE_TYPE_ MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT, | |
150 PIPE_UNLIMITED_INSTANCES, Communication::bufferS ize, Communication::bufferSize, 0, &sa); | |
151 LocalFree(securitydescriptor); | |
152 return pipe; | |
153 } | |
154 | |
155 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) | 133 int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) |
156 { | 134 { |
157 // TODO: Attempt to create the pipe first, and exit immediately if this | 135 // TODO: Attempt to create the pipe first, and exit immediately if this |
158 // fails. Since multiple instances of the engine could be running, | 136 // fails. Since multiple instances of the engine could be running, |
159 // this may need named mutices to avoid race conditions. | 137 // this may need named mutices to avoid race conditions. |
160 // Note that as soon as the pipe is created first, we can reduce the | 138 // Note that as soon as the pipe is created first, we can reduce the |
161 // client timeout after CreateProcess(), but should increase the one | 139 // client timeout after CreateProcess(), but should increase the one |
162 // in WaitNamedPipe(). | 140 // in WaitNamedPipe(). |
163 | 141 |
164 filterEngine = CreateFilterEngine(); | 142 filterEngine = CreateFilterEngine(); |
165 | 143 |
166 for (;;) | 144 for (;;) |
167 { | 145 { |
168 HANDLE pipe = CreatePipe(Communication::pipeName); | 146 try |
169 if (pipe == INVALID_HANDLE_VALUE) | |
170 { | 147 { |
171 LogLastError("CreateNamedPipe failed"); | 148 Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeNam e, |
172 return 1; | 149 Communication::Pipe::MODE_CREATE); |
150 | |
151 // TODO: Count established connections, kill the engine when none are left | |
152 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pip e), 0, 0)); | |
153 if (!thread.get()) | |
154 { | |
155 delete pipe; | |
156 LogLastError("CreateThread failed"); | |
157 return 1; | |
158 } | |
173 } | 159 } |
174 | 160 catch (std::runtime_error e) |
175 if (!ConnectNamedPipe(pipe, 0)) | |
176 { | 161 { |
177 LogLastError("Client failed to connect"); | 162 LogException(e); |
178 CloseHandle(pipe); | |
179 continue; | |
180 } | |
181 | |
182 // TODO: Count established connections, kill the engine when none are left | |
183 | |
184 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pipe) , 0, 0)); | |
185 if (!thread.get()) | |
186 { | |
187 LogLastError("CreateThread failed"); | |
188 return 1; | 163 return 1; |
189 } | 164 } |
190 } | 165 } |
191 | 166 |
192 return 0; | 167 return 0; |
193 } | 168 } |
OLD | NEW |