⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netmessage.cpp

📁 这是一个简单的使用WinAPI基于WinSock的ICP/IP程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void CNetMsg::Write(CNetStream* pStream)
{
// update the size of the message body (excluding the header data size)
  m_header.SetMsgSize(GetSize_Write());
// let the header know about the message type it's part of
  m_header.SetClassId(GetClassId());
// preallocate the buffer for header's data
  m_header.AllocateData();
// write header's data into void*
// its data will be sent when the message data is sent
  m_header.Write();
// preallocate the buffer for message's body data
  AllocateData();
// write message data into void*
  WriteBody();
// now write the header data and the body data into the socket stream
  pStream->Write(*this);
// if this is a complex object process its children
  if (HasChildren())
	WriteChildren(pStream);
}

ulong CNetMsg::GetSize_Read() const
{ 
  return m_header.GetMsgSize(); 
}
 
ulong CNetMsg::GetSize_Write() const
{
  return 0;
}

void CNetMsg::ReadBody()
{
  // Does nothing here. This has no body
}

void CNetMsg::ReadBody(const CNetMsg& obj)
{
  // Does nothing here. This has no body
}

void CNetMsg::WriteBody()
{
  // Does nothing here. This has no body
}

void CNetMsg::WriteChildren(CNetStream*)
{
  // Does nothing here. This has no children
}

void CNetMsg::ReadChildren(CNetStream*)
{
  // Does nothing here. This has no children
}

//////////////////////////// CNetList /////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNetList::CNetList():
m_pfnOutOfData(0),
m_pCaller(0)
{
}

CNetList::CNetList(const CNetList& obj):
m_pfnOutOfData(0),
m_pCaller(0)
{
  operator=(obj);
}

CNetList::~CNetList()
{
  DeleteAllElem();
}

CNetObject& CNetList::operator=(const CNetObject& obj)
{
// copies the header data
  CNetMsg::operator=(obj);
  const CNetList& list = dynamic_cast<const CNetList&>(obj);
// copies the MSG structure
  m_data.m_nElemN = list.m_data.m_nElemN;

  DeleteAllElem();

  for (int i=0; i<list.m_vecElem.size(); i++) {
    CNetMsg* pMsg = list.m_vecElem[i]->Clone();
	*pMsg = *list.m_vecElem[i];
	m_vecElem.push_back(pMsg);
  }
  return *this;
}

bool CNetList::operator==(const CNetObject& obj)
{
  CNetMsg::operator==(obj);
  const CNetList& list = dynamic_cast<const CNetList&>(obj);
  if (m_data.m_nElemN != list.m_data.m_nElemN)
	return false;
  ulong nSz = m_vecElem.size();
  if ( nSz != list.m_vecElem.size())
	return false;
  for (int i=0; i<nSz; i++)
	if (!(m_vecElem[i] == list.m_vecElem[i]))
	  return false;
  return true;
}

void CNetList::DeleteAllElem()
{
  for (int i=0; i<m_vecElem.size(); i++)
	delete m_vecElem[i];
  m_vecElem.clear();
}

// if this is a complex class (aggregates other CNetMsg derivables) return true
bool CNetList::HasChildren() const
{
  if (!m_data.m_nElemN)
	return false;
  return true;
}

void CNetList::ReadBody()
{
  if (!Get(m_data.m_nElemN))
// if something went wrong
    throw CNetMsgException(ERR_NET_MSG_DATA_CORRUPTED, "void CNetList::ReadBody()");
}

void CNetList::WriteBody()
{
  if (!Put(m_data.m_nElemN))
// if something went wrong
    throw CNetMsgException(ERR_NET_MSG_DATA_CORRUPTED, "void CNetList::WriteBody()");
}

// I have to change this! For large files I can not store all
// the packets in the vector. I have to ged rid of them assson as
// all packets in the vector are sent and I'm about to request more data
void CNetList::WriteChildren(CNetStream* pStream)
{
  if (!m_data.m_nElemN) return;
  if (!m_pfnOutOfData && m_data.m_nElemN > m_vecElem.size())
	throw CNetMsgException(ERR_NET_NO_CALLBACK_REGISTERED, "void CNetList::WriteChildren()");

// okay it must have data, start sending it
  ulong nElem = 0;
  while (nElem < m_data.m_nElemN) {
// if must get more data from the data provider, request it
	if (nElem == m_vecElem.size() && m_data.m_nElemN > m_vecElem.size())
	  if (!m_pfnOutOfData(m_pCaller, this))
// if the request fails generate an error message and send it
// ... TO DO
	    return;
// send a packet
    *pStream << *m_vecElem[nElem++];
  }
}

void CNetList::ReadChildren(CNetStream* pStream)
{
  ulong nElem = 0;
  while (nElem++ < m_data.m_nElemN) {
	CNetMsg* pMsg;
	*pStream >> pMsg;
	m_vecElem.push_back(pMsg);
  }
}

