#ifndef DATARESPONSE_H
#define DATARESPONSE_H
// #include <standard library headers>
#include <vector>
// #include <other library headers>
// #include "customer headers"
#include "Socket.h"
/**
* description
*/
class DataResponse
{
public:
DataResponse();
virtual ~DataResponse();
public:
virtual const std::vector<char>& GetSendData(CommandHeader& header);
bool SetReceiveData(const sockaddr_in& addr,
const CommandHeader& header, const std::vector<char>& data);
virtual WORD GetCommandID() const = 0;
protected:
virtual void MakeSendData() = 0;
virtual bool PhaseReceiveData() = 0;
private:
bool CheckReceiveHeader();
protected:
sockaddr_in mRecvAddr;
CommandHeader mRecvHeader;
std::vector<char> mRecvData;
CommandHeader mSendHeader;
std::vector<char> mSendData;
private:
};
#endif
/
// #include <standard library headers>
#include <cassert>
// #include <other library headers>
// #include "customer headers"
#include "DataResponse.h"
/**
* description
*/
DataResponse::DataResponse()
{
}
/**
* description
*/
DataResponse::~DataResponse()
{
}
const std::vector<char>& DataResponse::GetSendData(CommandHeader& header)
{
MakeSendData();
memcpy(mSendHeader.CommandTag, SLAN_HEADER_TAG, 4);
mSendHeader.CommandVersion =HEADER_VERSION;
mSendHeader.CommandID = GetCommandID();
mSendHeader.RequestID = mRecvHeader.RequestID;
mSendHeader.RequestIP = mRecvHeader.RequestIP;
mSendHeader.DataLen = static_cast<DWORD>(mSendData.size());
header = mSendHeader;
return mSendData;
}
bool DataResponse::SetReceiveData(const sockaddr_in& addr,
const CommandHeader& header,
const std::vector<char>& data)
{
assert(header.DataLen == data.size());
mRecvAddr = addr;
mRecvHeader = header;
mRecvData = data;
return (CheckReceiveHeader() && PhaseReceiveData());
}
bool DataResponse::CheckReceiveHeader()
{
if (memcmp(mRecvHeader.CommandTag, HEADER_TAG, 4) != 0)
{
return false;
}
if (mRecvHeader.CommandVersion != HEADER_VERSION)
{
return false;
}
if (mRecvHeader.DataLen != mRecvData.size())
{
return false;
}
return true;
}