Skip to content

Commit

Permalink
unpack ok!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
heweitykc committed Aug 27, 2014
1 parent 648e412 commit 953884d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 27 deletions.
34 changes: 25 additions & 9 deletions proj/pkgutil/Win32Project2/pkgutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
14 changes: 9 additions & 5 deletions proj/pkgutil/Win32Project2/pkgutil.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef __PKG_UTIL_H__
#define __PKG_UTIL_H__

#define BUFFSIZE 8192
#define BUFFSIZE 1024*1024
#include <vector>

struct netpack{
int len; //长度
int len; //长度
int cmd; //协议号
char* raw; //数据
};
Expand All @@ -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
62 changes: 49 additions & 13 deletions proj/pkgutiltest/pkgutiltest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 953884d

Please sign in to comment.