/******************************************************************************/
/*!
    GameTalkProtocol
    Copyright 2005 Electronic Arts Inc.

    \file
    \ingroup    Debug
    \brief      GameTalk messaging over EasyTCP transport (DirtySock)
*/
/******************************************************************************/

/*** Include guard ************************************************************/

#ifndef GameTalkProtocol_H
#define GameTalkProtocol_H

/*** Includes *****************************************************************/

#include <GameTalk/IGameTalkProtocol.h>
#include "dirtydefs.h"
#include <dirtynet.h>
#include <dirtylib.h>
#include <netconn.h>
/*** Macros *******************************************************************/

/*** Interface ****************************************************************/

// --- Constants

// --- Forward declarations

namespace EasyTCP
{
    struct Event;
    class Connection;
}

namespace EA
{
	namespace GameTalk
	{
		class GameTalkProtocol : public EA::GameTalk::IGameTalkProtocol
		{
		public:
			GameTalkProtocol(const char *hostname, const uint32_t port = EA::GameTalk::IGameTalkProtocol::DEFAULT_GAMETALK_PORT);
			GameTalkProtocol(const uint32_t port = EA::GameTalk::IGameTalkProtocol::DEFAULT_GAMETALK_PORT);
			virtual ~GameTalkProtocol();

			virtual bool IsConnected();
			virtual bool Send(void *msg, size_t msgSize);
			virtual int Poll();
			virtual void RegisterReceiverCallback(GameTalkReceiverFnc callback, GameTalkInstance * gameTalkInstance);

			virtual bool Connect();

			static bool InitNetwork();
			static bool ShutdownNetwork();

			USE_GAMETALK_ALLOC("GameTalkProtocol")
		protected:
			void DigestReceivedData(uint8_t *receivedBytes, uint32_t numReceivedBytes);
			bool GetMessageSize(uint8_t *receivedBytes, uint32_t numReceivedBytes, uint32_t *msgSizeRequirements);

			void ReadFromBuffer();

			EasyTCP::Connection* mConnection;
        private:
            GameTalkProtocol(const GameTalkProtocol& nc);
            GameTalkProtocol & operator=(const GameTalkProtocol& nc);
		};
	}
}

#endif  //  GameTalkProtocol_H

