From 953884d057589fe9c55ec7c308ce009f03a717a0 Mon Sep 17 00:00:00 2001 From: callee Date: Wed, 27 Aug 2014 17:50:27 +0800 Subject: [PATCH] unpack ok!!! --- proj/pkgutil/Win32Project2/pkgutil.cpp | 34 ++++++++++---- proj/pkgutil/Win32Project2/pkgutil.h | 14 +++--- proj/pkgutiltest/pkgutiltest.cpp | 62 ++++++++++++++++++++------ 3 files changed, 83 insertions(+), 27 deletions(-) diff --git a/proj/pkgutil/Win32Project2/pkgutil.cpp b/proj/pkgutil/Win32Project2/pkgutil.cpp index 36bd9f8..d386bb1 100644 --- a/proj/pkgutil/Win32Project2/pkgutil.cpp +++ b/proj/pkgutil/Win32Project2/pkgutil.cpp @@ -45,21 +45,37 @@ void pkgUtil::unpkg(const char* in, netpack *pack) pkgUtil::pkgUtil() { - std::memset(_buff, 0, BUFFSIZE); - _cursor = 0; + std::memset(_buff0, 0, BUFFSIZE); + _beginCursor = 0; + _endCursor = 0; } void pkgUtil::append(const char* str, int len) -{ - int i = 0; - while (i++ < len){ - _buff[_cursor] = str[i]; - _cursor++; - } +{ + memcpy(_buff0 + _endCursor, str, len); + _endCursor += len; } void pkgUtil::getNext(netpack* pack) -{ +{ pack->cmd = -1; + if (_endCursor == _beginCursor) return; //没有数据 + unpkg(_buff0 + _beginCursor, pack); + _beginCursor += pack->len + HEAD_SIZE; + resetBuffer(); +} + +void pkgUtil::resetBuffer() +{ + int len = _endCursor - _beginCursor; + if (len == 0){ //没有数据了 + _beginCursor = 0; + _endCursor = 0; + return; + } + memcpy(_buff1, _buff0 + _beginCursor, len); //拷贝到辅助_buffer1 + memcpy(_buff0, _buff1, len); + _beginCursor = 0; + _endCursor = len; } \ No newline at end of file diff --git a/proj/pkgutil/Win32Project2/pkgutil.h b/proj/pkgutil/Win32Project2/pkgutil.h index a201a94..232840c 100644 --- a/proj/pkgutil/Win32Project2/pkgutil.h +++ b/proj/pkgutil/Win32Project2/pkgutil.h @@ -1,11 +1,11 @@ #ifndef __PKG_UTIL_H__ #define __PKG_UTIL_H__ -#define BUFFSIZE 8192 +#define BUFFSIZE 1024*1024 #include struct netpack{ - int len; //长度 + int len; //长度 int cmd; //协议号 char* raw; //数据 }; @@ -15,15 +15,19 @@ class pkgUtil public: static const int HEAD_SIZE = 8; static void printRaw(char* data, int len); - static void pkg(netpack *pack, char* out); //封包 + static void pkg(netpack *pack, char* out); //封包 static void unpkg(const char* in, netpack *pack); //解包 pkgUtil(); void append(const char* str, int len); void getNext(netpack* pack); private: - int _cursor; - char _buff[BUFFSIZE]; + int _beginCursor; //有效数据的开始位置 + int _endCursor; //有效数据的结束位置 + char _buff0[BUFFSIZE]; //主buffer + + void resetBuffer(); //重置开始位置为0 + char _buff1[BUFFSIZE]; //重置时的辅助buffer }; #endif \ No newline at end of file diff --git a/proj/pkgutiltest/pkgutiltest.cpp b/proj/pkgutiltest/pkgutiltest.cpp index 2f099ed..11aec0f 100644 --- a/proj/pkgutiltest/pkgutiltest.cpp +++ b/proj/pkgutiltest/pkgutiltest.cpp @@ -10,30 +10,66 @@ using namespace std; int _tmain(int argc, _TCHAR* argv[]) -{ +{ + pkgUtil* util = new pkgUtil(); + netpack in; in.raw = "abc"; in.cmd = 100; - in.len = sizeof(in.raw)-1; + in.len = strlen(in.raw); + + netpack in2; + in2.raw = "a"; + in2.cmd = 104; + in2.len = strlen(in2.raw); + + netpack in3; + in3.raw = "abcsfcdsf"; + in3.cmd = 102; + in3.len = strlen(in3.raw); + char *out = new char[in.len + pkgUtil::HEAD_SIZE]; pkgUtil::pkg(&in, out); - pkgUtil::printRaw(out, in.len + pkgUtil::HEAD_SIZE); - - netpack out2; - pkgUtil::unpkg(out, &out2); - pkgUtil::printRaw(out2.raw, out2.len); + util->append(out, in.len + pkgUtil::HEAD_SIZE); + delete[] out; - pkgUtil util; - util.append(out, in.len + pkgUtil::HEAD_SIZE); + out = new char[in2.len + pkgUtil::HEAD_SIZE]; + pkgUtil::pkg(&in2, out); + util->append(out, in2.len + pkgUtil::HEAD_SIZE); + delete[] out; + out = new char[in3.len + pkgUtil::HEAD_SIZE]; + pkgUtil::pkg(&in3, out); + util->append(out, in3.len + pkgUtil::HEAD_SIZE); + delete[] out; + + //解包 netpack pack; - util.getNext(&pack); + util->getNext(&pack); + if (pack.cmd > 0) + printf("%d,%d,%s,\n", pack.len, pack.cmd,pack.raw); + util->getNext(&pack); if (pack.cmd > 0) - printf("%d,%d,%s", pack.len, pack.cmd,pack.raw); + printf("%d,%d,%s,\n", pack.len, pack.cmd, pack.raw); + util->getNext(&pack); + if (pack.cmd > 0) + printf("%d,%d,%s,\n", pack.len, pack.cmd, pack.raw); + util->getNext(&pack); + + if (pack.cmd > 0) + printf("%d,%d,%s,\n", pack.len, pack.cmd, pack.raw); + util->getNext(&pack); + if (pack.cmd > 0) + printf("%d,%d,%s,\n", pack.len, pack.cmd, pack.raw); + util->getNext(&pack); + if (pack.cmd > 0) + printf("%d,%d,%s,\n", pack.len, pack.cmd, pack.raw); + util->getNext(&pack); + int len = 0; scanf_s("%d",len); - - delete[] out; + + delete util; return 0; }