///////////////////////////////////////////////////////////////////////////////
// GameTalkProtocol.h
//
// Copyright (c) 2006 Electronic Arts Inc. All rights reserved.
// Created by Cyril Saint Girons
///////////////////////////////////////////////////////////////////////////////


#ifndef UTFSOCKETS_GAMETALKPROTOCOL_H
#define UTFSOCKETS_GAMETALKPROTOCOL_H

#include <GameTalk/IGameTalkProtocol.h>
#include <GameTalk/Utility.h>
#include <UTFSockets/Socket_imp.h>
#include "UTFSockets/Manager.h"

namespace EA
{
    namespace GameTalk
    {
        class GameTalkProtocol : public GameTalk::IGameTalkProtocol
        {
        public:
            // Constructor when you want the game to connect to the server
            GameTalkProtocol(const char* const ipAddress, const int port = DEFAULT_GAMETALK_PORT);

            // Constructor when you want the server to connect to the game
            GameTalkProtocol(const int port = DEFAULT_GAMETALK_PORT);

            virtual ~GameTalkProtocol();

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

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

			USE_GAMETALK_ALLOC("GameTalkProtocol")
		protected:
			virtual bool SetSocketBlocking(bool blocking);
			virtual void Disconnect();

            EA::Sockets::StreamSocket mListener;
            EA::Sockets::StreamSocket mConnection;

			// 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)
			int mReceiveBufferIndex;
			bool mDisconnected;
		private:
            GameTalkProtocol(const GameTalkProtocol& nc);
            GameTalkProtocol & operator=(const GameTalkProtocol& nc);
		};

    } // namespace GameTalk

} // namespace EA


#endif // Header include guard

