#ifndef DATAREQUEST_H
#define DATAREQUEST_H
// #include <standard library headers>
#include <string>
#include <vector>
// #include <other library headers>
// #include "customer headers"
//#include "../sock/Socket.h"
#include "../SocketStruct.h"
/**
* description
*/
class DataRequest
{
public:
explicit DataRequest();
virtual ~DataRequest();
public:
void SetRequestID(const DWORD requestID);
void SetRequestIP(const std::string& requestIP);
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:
CommandHeader mSendHeader;
std::vector<char> mSendData;
sockaddr_in mRecvAddr;
CommandHeader mRecvHeader;
std::vector<char> mRecvData;
private:
};
#endif
///
// #include "customer headers"
#include "DataRequest.h"
/**
* description
*/
DataRequest::DataRequest()
{
}
/**
* description
*/
DataRequest::~DataRequest()
{
}
void DataRequest::SetRequestID(const DWORD requestID)
{
mSendHeader.RequestID = requestID;
}
void DataRequest::SetRequestIP(const std::string& requestIP)
{
DWORD nRequest = inet_addr(requestIP.c_str());
mSendHeader.RequestIP = nRequest;
}
const std::vector<char>& DataRequest::GetSendData(CommandHeader& header)
{
MakeSendData();
memcpy(mSendHeader.CommandTag, HEADER_TAG, 4);
mSendHeader.CommandVersion = HEADER_VERSION;
mSendHeader.CommandID = GetCommandID();
mSendHeader.DataLen = (DWORD)mSendData.size();
header = mSendHeader;
return mSendData;
}
bool DataRequest::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 DataRequest::CheckReceiveHeader()
{
if (memcmp(mRecvHeader.CommandTag, HEADER_TAG, 4) != 0)
{
return false;
}
if (mRecvHeader.CommandVersion != HEADER_VERSION)
{
return false;
}
if (mRecvHeader.RequestID != mSendHeader.RequestID)
{
return false;
}
if (mRecvHeader.DataLen != mRecvData.size())
{
return false;
}
return true;
}