mcpack2pb.h 6.11 KB
Newer Older
gejun's avatar
gejun committed
1
// mcpack2pb - Make protobuf be front-end of mcpack/compack
gejun's avatar
gejun committed
2
// Copyright (c) 2015 Baidu, Inc.
gejun's avatar
gejun committed
3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
gejun's avatar
gejun committed
15

gejun's avatar
gejun committed
16
// Author: Ge,Jun (gejun@baidu.com)
gejun's avatar
gejun committed
17 18
// Date: Mon Oct 19 17:17:36 CST 2015

gejun's avatar
gejun committed
19 20
#ifndef MCPACK2PB_MCPACK_MCPACK2PB_H
#define MCPACK2PB_MCPACK_MCPACK2PB_H
gejun's avatar
gejun committed
21 22 23

#include <google/protobuf/message.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
24 25
#include "butil/containers/flat_map.h"
#include "butil/iobuf.h"
gejun's avatar
gejun committed
26 27 28 29 30 31 32 33 34
#include "mcpack2pb/parser.h"
#include "mcpack2pb/serializer.h"

namespace mcpack2pb {

typedef bool (*SetFieldFn)(::google::protobuf::Message* msg,
                           UnparsedValue& value);

// Mapping from filed name to its parsing&setting function.
35
typedef butil::FlatMap<butil::StringPiece, SetFieldFn> FieldMap;
gejun's avatar
gejun committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

enum SerializationFormat {
    FORMAT_COMPACK,
    FORMAT_MCPACK_V2
};

struct MessageHandler {
    // Parse `msg' from `input' as a mcpack_v2 or compack object.
    // Returns number of bytes consumed.
    size_t (*parse)(::google::protobuf::Message* msg,
                    ::google::protobuf::io::ZeroCopyInputStream* input);

    // Parse `msg' from `input' as a mcpack_v2 or compack object removed with header.
    // Returns true on success.
    bool (*parse_body)(::google::protobuf::Message* msg,
                       ::google::protobuf::io::ZeroCopyInputStream* input,
                       size_t size);
    
    // Serialize `msg' as a mcpack_v2 or compack object into `output'.
    // The serialization format is decided by `format'.
    // Returns true on success.
    bool (*serialize)(const ::google::protobuf::Message& msg,
                      ::google::protobuf::io::ZeroCopyOutputStream* output,
                      SerializationFormat format);

    // Serialize `msg' as a mcpack_v2 or compack object without header into
    // `serializer'. The serialization format is decided by `format'.
    // Returns true on success.
    void (*serialize_body)(const ::google::protobuf::Message& msg,
                           Serializer& serializer,
                           SerializationFormat format);

    // -------------------
    //  Helper functions
    // -------------------

    // Parse `msg' from IOBuf or array which may contain more data than just
    // the message.
    // Returns bytes parsed, 0 on error.
    size_t parse_from_iobuf_prefix(::google::protobuf::Message* msg,
76
                                   const ::butil::IOBuf& buf);
gejun's avatar
gejun committed
77 78 79 80 81
    size_t parse_from_array_prefix(::google::protobuf::Message* msg,
                                   const void* data, int size);
    // Parse `msg' from IOBuf or array which may just contain the message.
    // Returns true on success.
    bool parse_from_iobuf(::google::protobuf::Message* msg,
82
                          const ::butil::IOBuf& buf);
gejun's avatar
gejun committed
83 84 85 86 87
    bool parse_from_array(::google::protobuf::Message* msg,
                          const void* data, int size);
    // Serialize `msg' to IOBuf or string.
    // Returns true on success.
    bool serialize_to_iobuf(const ::google::protobuf::Message& msg,
88
                            ::butil::IOBuf* buf, SerializationFormat format);
gejun's avatar
gejun committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

    // TODO(gejun): serialize_to_string is not supported because OutputStream
    // requires the embedded zero-copy stream to return permanent memory blocks
    // to support reserve() however the string inside StringOutputStream may
    // be resized and invalidates previous returned memory blocks.
};

static const MessageHandler INVALID_MESSAGE_HANDLER = {NULL, NULL, NULL, NULL};

// if the *.pb.cc and *.pb.h was generated by mcpack2pb. This function will be
// called with the mcpack/compack parser and serializer BEFORE main().
void register_message_handler_or_die(const std::string& full_name,
                                     const MessageHandler& handler);

// Find the registered parser/serializer by `full_name'
// e.g. "example.SampleMessage".
// If the handler was not registered, function pointers inside are NULL.
MessageHandler find_message_handler(const std::string& full_name);

// inline impl.
inline size_t MessageHandler::parse_from_iobuf_prefix(
110
    ::google::protobuf::Message* msg, const ::butil::IOBuf& buf) {
gejun's avatar
gejun committed
111 112 113 114
    if (parse == NULL) {
        LOG(ERROR) << "`parse' is NULL";
        return 0;
    }
115
    ::butil::IOBufAsZeroCopyInputStream zc_stream(buf);
gejun's avatar
gejun committed
116 117 118 119
    return parse(msg, &zc_stream);
}

inline bool MessageHandler::parse_from_iobuf(
120
    ::google::protobuf::Message* msg, const ::butil::IOBuf& buf) {
gejun's avatar
gejun committed
121 122 123 124
    if (parse == NULL) {
        LOG(ERROR) << "`parse' is NULL";
        return 0;
    }
125
    ::butil::IOBufAsZeroCopyInputStream zc_stream(buf);
gejun's avatar
gejun committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    return parse(msg, &zc_stream) == buf.size();
}

inline size_t MessageHandler::parse_from_array_prefix(
    ::google::protobuf::Message* msg, const void* data, int size) {
    if (parse == NULL) {
        LOG(ERROR) << "`parse' is NULL";
        return 0;
    }
    ::google::protobuf::io::ArrayInputStream zc_stream(data, size);
    return parse(msg, &zc_stream);
}

inline bool MessageHandler::parse_from_array(
    ::google::protobuf::Message* msg, const void* data, int size) {
    if (parse == NULL) {
        LOG(ERROR) << "`parse' is NULL";
        return 0;
    }
    ::google::protobuf::io::ArrayInputStream zc_stream(data, size);
    return (int)parse(msg, &zc_stream) == size;
}

inline bool MessageHandler::serialize_to_iobuf(
    const ::google::protobuf::Message& msg,
151
    ::butil::IOBuf* buf, SerializationFormat format) {
gejun's avatar
gejun committed
152 153 154 155
    if (serialize == NULL) {
        LOG(ERROR) << "`serialize' is NULL";
        return false;
    }
156
    ::butil::IOBufAsZeroCopyOutputStream zc_stream(buf);
gejun's avatar
gejun committed
157 158 159 160 161
    return serialize(msg, &zc_stream, format);
}

} // namespace mcpack2pb

gejun's avatar
gejun committed
162
#endif // MCPACK2PB_MCPACK_MCPACK2PB_H