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: Thread safety Created Aug. 8, 2013, 1:45 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') | no next file with comments »
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
@@ -16,6 +16,8 @@
{
std::auto_ptr<AdblockPlus::FilterEngine> filterEngine;
std::auto_ptr<Updater> updater;
+ int activeConnections = 0;
+ CriticalSection activeConnectionsLock;
void WriteStrings(Communication::OutputBuffer& response,
const std::vector<std::string>& strings)
@@ -273,18 +275,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;
}
@@ -359,7 +391,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') | no next file with comments »

Powered by Google App Engine
This is Rietveld