#pragma once
#include "clientcontext.h"
#include <list>
#include "message.h"

class HouseKeeper
{
public:
	HouseKeeper();
	~HouseKeeper();
	
	void Start();

	// Send messages to the client	
	void PostMessage(Message& msg); // creates a copy of the message
	int PostMessageAndWait(Message& msg, int timeout); // 

	// tell the message pump to quit
	void Die();

private:
	void IdleProcess(); // this gets called every PingPeriod ms to perform house-keeping operations
	Vector<int> ActiveEvents;

	static DWORD WINAPI StartMessagePump(LPVOID p);

	///// Elements of the Message Pump
	HANDLE hThread; // the identifier of this message pump's thread
	std::list<Message*> Q; // the message queue
	int MessagePump(); // the message pump for this context (runs in its own thread)
	int dead;
	HANDLE MessageNotify; // the message notify event
	int PingPeriod; // the rate at which we ping the client for his connection state

	CRITICAL_SECTION CriticalSection; 
};

extern HouseKeeper hk;
