Closed pwf2006 closed 6 years ago
longlink_packer.cc文件内容如下:
`#include "longlink_packer.h"
static u_int8_t sg_is_gzip = 0;
struct __STNetMsgXpHeader { u_int16_t total_length; //包的总长度(包头 + 包体) u_int8_t cmdID; //命令字(协议) u_int8_t isGzip; //是否开启Gzip压缩 };
namespace mars { namespace stn { longlink_tracker (longlink_tracker::Create)() = []() { return new longlink_tracker; };
static int __unpack_test(const void* _packed, size_t _packed_len, uint32_t& _cmdid, uint32_t& _seq, size_t& _package_len, size_t& _body_len) { STNetMsgXpHeader st = {0}; if (_packed_len < sizeof(STNetMsgXpHeader)) { _package_len = 0; _body_len = 0; return LONGLINK_UNPACK_CONTINUE; }
memcpy(&st, _packed, sizeof(__STNetMsgXpHeader));
u_int32_t totalLen = ntohs(st.total_length);
st.total_length = totalLen;
// uint32_t client_version = ntohl(st.client_version); // if (client_version != sg_client_version) { // _package_len = 0; // _body_len = 0; // return LONGLINK_UNPACK_FALSE; // } _cmdid = st.cmdID;
// _seq = ntohl(st.seq); _body_len = totalLen - sizeof(__STNetMsgXpHeader); _package_len = totalLen;
if (_package_len > 1024*1024) { return LONGLINK_UNPACK_FALSE; }
if (_package_len > _packed_len) { return LONGLINK_UNPACK_CONTINUE; }
return LONGLINK_UNPACK_OK;
}
void (longlink_pack)(uint32_t _cmdid, uint32_t _seq, const AutoBuffer& _body, const AutoBuffer& _extension, AutoBuffer& _packed, longlink_tracker _tracker) = [](uint32_t _cmdid, uint32_t _seq, const AutoBuffer& _body, const AutoBuffer& _extension, AutoBuffer& _packed, longlink_tracker* _tracker) { __STNetMsgXpHeader st = {0}; st.total_length = htons(sizeof(__STNetMsgXpHeader) + _body.Length()); // st.client_version = htonl(sg_client_version); st.cmdID = _cmdid; st.isGzip = sg_is_gzip; // st.seq = htonl(_seq); // st.body_length = htonl(_body.Length());
_packed.AllocWrite(sizeof(__STNetMsgXpHeader) + _body.Length());
_packed.Write(&st, sizeof(st));
if (NULL != _body.Ptr()) _packed.Write(_body.Ptr(), _body.Length());
_packed.Seek(0, AutoBuffer::ESeekStart);
};
int (longlink_unpack)(const AutoBuffer& _packed, uint32_t& _cmdid, uint32_t& _seq, size_t& _package_len, AutoBuffer& _body, AutoBuffer& _extension, longlink_tracker _tracker) = [](const AutoBuffer& _packed, uint32_t& _cmdid, uint32_t& _seq, size_t& _package_len, AutoBuffer& _body, AutoBuffer& _extension, longlink_tracker* _tracker) { size_t body_len = 0; int ret = __unpack_test(_packed.Ptr(), _packed.Length(), _cmdid, _seq, _package_len, body_len);
if (LONGLINK_UNPACK_OK != ret) return ret;
_body.Write(AutoBuffer::ESeekCur, _packed.Ptr(_package_len-body_len), body_len);
// _body.Write(AutoBuffer::ESeekCur, _packed.Ptr(_package_len-4), body_len); return ret; };
//#define NOOP_CMDID 6
uint32_t (*longlink_noop_cmdid)() = []() -> uint32_t { return NOOP_CMDID; };
bool (*longlink_noop_isresp)(uint32_t _taskid, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend) = [](uint32_t _taskid, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend) { // return Task::kNoopTaskID == _taskid && NOOP_CMDID == _cmdid; return NOOP_CMDID == _cmdid; };
uint32_t (*signal_keep_cmdid)() = []() -> uint32_t { return SIGNALKEEP_CMDID; };
void (*longlink_noop_req_body)(AutoBuffer& _body, AutoBuffer& _extend) = [](AutoBuffer& _body, AutoBuffer& _extend) {
};
void (*longlink_noop_resp_body)(const AutoBuffer& _body, const AutoBuffer& _extend) = [](const AutoBuffer& _body, const AutoBuffer& _extend) {
};
uint32_t (longlink_noop_interval)() = []() -> uint32_t { return 0; // return 1 60 * 1000; //一分钟心跳间隔 };
bool (*longlink_complexconnect_need_verify)() = []() { return false; };
bool (*longlink_ispush)(uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend) = [](uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend) { return PUSH_DATA_TASKID == _taskid; // return true; };
bool (*longlink_identify_isresp)(uint32_t _sent_seq, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend) = [](uint32_t _sent_seq, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend) { // return _sent_seq == _recv_seq ;//&& (0 != _sent_seq); return true; };
} } `
能否给一个定制长连接包头的demo?这样好理解一些