Skip to content

Commit

Permalink
fix Tencent#788 build fail
Browse files Browse the repository at this point in the history
@astrozhou longlink_decoder
  • Loading branch information
garryyan committed Apr 29, 2020
1 parent eae122f commit 9b9706a
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 163 deletions.
2 changes: 2 additions & 0 deletions mars/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ cmake_minimum_required (VERSION 3.6)
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE PATH "Installation directory" FORCE)
message(STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}")

include_directories(openssl/include)

add_subdirectory(comm comm)
add_subdirectory(boost boost)
add_subdirectory(app app)
Expand Down
92 changes: 47 additions & 45 deletions mars/libraries/mars_android_sdk/jni/longlink_packer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ struct __STNetMsgXpHeader {

namespace mars {
namespace stn {

LongLinkEncoder gDefaultLongLinkEncoder;

longlink_tracker* (*longlink_tracker::Create)()
= []() {
return new longlink_tracker;
Expand Down Expand Up @@ -84,8 +87,13 @@ static int __unpack_test(const void* _packed, size_t _packed_len, uint32_t& _cmd
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) {
LongLinkEncoder::LongLinkEncoder() {
longlink_pack = [](uint32_t _cmdid,
uint32_t _seq,
const AutoBuffer& _body,
const AutoBuffer& _extension,
AutoBuffer& _packed,
longlink_tracker* _tracker) {
__STNetMsgXpHeader st = {0};
st.head_length = htonl(sizeof(__STNetMsgXpHeader));
st.client_version = htonl(sg_client_version);
Expand All @@ -95,74 +103,68 @@ void (*longlink_pack)(uint32_t _cmdid, uint32_t _seq, const AutoBuffer& _body, c

_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);
};

longlink_unpack = [](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);

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);

return ret;
};
if (LONGLINK_UNPACK_OK != ret) return ret;

_body.Write(AutoBuffer::ESeekCur, _packed.Ptr(_package_len - body_len), body_len);

return ret;
};

#define NOOP_CMDID 6
#define SIGNALKEEP_CMDID 243
#define PUSH_DATA_TASKID 0

uint32_t (*longlink_noop_cmdid)()
= []() -> uint32_t {
return NOOP_CMDID;
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;
longlink_noop_isresp = [](uint32_t _taskid, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend) {
return Task::kNoopTaskID == _taskid && NOOP_CMDID == _cmdid;
};

uint32_t (*signal_keep_cmdid)()
= []() -> uint32_t {
return SIGNALKEEP_CMDID;
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) {

longlink_noop_req_body = [](AutoBuffer& _body, AutoBuffer& _extend) {

};

uint32_t (*longlink_noop_interval)()
= []() -> uint32_t {
return 0;
longlink_noop_resp_body = [](const AutoBuffer& _body, const AutoBuffer& _extend) {

};

bool (*longlink_complexconnect_need_verify)()
= []() {
return false;
longlink_noop_interval = []() -> uint32_t {
return 0;
};

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;
longlink_complexconnect_need_verify = []() {
return false;
};

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;

longlink_ispush = [](uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend) {
return PUSH_DATA_TASKID == _taskid;
};

longlink_identify_isresp = [](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;
};
}
}
}
43 changes: 43 additions & 0 deletions mars/libraries/mars_android_sdk/jni/longlink_packer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <stdlib.h>
#include <stdint.h>
#include <functional>

#define LONGLINK_UNPACK_CONTINUE (-2)
#define LONGLINK_UNPACK_FALSE (-1)
Expand All @@ -49,6 +50,48 @@ class longlink_tracker {
virtual ~longlink_tracker(){};
};

class LongLinkEncoder {
public:
LongLinkEncoder();

/**
* package the request data
* _cmdid: business identifier
* _seq: task id
* _raw: business send buffer
* _packed: business send buffer + request header
*/
std::function<void (uint32_t _cmdid, uint32_t _seq, const AutoBuffer& _body, const AutoBuffer& _extension, AutoBuffer& _packed, longlink_tracker* _tracker)> longlink_pack;

/**
* unpackage the response data
* _packed: data received from server
* _cmdid: business identifier
* _seq: task id
* _package_len:
* _body: business receive buffer
* return: 0 if unpackage succ
*/
std::function<int (const AutoBuffer& _packed, uint32_t& _cmdid, uint32_t& _seq, size_t& _package_len, AutoBuffer& _body, AutoBuffer& _extension, longlink_tracker* _tracker)> longlink_unpack;

//heartbeat signal to keep longlink network alive
std::function<uint32_t ()> longlink_noop_cmdid;
std::function<bool (uint32_t _taskid, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_noop_isresp;
std::function<uint32_t ()> signal_keep_cmdid;
std::function<void (AutoBuffer& _body, AutoBuffer& _extend)> longlink_noop_req_body;
std::function<void (const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_noop_resp_body;
std::function<uint32_t ()> longlink_noop_interval;
std::function<bool ()> longlink_complexconnect_need_verify;

/**
* return: whether the received data is pushing from server or not
*/
std::function<bool (uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_ispush;
std::function<bool (uint32_t _sent_seq, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_identify_isresp;
};

extern LongLinkEncoder gDefaultLongLinkEncoder;

/**
* package the request data
* _cmdid: business identifier
Expand Down
143 changes: 89 additions & 54 deletions mars/stn/proto/longlink_packer.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include <stdlib.h>
#include <stdint.h>
#include <functional>

#define LONGLINK_UNPACK_CONTINUE (-2)
#define LONGLINK_UNPACK_FALSE (-1)
Expand All @@ -41,58 +40,94 @@ class AutoBuffer;

namespace mars {
namespace stn {

class longlink_tracker {
public:
static longlink_tracker* (*Create)();

public:
virtual ~longlink_tracker(){};
};

class LongLinkEncoder {
public:
LongLinkEncoder();

/**
* package the request data
* _cmdid: business identifier
* _seq: task id
* _raw: business send buffer
* _packed: business send buffer + request header
*/
std::function<void (uint32_t _cmdid, uint32_t _seq, const AutoBuffer& _body, const AutoBuffer& _extension, AutoBuffer& _packed, longlink_tracker* _tracker)> longlink_pack;

/**
* unpackage the response data
* _packed: data received from server
* _cmdid: business identifier
* _seq: task id
* _package_len:
* _body: business receive buffer
* return: 0 if unpackage succ
*/
std::function<int (const AutoBuffer& _packed, uint32_t& _cmdid, uint32_t& _seq, size_t& _package_len, AutoBuffer& _body, AutoBuffer& _extension, longlink_tracker* _tracker)> longlink_unpack;

//heartbeat signal to keep longlink network alive
std::function<uint32_t ()> longlink_noop_cmdid;
std::function<bool (uint32_t _taskid, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_noop_isresp;
std::function<uint32_t ()> signal_keep_cmdid;
std::function<void (AutoBuffer& _body, AutoBuffer& _extend)> longlink_noop_req_body;
std::function<void (const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_noop_resp_body;
std::function<uint32_t ()> longlink_noop_interval;
std::function<bool ()> longlink_complexconnect_need_verify;

/**
* return: whether the received data is pushing from server or not
*/
std::function<bool (uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_ispush;
std::function<bool (uint32_t _sent_seq, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_identify_isresp;
};

extern LongLinkEncoder gDefaultLongLinkEncoder;

}

class longlink_tracker {
public:
static longlink_tracker* (*Create)();

public:
virtual ~longlink_tracker(){};
};

class LongLinkEncoder {
public:
LongLinkEncoder();

/**
* package the request data
* _cmdid: business identifier
* _seq: task id
* _raw: business send buffer
* _packed: business send buffer + request header
*/
std::function<void (uint32_t _cmdid, uint32_t _seq, const AutoBuffer& _body, const AutoBuffer& _extension, AutoBuffer& _packed, longlink_tracker* _tracker)> longlink_pack;

/**
* unpackage the response data
* _packed: data received from server
* _cmdid: business identifier
* _seq: task id
* _package_len:
* _body: business receive buffer
* return: 0 if unpackage succ
*/
std::function<int (const AutoBuffer& _packed, uint32_t& _cmdid, uint32_t& _seq, size_t& _package_len, AutoBuffer& _body, AutoBuffer& _extension, longlink_tracker* _tracker)> longlink_unpack;

//heartbeat signal to keep longlink network alive
std::function<uint32_t ()> longlink_noop_cmdid;
std::function<bool (uint32_t _taskid, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_noop_isresp;
std::function<uint32_t ()> signal_keep_cmdid;
std::function<void (AutoBuffer& _body, AutoBuffer& _extend)> longlink_noop_req_body;
std::function<void (const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_noop_resp_body;
std::function<uint32_t ()> longlink_noop_interval;
std::function<bool ()> longlink_complexconnect_need_verify;

/**
* return: whether the received data is pushing from server or not
*/
std::function<bool (uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_ispush;
std::function<bool (uint32_t _sent_seq, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend)> longlink_identify_isresp;
};

extern LongLinkEncoder gDefaultLongLinkEncoder;

/**
* package the request data
* _cmdid: business identifier
* _seq: task id
* _raw: business send buffer
* _packed: business send buffer + request header
*/
extern void (*longlink_pack)(uint32_t _cmdid, uint32_t _seq, const AutoBuffer& _body, const AutoBuffer& _extension, AutoBuffer& _packed, longlink_tracker* _tracker);

/**
* unpackage the response data
* _packed: data received from server
* _cmdid: business identifier
* _seq: task id
* _package_len:
* _body: business receive buffer
* return: 0 if unpackage succ
*/
extern int (*longlink_unpack)(const AutoBuffer& _packed, uint32_t& _cmdid, uint32_t& _seq, size_t& _package_len, AutoBuffer& _body, AutoBuffer& _extension, longlink_tracker* _tracker);

//heartbeat signal to keep longlink network alive
extern uint32_t (*longlink_noop_cmdid)();
extern bool (*longlink_noop_isresp)(uint32_t _taskid, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend);
extern uint32_t (*signal_keep_cmdid)();
extern void (*longlink_noop_req_body)(AutoBuffer& _body, AutoBuffer& _extend);
extern void (*longlink_noop_resp_body)(const AutoBuffer& _body, const AutoBuffer& _extend);

extern uint32_t (*longlink_noop_interval)();

extern bool (*longlink_complexconnect_need_verify)();

/**
* return: whether the received data is pushing from server or not
*/
extern bool (*longlink_ispush)(uint32_t _cmdid, uint32_t _taskid, const AutoBuffer& _body, const AutoBuffer& _extend);
extern bool (*longlink_identify_isresp)(uint32_t _sent_seq, uint32_t _cmdid, uint32_t _recv_seq, const AutoBuffer& _body, const AutoBuffer& _extend);

}
}
#endif // STN_SRC_LONGLINKPACKER_H_

7 changes: 6 additions & 1 deletion mars/stn/src/net_core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ void NetCore::__InitLongLink(){
timing_sync_ = new TimingSync(*ActiveLogic::Instance());

longlink_task_manager_ = new LongLinkTaskManager(*net_source_, *ActiveLogic::Instance(), *dynamic_timeout_, GetMessageQueueId());


LonglinkConfig defaultConfig(DEFAULT_LONGLINK_NAME, DEFAULT_LONGLINK_GROUP, true);
defaultConfig.is_keep_alive = true;
defaultConfig.longlink_encoder = &gDefaultLongLinkEncoder;
CreateLongLink(defaultConfig);

// async
longlink_task_manager_->fun_callback_ = boost::bind(&NetCore::__CallBack, this, (int)kCallFromLong, _1, _2, _3, _4, _5);

Expand Down
2 changes: 1 addition & 1 deletion mars/stn/stn.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

#include "mars/comm/autobuffer.h"
#include "mars/comm/projdef.h"
#include "mars/stn/proto/longlink_packer.h"

namespace mars{
namespace stn{
Expand All @@ -40,6 +39,7 @@ static const std::string DEFAULT_LONGLINK_NAME = "default-longlink";
static const std::string DEFAULT_LONGLINK_GROUP = "default-group";
struct TaskProfile;
struct DnsProfile;
class LongLinkEncoder;

struct Task {
public:
Expand Down
Loading

0 comments on commit 9b9706a

Please sign in to comment.