This commit is contained in:
aozhiwei 2019-09-17 00:11:27 +08:00
parent f7c0890bc8
commit 47aa7d49ae

View File

@ -12,11 +12,13 @@ namespace f8
const unsigned short MAX_MSG_ID = 2000; const unsigned short MAX_MSG_ID = 2000;
struct MsgHdr; struct MsgHdr;
typedef bool (*CUSTOM_PARSER)(MsgHdr&, google::protobuf::Message*);
struct NetMsgHandler struct NetMsgHandler
{ {
int handlerid; int handlerid;
const ::google::protobuf::Message* prototype = nullptr; const ::google::protobuf::Message* prototype = nullptr;
CUSTOM_PARSER custom_parser = nullptr;
}; };
struct NetMsgHandlerObject struct NetMsgHandlerObject
@ -38,7 +40,12 @@ namespace f8
static void NetMsgHandlerWrapper(InstanceType* instance, MsgHdr& hdr, NetMsgHandler* handler) static void NetMsgHandlerWrapper(InstanceType* instance, MsgHdr& hdr, NetMsgHandler* handler)
{ {
MsgType msg; MsgType msg;
bool ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset); bool ok = false;
if (handler->custom_parser) {
ok = handler->custom_parser(hdr, &msg);
} else {
ok = msg.ParseFromArray(hdr.buf + hdr.offset, hdr.buflen - hdr.offset);
}
assert(ok); assert(ok);
if (ok) { if (ok) {
struct Invoker: public NetMsgHandler struct Invoker: public NetMsgHandler
@ -54,7 +61,8 @@ namespace f8
template<typename InstanceType, typename MsgType> template<typename InstanceType, typename MsgType>
static void RegisterNetMsgHandler(NetMsgHandlerObject* msghandler, static void RegisterNetMsgHandler(NetMsgHandlerObject* msghandler,
void (InstanceType::*ptr)(MsgHdr&, const MsgType&) void (InstanceType::*ptr)(MsgHdr&, const MsgType&),
CUSTOM_PARSER custom_parser = nullptr
) )
{ {
MsgType dumy_msg; MsgType dumy_msg;
@ -71,6 +79,7 @@ namespace f8
Invoker *p = new Invoker(); Invoker *p = new Invoker();
p->wrapper = &NetMsgHandlerWrapper<InstanceType, MsgType>; p->wrapper = &NetMsgHandlerWrapper<InstanceType, MsgType>;
p->func = ptr; p->func = ptr;
p->custom_parser = custom_parser;
p->handlerid = InstanceType::HID; p->handlerid = InstanceType::HID;
p->prototype = ::google::protobuf::MessageFactory::generated_factory()->GetPrototype(MsgType::descriptor()); p->prototype = ::google::protobuf::MessageFactory::generated_factory()->GetPrototype(MsgType::descriptor());
msghandler->handlers[msgid] = p; msghandler->handlers[msgid] = p;