Refactor the implementation of message encode/decode.