| OLD | NEW |
| 1 #include <AdblockPlus.h> | 1 #include <AdblockPlus.h> |
| 2 #include <functional> | 2 #include <functional> |
| 3 #include <vector> | 3 #include <vector> |
| 4 #include <Windows.h> | 4 #include <Windows.h> |
| 5 | 5 |
| 6 #include "../shared/AutoHandle.h" | 6 #include "../shared/AutoHandle.h" |
| 7 #include "../shared/Communication.h" | 7 #include "../shared/Communication.h" |
| 8 #include "../shared/Dictionary.h" | 8 #include "../shared/Dictionary.h" |
| 9 #include "../shared/Utils.h" | 9 #include "../shared/Utils.h" |
| 10 #include "../shared/Version.h" | 10 #include "../shared/Version.h" |
| 11 #include "../shared/CriticalSection.h" | 11 #include "../shared/CriticalSection.h" |
| 12 #include "Debug.h" | 12 #include "Debug.h" |
| 13 #include "Updater.h" | 13 #include "Updater.h" |
| 14 | 14 |
| 15 namespace | 15 namespace |
| 16 { | 16 { |
| 17 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; | 17 std::auto_ptr<AdblockPlus::FilterEngine> filterEngine; |
| 18 std::auto_ptr<Updater> updater; | 18 std::auto_ptr<Updater> updater; |
| 19 int activeConnections = 0; |
| 20 CriticalSection activeConnectionsLock; |
| 19 | 21 |
| 20 void WriteStrings(Communication::OutputBuffer& response, | 22 void WriteStrings(Communication::OutputBuffer& response, |
| 21 const std::vector<std::string>& strings) | 23 const std::vector<std::string>& strings) |
| 22 { | 24 { |
| 23 int32_t count = strings.size(); | 25 int32_t count = strings.size(); |
| 24 response << count; | 26 response << count; |
| 25 for (int32_t i = 0; i < count; i++) | 27 for (int32_t i = 0; i < count; i++) |
| 26 response << strings[i]; | 28 response << strings[i]; |
| 27 } | 29 } |
| 28 | 30 |
| (...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 266 } | 268 } |
| 267 | 269 |
| 268 } | 270 } |
| 269 return response; | 271 return response; |
| 270 } | 272 } |
| 271 | 273 |
| 272 DWORD WINAPI ClientThread(LPVOID param) | 274 DWORD WINAPI ClientThread(LPVOID param) |
| 273 { | 275 { |
| 274 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa
ram)); | 276 std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(pa
ram)); |
| 275 | 277 |
| 276 try | 278 std::stringstream stream; |
| 279 stream << GetCurrentThreadId(); |
| 280 std::string threadId = stream.str(); |
| 281 std::string threadString = "(Thread ID: " + threadId + ")"; |
| 282 Debug("Client connected " + threadString); |
| 283 |
| 277 { | 284 { |
| 278 Communication::InputBuffer message = pipe->ReadMessage(); | 285 CriticalSection::Lock lock(activeConnectionsLock); |
| 279 Communication::OutputBuffer response = HandleRequest(message); | 286 activeConnections++; |
| 280 pipe->WriteMessage(response); | |
| 281 } | |
| 282 catch (const std::exception& e) | |
| 283 { | |
| 284 DebugException(e); | |
| 285 } | 287 } |
| 286 | 288 |
| 287 // TODO: Keep the pipe open until the client disconnects | 289 for (;;) |
| 290 { |
| 291 try |
| 292 { |
| 293 Communication::InputBuffer message = pipe->ReadMessage(); |
| 294 Communication::OutputBuffer response = HandleRequest(message); |
| 295 pipe->WriteMessage(response); |
| 296 } |
| 297 catch (const Communication::PipeDisconnectedError&) |
| 298 { |
| 299 break; |
| 300 } |
| 301 catch (const std::exception& e) |
| 302 { |
| 303 DebugException(e); |
| 304 break; |
| 305 } |
| 306 } |
| 307 |
| 308 Debug("Client disconnected " + threadString); |
| 309 |
| 310 { |
| 311 CriticalSection::Lock lock(activeConnectionsLock); |
| 312 activeConnections--; |
| 313 if (activeConnections < 1) |
| 314 { |
| 315 Debug("No connections left, shutting down the engine"); |
| 316 activeConnections = 0; |
| 317 exit(0); |
| 318 } |
| 319 } |
| 288 | 320 |
| 289 return 0; | 321 return 0; |
| 290 } | 322 } |
| 291 | 323 |
| 292 void OnUpdateAvailable(AdblockPlus::JsValueList& params) | 324 void OnUpdateAvailable(AdblockPlus::JsValueList& params) |
| 293 { | 325 { |
| 294 updateAvailable = true; | 326 updateAvailable = true; |
| 295 if (params.size() < 1) | 327 if (params.size() < 1) |
| 296 { | 328 { |
| 297 Debug("updateAvailable event missing URL"); | 329 Debug("updateAvailable event missing URL"); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 352 filterEngine = CreateFilterEngine(locale); | 384 filterEngine = CreateFilterEngine(locale); |
| 353 updater.reset(new Updater(filterEngine->GetJsEngine())); | 385 updater.reset(new Updater(filterEngine->GetJsEngine())); |
| 354 | 386 |
| 355 for (;;) | 387 for (;;) |
| 356 { | 388 { |
| 357 try | 389 try |
| 358 { | 390 { |
| 359 Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeNam
e, | 391 Communication::Pipe* pipe = new Communication::Pipe(Communication::pipeNam
e, |
| 360 Communication::Pipe::MODE_CREATE); | 392 Communication::Pipe::MODE_CREATE); |
| 361 | 393 |
| 362 // TODO: Count established connections, kill the engine when none are left | |
| 363 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pip
e), 0, 0)); | 394 AutoHandle thread(CreateThread(0, 0, ClientThread, static_cast<LPVOID>(pip
e), 0, 0)); |
| 364 if (!thread) | 395 if (!thread) |
| 365 { | 396 { |
| 366 delete pipe; | 397 delete pipe; |
| 367 DebugLastError("CreateThread failed"); | 398 DebugLastError("CreateThread failed"); |
| 368 return 1; | 399 return 1; |
| 369 } | 400 } |
| 370 } | 401 } |
| 371 catch (std::runtime_error e) | 402 catch (std::runtime_error e) |
| 372 { | 403 { |
| 373 DebugException(e); | 404 DebugException(e); |
| 374 return 1; | 405 return 1; |
| 375 } | 406 } |
| 376 } | 407 } |
| 377 | 408 |
| 378 return 0; | 409 return 0; |
| 379 } | 410 } |
| OLD | NEW |