94 lines
2.6 KiB
C++
94 lines
2.6 KiB
C++
#ifndef PROTOCOL_HANDLER__H_
|
|
#define PROTOCOL_HANDLER__H_
|
|
|
|
#ifdef _WIN32
|
|
#pragma once
|
|
#endif
|
|
|
|
|
|
class HTransport;
|
|
|
|
|
|
class HProtocolHandler : public HObject
|
|
{
|
|
public:
|
|
HProtocolHandler() {}
|
|
~HProtocolHandler() {}
|
|
|
|
virtual inline void SetTransport(HTransport * pTransport);
|
|
virtual inline int SendToTransport(const char * pData, int nSize);
|
|
virtual inline void TerminateTransport();
|
|
|
|
virtual inline bool GetTransportInfo(const char * pName, void * pValue);
|
|
|
|
protected:
|
|
virtual void OnReceivedStatusFromTransport(int nStatus) = 0;
|
|
|
|
virtual int OnSendRetryToTransport() = 0;
|
|
virtual bool OnPrepareReceive() = 0;
|
|
virtual int OnReceivedFromTransport(const char * pData, int nSize) = 0;
|
|
|
|
protected:
|
|
HTransport * m_pTransport;
|
|
|
|
|
|
friend class HTransport;
|
|
};
|
|
|
|
///////////////////////////////////////
|
|
|
|
class HTransport : public HObject
|
|
{
|
|
public:
|
|
enum
|
|
{
|
|
T_BEGIN_TRANSPORT = 1,
|
|
T_END_TRANSPORT,
|
|
T_FAILED_BEGIN
|
|
};
|
|
|
|
HTransport() {}
|
|
~HTransport() { delete m_pHandler; }
|
|
|
|
virtual inline void AttachHandler(HProtocolHandler * pHandler);
|
|
virtual inline HProtocolHandler * GetHandler();
|
|
|
|
virtual inline bool GetTransportInfo(const char * pName, void * pValue) { return false; }
|
|
|
|
virtual inline void Terminate() {}
|
|
|
|
virtual int Send(const char * pData, int nSize) = 0;
|
|
|
|
protected:
|
|
inline void OnReceivedStatus(int nStatus);
|
|
inline bool OnPrepareReceive();
|
|
inline int OnReceived(const char * pData, int nSize);
|
|
inline int OnSendRetry();
|
|
|
|
protected:
|
|
HProtocolHandler * m_pHandler;
|
|
|
|
|
|
friend class HProtocolHandler;
|
|
};
|
|
|
|
inline void HProtocolHandler::SetTransport(HTransport * pTransport) { m_pTransport = pTransport; }
|
|
inline int HProtocolHandler::SendToTransport(const char * pData, int nSize) { return m_pTransport->Send(pData, nSize); }
|
|
inline void HProtocolHandler::TerminateTransport() { m_pTransport->Terminate(); }
|
|
inline bool HProtocolHandler::GetTransportInfo(const char * pName, void * pValue) { return m_pTransport->GetTransportInfo(pName, pValue); }
|
|
|
|
inline void HTransport::AttachHandler(HProtocolHandler * pHandler)
|
|
{
|
|
pHandler->SetTransport(this);
|
|
m_pHandler = pHandler;
|
|
}
|
|
inline HProtocolHandler * HTransport::GetHandler() { return m_pHandler; }
|
|
|
|
inline void HTransport::OnReceivedStatus(int nStatus) { m_pHandler->OnReceivedStatusFromTransport(nStatus); }
|
|
inline bool HTransport::OnPrepareReceive() { return m_pHandler->OnPrepareReceive(); }
|
|
inline int HTransport::OnReceived(const char * pData, int nSize) { return m_pHandler->OnReceivedFromTransport(pData, nSize); }
|
|
inline int HTransport::OnSendRetry() { return m_pHandler->OnSendRetryToTransport(); }
|
|
|
|
#endif // PROTOCOL_HANDLER__H_
|
|
|