#include "StdAfx.h" #include "MessageObject.h" MessageObject::MessageObject(void) { } MessageObject::~MessageObject(void) { } void MessageObject::addPacket(char * packetBuf, unsigned long packetBufLen) { int findFlag = 0; for (int i = 0; i < packetBufLen; i++) { if (packetBuf[i] == 0xfd && packetBuf[i+1] == 0xff) { if (alreadyRecvLen + i - endFlagLen == messageTotalLen) { strncpy(messageStr+alreadyRecvLen, packetBuf, i - endFlagLen); messageNextTotalLen = ntohl((long)(WORD*)(packetBuf[i+2])); strncpy(messageNextStr, packetBuf+i+1, packetBufLen - i - 4); alreadyRecvLen += i - endFlagLen; messageNextFlag = true; } else if (alreadyRecvLen == messageTotalLen) { alreadyRecvLen = 0; messageTotalLen = 0; messageTotalLen = ntohl((long)(WORD*)(packetBuf[i+2])); strncpy(messageStr+alreadyRecvLen, packetBuf+i+2, packetBufLen-i-2); } else if (alreadyRecvLen < messageNextTotalLen) { messagePreDroped = true; } findFlag = 1; } } if (findFlag == 0) { strncpy(messageStr+alreadyRecvLen, packetBuf, packetBufLen); alreadyRecvLen += packetBufLen; } } bool MessageObject::isCompleted() { if (alreadyRecvLen >= messageTotalLen && alreadyRecvLen != 0) { return true; } return false; } void MessageObject::getMessageStr(char* outMessageStr) { strncpy(outMessageStr, messageStr, alreadyRecvLen); if (messageNextFlag == true) { } }
#pragma once #include <WinSock2.h> class MessageObject { public: MessageObject(void); public: ~MessageObject(void); private: int messageTotalLen; int alreadyRecvLen; char messageStr[8192]; char messageNextStr[8192]; int messageNextTotalLen; int endFlagLen; bool messagePreDroped; bool messageNextFlag; protected: void addPacket(char * packetBuf, unsigned long packetBufLen); bool isCompleted(); void getMessageStr(char* outMessageStr); };
还没搞完。