every one, I have a question about how protocol buffer interact with existed protocol mechanism,Say code below:
class PacketBase
{
pub开发者_如何转开发lic:
PacketBase();
private:
int msgType;
int msgLen;
private:
MessageBuilder* m_pMsgBuilder; /// do Write and Read From msg stream
};
class LoginRequest : public PacketBase
{
/// here we can use proto replace the msg field defined here
/// invoke write or read method in packetbase to serialze or de-serialize
};
Can protocol buffer do the job while maintain the class hierarchy unchanged?
Well, since your msgType and msgLen fields are both private, I think your question boils down to "Can I replace LoginRequest with a protocol buffer that has a no-args constructor?" and the answer here is an unqualified "yes".
Protocol buffers take care of (de-)serializing fields while maintaining type information; there's really no reason for you to write that code yourself. If you were to create a LoginRequest protobuf, for example, you could just construct it, call setters to set its fields, and then serialize it to an ostringstream. That's probably sufficient for what you want, right?
精彩评论