/*
    Copyright 2004-2006 Electronic Arts Inc.
*/

// --- Include guard

#ifndef EA_GAMETALK_GAMETALKPROTOCOL_H
#define EA_GAMETALK_GAMETALKPROTOCOL_H

// --- Includes
#include <GameTalk/IGameTalkProtocol.h>
#include <GameTalk/GameTalkBase.h>
#include <GameTalk/Utility.h>
#if defined(EA_PLATFORM_XENON)
#include <xtl.h>
#include <winsockx.h>
#else
#include <winsock2.h>
#endif

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

			virtual bool IsConnected();
            virtual bool Send(void* buffer, size_t msgSize);
            virtual int Poll();
			virtual bool Connect();

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

			USE_GAMETALK_ALLOC("GameTalkProtocol")
        private:
            GameTalkProtocol(const GameTalkProtocol& nc);
            GameTalkProtocol & operator=(const GameTalkProtocol& nc);
			bool Listen (void);
			void Disconnect();
			void InitSocket();
			bool SetSocketBlocking(bool blocking);

			SOCKET mSocket;
			SOCKET mListenerSocket;
			SOCKADDR_IN mRemote;
			bool mConnected;

			// The mReceiveBuffer is used to receive data "from the wire"
			char mReceiveBuffer[GAMETALK_RECEIVE_BUFFER_SIZE];
			// The mReceiveBufferIndex is used in case a user Poll's from within their callback (recurses on Poll)
			uint32_t mReceiveBufferIndex;
		};
	}
}

#endif  //  EA_GAMETALK_GAMETALKPROTOCOL_H

