/* -----------------------------------------------------------------------------
	GSFramework
	Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
----------------------------------------------------------------------------- */


#ifndef	__NETWORK_DEBUGGER_THREAD__
#define	__NETWORK_DEBUGGER_THREAD__


		#include "network_enet/enet_network.h"
		#include "async/async_call_queue.h"
		#include "thread/thread.h"
		#include "nstring/nstring.h"


namespace GS {
	namespace Script {

class NetworkDebugger;

/*!
	@short	Debugger network controller.
	@author	Emmanuel Julien (ejulien@gsworks.fr)
*/
class	NetworkDebuggerThread : public Threading::Thread, public Network::Enet
{
protected:

		String		address;
		int			port;

		NetworkDebugger &ctl;

private:

static	const int StateStopped = 0;
static	const int StateWaitingController = 1;
static	const int StateControllerConnected = 2;
static	const int StateStop = 3;

		Threading::Atomic32 connected, state;

		void		*ctl_peer;

virtual	void		OnPeerConnection(void *);
virtual	void		OnPacketReceived(void *, const void *, size_t);
virtual	void		OnConnectionClosed(void *);

virtual	bool		OpenServer(const char *address = "127.0.0.1", int port = 999);
virtual	bool		OpenClient(const char *address = "127.0.0.1", int port = 999) { return false; }

virtual	void		Execute();

public:

		String		GetControllerAddress();

		bool		IsConnected() const { return asbool(connected.Get()); }
		void		DisconnectController();

		void		SendToController(const String &);

		void		Stop();

		ASync::CallQueue async;

		NetworkDebuggerThread(NetworkDebugger &h, const char *address, int port);
		~NetworkDebuggerThread();
};

	} // Script
} // GS


#endif	// __NETWORK_DEBUGGER_THREAD__
