//
// Asura_DemonWare_PacketInterceptor.h
//
// Created by Andrew Catlender - 31 January 2006 16:35:20
//
// Copyright (C) 2006 Rebellion (Derby), All Rights Reserved.
//
//
// Wrapper class to allow integration of DemonWare's Matchmaking+ SDK
//

#if defined(ASURA_USE_PRAGMA_ONCE)
#pragma once
#endif

#ifndef __ASURA_DEMONWARE_PACKETINTERCEPTOR_H__
#define __ASURA_DEMONWARE_PACKETINTERCEPTOR_H__

#if defined(ASURA_SUPPORT_EXTERNAL_NETWORK) && defined (ASURA_SUPPORT_DEMONWARE)
#include "DemonWare/bdSocket/bdPacketInterceptor.h"

// Forward declaration
//class bdPacketInterceptor;
class Asura_NetSocket;
struct Asura_NetSocket_MessageInfo;

#define ASURA_DEMONWARE_PACKET_BUFFER_SIZE (16 /* Max clients */ * 10 /* Num updates per second */ * 1 /* Max messages per update */)

//
//	Asura_DemonWare_PacketInterceptor
//
class Asura_DemonWare_PacketInterceptor : public bdPacketInterceptor
{
public:
	BD_DECLARE_NEW_AND_DELETE_OPERATORS

	Asura_DemonWare_PacketInterceptor(void) : m_uiHead(0), m_uiTail(0), m_uiUsedSlots(0), m_uiHighWaterMark(0) {};
	~Asura_DemonWare_PacketInterceptor(void);

	// Determines if the packet is DemonWare or game traffic
	virtual bdBool acceptPacket(bdDefaultSocket *xSocket, bdAddr xAddr, void *pData, const bdUInt uiSize, const bdUByte8 type );
	bool sendPacket(const Asura_NetSocket_MessageInfo* const pxMessageInfo);

	// Buffering the incoming packets
	void AddPacket(void* pPacket, u_int uiLength, Asura_NetAddress xAddress, unsigned short int usPort);
	bool ArePacketsPending(void);
	bool HandleNextPacket(Asura_NetSocket* pxAsuraSocket);

#if defined(_DEBUG)
	u_int GetHighWaterMark(void);
#endif

protected:
	struct Packet
	{
		Asura_NetAddress m_xAddress;
		unsigned short int m_usPort;
		unsigned long int m_uiLength;
		char m_acPayload[BD_MAX_DATAGRAM_SIZE];
	};

	Packet m_xBuffer[ASURA_DEMONWARE_PACKET_BUFFER_SIZE];

	u_int m_uiHead;
	u_int m_uiTail;

	u_int m_uiUsedSlots;
	u_int m_uiHighWaterMark;

private:
};

#endif // ASURA_SUPPORT_EXTERNAL_NETWORK && ASURA_SUPPORT_DEMONWARE

#endif // __ASURA_DEMONWARE_PACKETINTERCEPTOR_H__