/**
*
* \file Asura_GameSpy_HTTP.h
*
* Created by Richard May - 09/12/2003 12:17:35
*
* Copyright (C) 2002 Rebellion, All Rights Reserved.
*
* \brief
*
**/


#ifdef ASURA_USE_PRAGMA_ONCE
	#pragma once
#endif

#ifndef __ASURA_GAMESPY_HTTP_H__
#define __ASURA_GAMESPY_HTTP_H__

#ifdef ASURA_SUPPORT_GAMESPY
/************/
/* Includes.*/
/************/

// asura

// gamespy
#include "Gamespy/ghttp/ghttp.h"


/************/
/* Classes. */
/************/


/*************************************************************
 *
 * Class Name    : Asura_GameSpy_HTTP
 *
 * Author        : Richard May
 *
 * Description   : Asura wrapper for gamespy http SDK
 *
 *************************************************************/
class Asura_GameSpy_HTTP
{

public:
	// Public interface functions.
	static void Initialise(void);
	static void Shutdown(void);
	static void Reset(void);

	static void Update(void);

	// query functions - using GameSpy SDK functionality directly
	static GHTTPRequest Get( const char* szUrl, ghttpCompletedCallback pxCompletedCallback, void* pxData=NULL, char* szDataBuffer=NULL, int iBufferSize=0, ghttpProgressCallback pxProgressCallback=NULL );
	static GHTTPRequest GetAsync( const char* szUrl, ghttpCompletedCallback pxCompletedCallback, void* pxData=NULL, char* szDataBuffer=NULL, int iBufferSize=0, ghttpProgressCallback pxProgressCallback=NULL );
	
	// wrapped get function - calling code need not know about ghttp sdk at all
	// I'm currently only setting it up for 1 request at a time, but there's no
	// reason this system couldn't be used for multiple simultaneous requests by 
	// replacing the static data with an array of Asura_HTTP_Request class instances.
	static bool BeginDownload( const char* szUrl );
	static bool GetDownloadProgress( float* pfProgress );	// returns true if progress is known and puts amount of file retrieved from 0.0f - 1.0f in *pfProgress. If filesize is unknown, returns false and puts 0.0f in *pfProgress.
	static bool IsDownloadComplete( void );
	static bool HasErrorOccurred( void );
	static void CancelDownload();
	// once the download is complete, this system allocates enough memory for it.
	// This memory can be accessed using GetDownloadedData(). When you no longer 
	// need the data, call FreeDownloadedData.
	static char* GetDownloadedData( void );
	static int GetDownloadedDataSize( void );
	static void FreeDownloadedData( void );

private:
	// Data.
	static bool s_bInitialised;

	// callbacks to deal with current file get.
	static void ProgressCallback( GHTTPRequest iRequest, GHTTPState eState, const char* pcBuffer, GHTTPByteCount iBufferLen, GHTTPByteCount iBytesReceived, GHTTPByteCount iTotalSize, void* pUserData );
	static GHTTPBool CompletedCallback( GHTTPRequest iRequest, GHTTPResult eResult, char* pcBuffer, GHTTPByteCount iBufferLen, void* pUserData );

	// data to deal with current file get.
	static bool s_bGetInProgress;
	static float s_fDownloadProgress;
	static GHTTPResult s_eResult;
	static char* s_pcDownloadedData;
	static int s_iDownloadedDataSize;
	static GHTTPRequest s_iHandle;
};
/* end of class Asura_GameSpy_HTTP */

#endif //#ifdef ASURA_SUPPORT_GAMESPY
/****************/
/* End of file. */
/****************/
#endif//__ASURA_GAMESPY_HTTP_H__
