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) |
{ |