/*
  File: include/ioutils/Callback.h
  Description: interface for callback client and server
  Part of the Mantis project
  (c) 2002 Lycos Inc.
  
  This file is distributed for the sole purpose of allowing you to write
  software to interface with Sonique 2 and other Mantis programs. It is
  not to be modified or used for any other purpose without written
  authorization.

  If you redistribute this file, it must be in the original package
  complete with all other files provided, including the usage agreement.

  Do not remove this notice.
*/

#ifndef _CALLBACK_H__
#define _CALLBACK_H__


#ifdef DEBUG_INCLUDE_ORDER
#pragma message("Including " __FILE__ "\n" )
#endif


#include "List.h"
#include "typesport.h"
#include "thread.h"

class CallbackServer;

class CallbackClient
{
public:
	virtual uint32 OnCallback( uint32 msg, uint32 param1, uint32 param2 ) = 0;	// return 'true' if msg was handled.

	virtual ~CallbackClient() {};
};



class CallbackServer
{
protected:
	class CallbackMSG
	{
	public:
		CallbackClient* pCC;
		uint32			MSG,
						param1,
						param2;

		SignalObject*	pSO;

	};


protected:
	SignalObject					m_so;
	SignalObject					m_soSend;
	CriticalSectionObject			m_csMsg;
	ThreadObject					m_ThreadObject;
	List<CallbackMSG,CallbackMSG&>	m_MSGQueue;

	int		CallbackThreadProc();
	bool	bQuit;
	bool	bDelete;

public:
	CallbackServer();
	virtual ~CallbackServer();
	
	void	PostMessage( CallbackClient* pCC, uint32 msg, uint32 param1, uint32 param2, SignalObject* pDoneSO = NULL );
	void	SendMessage( CallbackClient* pCC, uint32 msg, uint32 param1, uint32 param2 );

	void	ReleaseServer() { bDelete = true; bQuit = true; };
	void	FlushMsgQueue();

	// friends

	friend int32 __stdcall CallbackThreadStart(void* param);
	
};


#endif
