You.i Engine
CYIDownloadHelper Class Reference

Detailed Description

This class can be used to simplify the process of downloading data off of a web server. Before using this helper the CYIHTTPService must be started using CYIHTTPService::Start.

Downloads can be requested using a URL StartDownload(const CYIUrl &) where a network request will be internally instanciated and passed on to CYIHTTPService, or via a custom request using StartDownload(const std::shared_ptr<CYIHTTPRequest> &). In both cases, signals fired will use the URL (either supplied) or extracted (

See also
CYIHTTPRequest::GetURL()).

Starting a new download will cancel any download in progress only if it is for a different target URL (as defined above). Starting a download for same target URL as the one in progress will be ignored.

See also
CYIHTTPService

#include <network/YiDownloadHelper.h>

Inheritance diagram for CYIDownloadHelper:

Public Types

enum  DownloadState {
  DownloadState::Idle = 0,
  DownloadState::Downloading
}
 
enum  FailureType {
  FailureType::InvalidURL = 0,
  FailureType::NetworkError
}
 

Public Member Functions

 CYIDownloadHelper ()
 
 CYIDownloadHelper (IYINetworkService *pNetwork)
 
virtual ~CYIDownloadHelper ()
 
const CYIUrlGetUrl () const
 
void StartDownload (const CYIUrl &url)
 
void StartDownload (const std::shared_ptr< CYIHTTPRequest > &request)
 
void CancelDownload ()
 
DownloadState GetDownloadState () const
 
const std::shared_ptr< CYIHTTPResponse > & GetResponse () const
 
void ClearResponse ()
 
- Public Member Functions inherited from CYISignalHandler
 CYISignalHandler ()
 
 CYISignalHandler (const CYISignalHandler &rSignalHandler)
 
virtual ~CYISignalHandler ()
 
CYISignalHandleroperator= (const CYISignalHandler &rSignalHandler)
 
void MoveToThread (CYIThread *pThread)
 This function allows the user to override the default thread affinity to any CYIThread that may or may not be running. More...
 
CYIThreadHandle GetThreadAffinity () const
 
void SetThreadAffinity (const CYIThreadHandle &rThreadAffinity)
 
virtual bool IsConnected () const
 
virtual bool IsConnected (const CYISignalBase &rSignal) const
 
void Disconnect (CYISignalBase &rSignal)
 
void DisconnectFromAllSignals ()
 
- Public Member Functions inherited from CYIThread::Listener
 Listener ()
 
virtual ~Listener ()
 
virtual void OnThreadStarted (CYIThread *)
 
virtual void OnThreadTerminated (CYIThread *)
 
virtual void OnThreadFinished (CYIThread *)
 

Public Attributes

CYISignal< FailureType, const CYIUrl &> DownloadFailed
 
CYISignal< const CYIUrl &> DownloadCancelled
 
CYISignal< const CYIUrl &> DownloadComplete
 

Protected Member Functions

virtual void OnRequestCompleted (const std::shared_ptr< CYIHTTPRequest > &, const std::shared_ptr< CYIHTTPResponse > &, bool)
 
virtual void OnRequestFailed (const std::shared_ptr< CYIHTTPRequest > &pRequest)
 
virtual void CancelDownloadImplementation ()
 
virtual void StartDownloadImpl ()
 

Protected Attributes

DownloadState m_state
 
std::shared_ptr< CYIHTTPRequestm_pCurrentRequest
 
std::shared_ptr< CYIHTTPResponsem_pCurrentResponse
 
IYINetworkServicem_pNetworkService
 
std::recursive_mutex m_stateMutex
 

Member Enumeration Documentation

◆ DownloadState

Represents the states the download helper can be in

Enumerator
Idle 

The download helper is not downloading data.

Downloading 

The download helper is downloading data.

◆ FailureType

Represents the different types of failures that can occur while downloading data

Enumerator
InvalidURL 

The url provided is empty.

NetworkError 

An error occurred while downloading data.

Constructor & Destructor Documentation

◆ CYIDownloadHelper() [1/2]

