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

Unified Diff: src/engine/Main.cpp

Issue 11430025: Shut down the engine when the last tab is closed (Closed)
Patch Set: Created Aug. 7, 2013, 2:57 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
« no previous file with comments | « no previous file | src/plugin/AdblockPlusClient.h » ('j') | src/plugin/AdblockPlusClient.cpp » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/engine/Main.cpp
===================================================================
--- a/src/engine/Main.cpp
+++ b/src/engine/Main.cpp
@@ -15,6 +15,8 @@
namespace
{
std::auto_ptr<AdblockPlus::FilterEngine> filterEngine;
+ int activeConnections = 0;
+ CriticalSection activeConnectionsLock;
void WriteStrings(Communication::OutputBuffer& response,
const std::vector<std::string>& strings)
@@ -272,18 +274,48 @@
{
std::auto_ptr<Communication::Pipe> pipe(static_cast<Communication::Pipe*>(param));
- try
+ std::stringstream stream;
+ stream << GetCurrentThreadId();
+ std::string threadId = stream.str();
+ std::string threadString = "(Thread ID: " + threadId + ")";
+ Debug("Client connected " + threadString);
+
{
- Communication::InputBuffer message = pipe->ReadMessage();
- Communication::OutputBuffer response = HandleRequest(message);
- pipe->WriteMessage(response);
- }
- catch (const std::exception& e)
- {
- DebugException(e);
+ CriticalSection::Lock lock(activeConnectionsLock);
+ activeConnections++;
}
- // TODO: Keep the pipe open until the client disconnects
+ for (;;)
+ {
+ try
+ {
+ Communication::InputBuffer message = pipe->ReadMessage();
+ Communication::OutputBuffer response = HandleRequest(message);
+ pipe->WriteMessage(response);
+ }
+ catch (const Communication::PipeDisconnectedError&)
+ {
+ break;
+ }
+ catch (const std::exception& e)
+ {
+ DebugException(e);
+ break;
+ }
+ }
+
+ Debug("Client disconnected " + threadString);
+
+ {
+ CriticalSection::Lock lock(activeConnectionsLock);
+ activeConnections--;
+ if (activeConnections < 1)
+ {
+ Debug("No connections left, shutting down the engine");
+ activeConnections = 0;
+ exit(0);
+ }
+ }
return 0;
}
@@ -358,7 +390,6 @@
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)
{
« no previous file with comments | « no previous file | src/plugin/AdblockPlusClient.h » ('j') | src/plugin/AdblockPlusClient.cpp » ('J')

Powered by Google App Engine
This is Rietveld