bool CNetList::AddElem(CNetMsg* pMsg)
{
  if (m_data.m_nElemN < m_vecElem.size())
	return false;
  m_vecElem.push_back(pMsg);
  return true;
}

bool CNetList::SetDataRequestCallback(void* pCaller, DataCallBack pfnOutOfData)
{
  if (!pfnOutOfData || !pCaller) return false;
  m_pfnOutOfData = pfnOutOfData;
  m_pCaller      = pCaller;
  return true;
}

bool CNetList::SetDataArrivedCallback(void* pCaller, DataCallBack pfnOutOfData)
{
  return false;
}

void CNetList::Dump(ostream& os)
{
  ulong sz = 0;
  os << endl << "Start Dump for CNetList:" << endl << endl;

  os << "Dump for the buffer:" << endl << endl;
  if (Get(sz)) 
    os << "CNetList::m_data::m_nElemN = " << sz << ";" << endl;
  
  os << "Dump for the member variables:" << endl << endl;
  os << "CNetList::m_data::m_nElemN = " << m_data.m_nElemN << ";" << endl;
  
  os << endl << "End Dump for CNetList." << endl << endl;
}

/////////////////////////// CNetPacket /////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNetPacket::CNetPacket()
{
  m_data.m_nSize = 0;
  m_data.m_szPacket[0] = 0;
  m_data.m_nSeqN = 0;
}

CNetPacket::CNetPacket(ulong hostId, ulong remoteId, CNetPacket::MSG msg_data)
{
  SetHost(hostId);
  SetRemote(remoteId);
// message data
  SetData(msg_data.m_szPacket, msg_data.m_nSize, msg_data.m_nSeqN);
}

CNetPacket::CNetPacket(const char* szPacket, ulong nSize, ulong nSeqN)
{
  SetData(szPacket, nSize, nSeqN);
}

void CNetPacket::ReadBody()
{
// read the packet itself
  if (!Get(m_data.m_nSize) ||
	  !Get(m_data.m_szPacket, m_data.m_nSize) ||
	  !Get(m_data.m_nSeqN))
// if something went wrong
    throw CNetMsgException(ERR_NET_MSG_DATA_CORRUPTED, "void CNetPacket::ReadBody()");
}

void CNetPacket::ReadBody(const CNetMsg& obj)
{
  const CNetPacket& packet = dynamic_cast<const CNetPacket&>(obj);
  m_data.m_nSize = packet.m_data.m_nSize;
  memcpy(m_data.m_szPacket, packet.m_data.m_szPacket, m_data.m_nSize);
  m_data.m_nSeqN = packet.m_data.m_nSeqN;
}

void CNetPacket::WriteBody()
{
// write the packet
  if (!Put(m_data.m_nSize) ||
	  !Put(m_data.m_szPacket, m_data.m_nSize) ||
	  !Put(m_data.m_nSeqN))
// if something went wrong
    throw CNetMsgException(ERR_NET_MSG_DATA_CORRUPTED, "void CNetText::WriteBody()");
}

ulong CNetPacket::GetSize_Write() const
{
// the total size = sizeof(ulong) + len(m_szPacket)
  return GetSize_ulong() + m_data.m_nSize + GetSize_ulong();
}

void CNetPacket::SetData(const char* szPacket, ulong nSize, ulong nSeqN)
{
  m_data.m_nSize = nSize;
  memcpy(m_data.m_szPacket, szPacket, m_data.m_nSize);
  m_data.m_nSeqN = nSeqN;
}

CNetObject& CNetPacket::operator=(const CNetObject& obj) 
{
// copies the header data
  CNetMsg::operator=(obj);
  const CNetPacket& packet = dynamic_cast<const CNetPacket&>(obj);
// copies the MSG structure
  SetData(packet.m_data.m_szPacket, packet.m_data.m_nSize, packet.m_data.m_nSeqN);
  return *this;
}

bool CNetPacket::operator==(const CNetObject& obj)
{
  const CNetPacket& packet = dynamic_cast<const CNetPacket&>(obj);
  if (!memcmp(m_data.m_szPacket, packet.m_data.m_szPacket, m_data.m_nSize) &&
	   m_data.m_nSeqN == packet.m_data.m_nSeqN)
    return true;
  return false;
}