CYIDownloadHelper::CYIDownloadHelper ( )

Constructs a default CYIDownloadHelper.

Note
A url must be set using SetUrl before a download can be succesfully started.

◆ CYIDownloadHelper() [2/2]

CYIDownloadHelper::CYIDownloadHelper ( IYINetworkService pNetwork)

◆ ~CYIDownloadHelper()

virtual CYIDownloadHelper::~CYIDownloadHelper ( )
virtual

Member Function Documentation

◆ CancelDownload()

void CYIDownloadHelper::CancelDownload ( )

Cancels the download of the current URL.

◆ CancelDownloadImplementation()

virtual void CYIDownloadHelper::CancelDownloadImplementation ( )
protectedvirtual

Called when a download is being cancelled. Implementations can assume that m_stateMutex is locked and m_state is DOWNLOADING.

Warning
If this function is overridden, CancelDownload() must be called in the subclass' destructor.
See also
CancelDownload

Reimplemented in CYIAssetDownloadHelper.

◆ ClearResponse()

void CYIDownloadHelper::ClearResponse ( )

Clears the response object (freeing memory) held in the class.

◆ GetDownloadState()

DownloadState CYIDownloadHelper::GetDownloadState ( ) const

Returns the current download state of the helper.

◆ GetResponse()

const std::shared_ptr<CYIHTTPResponse>& CYIDownloadHelper::GetResponse ( ) const

Returns the response received for the last download request. This is available after the completion callbacks are received.

◆ GetUrl()

const CYIUrl& CYIDownloadHelper::GetUrl ( ) const

Returns the current URL.

◆ OnRequestCompleted()

virtual void CYIDownloadHelper::OnRequestCompleted ( const std::shared_ptr< CYIHTTPRequest > &  ,
const std::shared_ptr< CYIHTTPResponse > &  ,
bool   
)
protectedvirtual

Called when the request has completed.

Reimplemented in CYIAssetDownloadHelper.

◆ OnRequestFailed()

virtual void CYIDownloadHelper::OnRequestFailed ( const std::shared_ptr< CYIHTTPRequest > &  pRequest)
protectedvirtual

Called when the download fails. Emits the CYIDownloadHelper::DownloadFailed signal.

◆ StartDownload() [1/2]

void CYIDownloadHelper::StartDownload ( const CYIUrl url)

Starts the download with url as the URL.

◆ StartDownload() [2/2]

void CYIDownloadHelper::StartDownload ( const std::shared_ptr< CYIHTTPRequest > &  request)

Starts the download with a custom request.

◆ StartDownloadImpl()

virtual void CYIDownloadHelper::StartDownloadImpl ( )
protectedvirtual

Starts the download via CYIHTTPService based on the the m_pCurrentResponse built in StartDownload() APIs.

Reimplemented in CYIAssetDownloadHelper.

Member Data Documentation

◆ DownloadCancelled

CYISignal<const CYIUrl & > CYIDownloadHelper::DownloadCancelled

Signals that the download has been cancelled and provides the URL of the cancelled download.

◆ DownloadComplete

CYISignal<const CYIUrl & > CYIDownloadHelper::DownloadComplete

Signals that the download has completed.

◆ DownloadFailed

CYISignal<FailureType, const CYIUrl & > CYIDownloadHelper::DownloadFailed

Signals that the download has failed and provides the URL which the download failure occured along with the cause of failure.

◆ m_pCurrentRequest

std::shared_ptr<CYIHTTPRequest> CYIDownloadHelper::m_pCurrentRequest
protected

◆ m_pCurrentResponse

std::shared_ptr<CYIHTTPResponse> CYIDownloadHelper::m_pCurrentResponse
protected

◆ m_pNetworkService

IYINetworkService* CYIDownloadHelper::m_pNetworkService
protected

◆ m_state

DownloadState CYIDownloadHelper::m_state
protected

◆ m_stateMutex

std::recursive_mutex CYIDownloadHelper::m_stateMutex
mutableprotected

The documentation for this class was generated from the following file: