/*
	Lan Wars.
	Copyright (c) 2009, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef GAME_PEER_H
#define GAME_PEER_H

//=====================================================================================================================
//=====================================================================================================================

#include "Network/NetworkAddress.h"

//=====================================================================================================================
//=====================================================================================================================

namespace Game
{
	//=================================================================================================================
	//=================================================================================================================
	
	class GameObjectBase;
	class GameSession;
	class GamePacket;

	//=================================================================================================================
	/// @brief		Represents one participant in the networked architecture of the game. This is either a client or
	///				a server. This basically encapuslates the network address of the peer as well as holding onto 
	///				which objects the peer owns.
	//=================================================================================================================

	class GamePeer
	{
		public:

			//=========================================================================================================
			// Structures
			//=========================================================================================================

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Server only used structure. This holds a notification of an object creation/deletion that we have
				/// either not sent yet or that we not received an acknowledgement for yet.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				struct CreateDeleteRequest
				{
					/// Object involved in the notify

					GameObjectBase * m_object;

					/// Time until the request is re-sent

					int m_time_till_retransmission;
				};

			//=========================================================================================================
			// Functions
			//=========================================================================================================
			
				/// Creates the peer description:
				
				GamePeer( const NetworkAddress & addr , GameSession * session , unsigned peer_id = 0 );
				
				/// Destroys the peer description:
				
				~GamePeer();
				
				/// Returns the network address of the peer
				
				NetworkAddress GetAddress() const { return m_address; }
				
				/// Sets which game object the peer 'owns'. This should always be null in the case of a server
				
				void SetControlObject( GameObjectBase * object );
				
				/// Gets which game object the peer 'owns'. This should always be null in the case of a server
				
				inline GameObjectBase * GetControlObject(){ return m_control_object; }
				
				/// Sets the time since the last packet was sent to the peer
				
				inline void SetLastSendTime( unsigned ms ){ m_last_send_time = ms; }
								
				/// Sets the time since the last packet was sent to the peer as the current time
				
				inline void SetLastSendTime(){ m_last_send_time = GetTickCount(); }
				
				/// Gets the time since the last packet was sent to the peer
				
				inline unsigned GetLastSendTime() const { return m_last_send_time; }
				
				/// Sets the time since we last received a packet from the peer
				
				inline void SetLastReceiveTime( unsigned ms ){ m_last_recv_time = ms; }
				
				/// Sets the time since we last received a packet from the peer as the current time
				
				inline void SetLastReceiveTime(){ m_last_recv_time = GetTickCount(); }
				
				/// Gets the time since we last received a packet from the peer
				
				inline unsigned GetLastReceiveTime() const { return m_last_recv_time; }

				/// Returns the peer id of the peer

				inline unsigned GetPeerId() const { return m_peer_id; }
				
				/// Tells if we should send a packet to the peer to keep the connection alive.
				
				bool ShouldSendKeepalive() const;
				
				/// Tells if the connection to the peer has timed out
				
				bool HasConnectionTimedOut() const;

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Server only function: makes a pending request to the client that the object be deleted on the 
				/// client. This request is sent at a later stage.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				void AddDeleteRequest( GameObjectBase & object );

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Server only function: Acknowledges receipt of a delete request on the client, thus stops any 
				/// retransmissions of that request to the client after the timeout has elapsed.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				void AcknowledgeDeleteRequest( unsigned net_id );

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Server only function: makes a pending request to the client that the object be created on the 
				/// client. This request is sent at a later stage.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				void AddCreateRequest( GameObjectBase & object );

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Server only function: Acknowledges receipt of a create request on the client, thus stops any 
				/// retransmissions of that request to the client after the timeout has elapsed.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				void AcknowledgeCreateRequest( unsigned net_id );

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Transmits any create object requests that need to be transmitted to the client and puts
				/// them into the given packet.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				void SendCreateRequests( GamePacket & p );

				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
				/// Transmits any delete object requests that need to be transmitted to the client and puts
				/// them into the given packet.
				//'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

				void SendDeleteRequests( GamePacket & p );

		private:
		
			//=========================================================================================================
			// Variables
			//=========================================================================================================
			
				/// This is the network address of the peer
				
				NetworkAddress m_address;
				
				/// This is the object the peer controls
				
				GameObjectBase * m_control_object;
				
				/// This is the game session object for the game
				
				GameSession * m_session;
				
				/// This is the time we last sent a packet to the peer
				
				unsigned m_last_send_time;
				
				/// This is the time we last received a packet from the peer
				
				unsigned m_last_recv_time;

				/// This is the id of the peer. This will be 0 for a server.

				unsigned m_peer_id;

				/// Server used only: a list of objects that we must send or re-transmit create object requests to the client for

				std::list<CreateDeleteRequest> m_pending_delete_requests;

				/// Server used only: a list of objects that we must send or re-transmit create object requests to the client for

				std::list<CreateDeleteRequest> m_pending_create_requests;
	};
}

#endif
