///////////////////////////////////////////////////////////////////////////////
/// @file deNetMsg.cpp
///
/// @brief 
///
/// @author trajar
///
/// This file is the intellectual property of Novus Delta, LLC.. Usage of the
/// contents of this file is subject to the Destiny3D Member License which
/// can be found at http://www.destiny3d.com.  Any other usage is prohibited.
///
/// This file is distributed "AS IS" without warranty of any kind.  Novus
/// Delta, LLC. does not guarantee the fitness of the contents of this file
/// for any particular purpose.
///
/// Copyright (C) 2001-2003 Novus Delta, LLC. All Rights Reserved.
///
/// <hr>
///                                 Change History
/// <hr>
///
/// @date Jun 2002
/// @author trajar
/// @remarks Creation
///
///////////////////////////////////////////////////////////////////////////////

#include "deNetMsg.hpp"
#include "dePacket.hpp"
#include <WINDOWS.H>

#ifdef	_DEBUG
 #include	<crtdbg.h> 
 #define new new(_NORMAL_BLOCK,__FILE__, __LINE__)
#endif

_inline deNetMsg::deNetMsg(void)
{
	m_iMsgID	= MSG_INVALID_ID;
	m_DataSize	= 0;
	m_Data		= NULL;
}

_inline deNetMsg::deNetMsg( dePacket * pPacket )
{
	eNetError r = Init( pPacket );
	deASSERT( r == CODE_OK );
}

_inline deNetMsg::~deNetMsg(void)
{
	SAFE_DELETE_ARRAY( m_Data );
}

_inline deNetMsg& deNetMsg::operator = ( const deNetMsg& r )
{	
	// clear mem
	SAFE_DELETE_ARRAY( m_Data );

	m_iMsgID	= r.m_iMsgID;
	m_Sender	= r.m_Sender;
	m_Recip		= r.m_Recip;
	m_DataSize	= r.m_DataSize;

	if( m_DataSize > 0 )
	{
		m_Data = new BYTE[ m_DataSize ];
		memcpy( m_Data, r.m_Data, m_DataSize );
	}

	return *this;
}

eNetError deNetMsg::Init( dePacket * pPacket )
{
	WORD pMsgID = pPacket->GetHeader().NetMsgID;
	
	if( pMsgID != MSG_INVALID_ID )
	{
		// ensure the packet is the correct size
		deASSERT( pPacket->GetHeader().Size == pPacket->GetHeader().TotalSize );

		m_Sender	= pPacket->GetHeader().Sender;
		m_Recip		= pPacket->GetHeader().Recipient;
		m_DataSize	= pPacket->GetHeader().Size;

		if( m_DataSize > 0 )
		{
			m_Data	= new BYTE[ m_DataSize ];
			memcpy( m_Data, pPacket->GetData(), m_DataSize );
		}
		else
		{
			m_Data = NULL;
		}

		m_iMsgID	= pMsgID;

		return CODE_OK;
	}
	else
	{
		m_Data		= NULL;
		m_DataSize	= 0;
		m_Sender	= 0;
		m_Recip		= 0;
		m_iMsgID	= MSG_INVALID_ID;

		return CODE_INVALID_PARAM;
	}
}