1
This commit is contained in:
parent
f7c0890bc8
commit
47aa7d49ae
@ -12,11 +12,13 @@ namespace f8
|
||||
const unsigned short MAX_MSG_ID = 2000;
|
||||
|
||||
struct MsgHdr;
|
||||
typedef bool (*CUSTOM_PARSER)(MsgHdr&, google::protobuf::Message*);
|
||||
|
||||
struct NetMsgHandler
|
||||
{
|
||||
int handlerid;
|
||||
const ::google::protobuf::Message* prototype = nullptr;
|
||||
CUSTOM_PARSER custom_parser = nullptr;
|
||||
};
|
||||
|
||||
struct NetMsgHandlerObject
|
||||
@ -38,7 +40,12 @@ namespace f8
|
||||
static void NetMsgHandlerWrapper(InstanceType* instance, MsgHdr& hdr, NetMsgHandler* handler)
|
||||
{
|
||||
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);
|
||||
if (ok) {
|
||||
struct Invoker: public NetMsgHandler
|
||||
@ -54,7 +61,8 @@ namespace f8
|
||||
|
||||
template<typename InstanceType, typename MsgType>
|
||||
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;
|
||||
@ -71,6 +79,7 @@ namespace f8
|
||||
Invoker *p = new Invoker();
|
||||
p->wrapper = &NetMsgHandlerWrapper<InstanceType, MsgType>;
|
||||
p->func = ptr;
|
||||
p->custom_parser = custom_parser;
|
||||
p->handlerid = InstanceType::HID;
|
||||
p->prototype = ::google::protobuf::MessageFactory::generated_factory()->GetPrototype(MsgType::descriptor());
|
||||
msghandler->handlers[msgid] = p;
|
||||
|
Loading…
x
Reference in New Issue
Block a user