mcpack2pb.h 6.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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
18 19
// mcpack2pb - Make protobuf be front-end of mcpack/compack

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

gejun's avatar
gejun committed
23 24
#ifndef MCPACK2PB_MCPACK_MCPACK2PB_H
#define MCPACK2PB_MCPACK_MCPACK2PB_H
gejun's avatar
gejun committed
25 26 27

#include <google/protobuf/message.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
28 29
#include "butil/containers/flat_map.h"
#include "butil/iobuf.h"
gejun's avatar
gejun committed
30 31 32 33 34 35 36 37 38
#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.
39
typedef butil::FlatMap<butil::StringPiece, SetFieldFn> FieldMap;
gejun's avatar
gejun committed
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 76 77 78 79

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,
80
                                   const ::butil::IOBuf& buf);
gejun's avatar
gejun committed
81 82 83 84 85
    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,
86
                          const ::butil::IOBuf& buf);
gejun's avatar
gejun committed
87 88 89 90 91
    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,
92
                            ::butil::IOBuf* buf, SerializationFormat format);
gejun's avatar
gejun committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

    // 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(
114
    ::google::protobuf::Message* msg, const ::butil::IOBuf& buf) {
gejun's avatar
gejun committed
115 116 117 118
    if (parse == NULL) {
        LOG(ERROR) << "`parse' is NULL";
        return 0;
    }
119
    ::butil::IOBufAsZeroCopyInputStream zc_stream(buf);
gejun's avatar
gejun committed
120 121 122 123
    return parse(msg, &zc_stream);
}

inline bool MessageHandler::parse_from_iobuf(
124
    ::google::protobuf::Message* msg, const ::butil::IOBuf& buf) {
gejun's avatar
gejun committed
125 126 127 128
    if (parse == NULL) {
        LOG(ERROR) << "`parse' is NULL";
        return 0;
    }
129
    ::butil::IOBufAsZeroCopyInputStream zc_stream(buf);
gejun's avatar
gejun committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    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,
155
    ::butil::IOBuf* buf, SerializationFormat format) {
gejun's avatar
gejun committed
156 157 158 159
    if (serialize == NULL) {
        LOG(ERROR) << "`serialize' is NULL";
        return false;
    }
160
    ::butil::IOBufAsZeroCopyOutputStream zc_stream(buf);
gejun's avatar
gejun committed
161 162 163 164 165
    return serialize(msg, &zc_stream, format);
}

} // namespace mcpack2pb

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