void CNetPacket::Dump(ostream& os)
{
  char data[MAX_NET_PACKET];
  ulong sz = 0;
  
  os << endl << "Start Dump for CNetPacket:" << endl << endl;

  os << "Dump for the buffer:" << endl << endl;
  if (Get(sz)) 
    os << "CNetPacket::m_data::m_nSize = " << sz << ";" << endl;
  if (Get(data, sz)) 
    os << "CNetPacket::m_data::m_szPacket = " << data << ";" << endl;
  if (Get(sz)) 
    os << "CNetPacket::m_data::m_nSeqN = " << sz << ";" << endl;
  
  os << "Dump for the member variables:" << endl << endl;
  os << "CNetText::m_data::m_nSize = " << m_data.m_nSize << ";" << endl;
  os << "CNetText::m_data::m_szPacket = " << m_data.m_szPacket << ";" << endl;
  os << "CNetText::m_data::m_nSeqN = " << m_data.m_nSeqN << ";" << endl;
  
  os << endl << "End Dump for CNetPacket:" << endl << endl;
}

/////////////////////////// CNetStatus /////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CNetStatus::CNetStatus()
{
  m_data.m_nTextSz = 0;
  m_data.m_nCode = 0;
}

CNetStatus::CNetStatus(ulong host, ulong remote, CNetStatus::MSG msg_data)
{
  SetHost(host);
  SetRemote(remote);
// message data
  m_data.m_nCode     = msg_data.m_nCode;
  m_data.m_strText   = msg_data.m_strText;
  m_data.m_nTextSz   = m_data.m_strText.length();
}

CNetStatus::~CNetStatus()
{
}

CNetObject& CNetStatus::operator=(const CNetObject& status) 
{
// copies the header data
  CNetMsg::operator=(status);
  const CNetStatus& obj = dynamic_cast<const CNetStatus&>(status);
// copies the MSG structure
  m_data.m_nCode = obj.m_data.m_nCode;
  m_data.m_strText   = obj.m_data.m_strText;
  m_data.m_nTextSz   = m_data.m_strText.length();

  return *this;
}

bool CNetStatus::operator==(const CNetObject& status)
{
  CNetMsg::operator==(status);
  const CNetStatus& obj = dynamic_cast<const CNetStatus&>(status);
  if (!m_data.m_strText.compare(obj.m_data.m_strText) &&
       m_data.m_nCode == obj.m_data.m_nCode)
    return true;
  return false;
}

void CNetStatus::ReadBody()
{
  if 
  (
// read the size of the string
        !Get(m_data.m_nTextSz)                   ||
// read the string itself
        !Get(m_data.m_strText, m_data.m_nTextSz) ||
// read the exit code
        !Get(m_data.m_nCode)                                 
  )
// if something went wrong
        throw CNetMsgException(ERR_NET_MSG_DATA_CORRUPTED, "void CNetStatus::ReadBody()");
}

void CNetStatus::ReadBody(const CNetMsg& status)
{
  const CNetStatus& obj = dynamic_cast<const CNetStatus&>(status);
  m_data.m_nCode     = obj.m_data.m_nCode;
  m_data.m_strText   = obj.m_data.m_strText;
  m_data.m_nTextSz   = m_data.m_strText.length();
}

void CNetStatus::WriteBody()
{
  if 
  (
// write the size of the string
        !Put(m_data.m_nTextSz)                                   ||
// write the string itself
        !Put(m_data.m_strText, m_data.m_nTextSz) ||
// write the exit code
        !Put(m_data.m_nCode)                                 
  )
// if something went wrong
        throw CNetMsgException(ERR_NET_MSG_DATA_CORRUPTED, "void CNetStatus::ReadBody()");
}

ulong CNetStatus::GetSize_Write() const
{
// the total size = sizeof(ulong) + sizeof(strlen(m_strText)) + sizeof(ulong)
  return GetSize_ulong() + m_data.m_nTextSz + GetSize_ulong();
}

void CNetStatus::Dump(ostream& os)
{
  string data = 0;
  ulong sz = 0;
  os << endl << "Start Dump for CNetStatus:" << endl << endl;

  os << "Dump for the buffer:" << endl << endl;
  if (Get(sz)) 
        os << "CCNetStatus::m_data::m_nTextSz = " << sz << ";" << endl;
  if (Get(data, sz)) 
        os << "CNetStatus::m_data::m_strText = " << data.c_str() << ";" << endl;
  if (Get(sz)) 
        os << "CNetStatus::m_data::m_nCode = " << sz << ";" << endl;
  
  os << "Dump for the member variables:" << endl << endl;
  os << "CNetStatus::m_data::m_nTextSz = " << m_data.m_nTextSz << ";" << endl;
  os << "CNetStatus::m_data::m_strText = " << m_data.m_strText.c_str() << ";" << endl;
  os << "CNetStatus::m_data::m_nCode = " << m_data.m_nCode << ";" << endl;
  
  os << endl << "End Dump for CNetStatus." << endl << endl;
}

void CNetStatus::SetText(const char* name)
{
  m_data.m_nTextSz = strlen(name);
  m_data.m_strText = name;
}

void CNetStatus::SetCode(ulong nCode)
{
  m_data.m_nCode= nCode;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -