25#ifndef GOBY_ACOMMS_DCCL_DCCL_H
26#define GOBY_ACOMMS_DCCL_DCCL_H
34#include <dccl/bitset.h>
35#include <dccl/codecs2/field_codec_default_message.h>
36#include <dccl/common.h>
37#include <dccl/exception.h>
38#include <dccl/field_codec.h>
39#include <dccl/field_codec_fixed.h>
40#include <dccl/field_codec_manager.h>
41#include <dccl/field_codec_typed.h>
42#include <dccl/logger.h>
43#include <dccl/option_extensions.pb.h>
44#include <google/protobuf/descriptor.h>
45#include <google/protobuf/descriptor.pb.h>
47#include "dccl/codec.h"
48#include "dccl/codecs2/field_codec_default.h"
49#include "dccl/field_codec_id.h"
73template <
typename WireType,
typename FieldType = WireType>
85 static_assert(
sizeof(
TimeCodec) == 0,
"TimeCodec must be specialized");
106template <
typename WireType,
typename FieldType = WireType>
112template <
typename WireType,
typename FieldType = WireType>
118template <
typename WireType,
typename FieldType = WireType>
139 std::shared_ptr<dccl::Codec>
codec() {
return codec_; }
151 process_cfg(new_id_codec);
156 codec_->load_library(dl_handle);
157 loaded_libs_.insert(dl_handle);
160 template <
typename ProtobufMessage>
void validate() {
validate(ProtobufMessage::descriptor()); }
162 template <
typename ProtobufMessage>
void info(std::ostream* os)
const
164 info(ProtobufMessage::descriptor(), os);
167 void info_all(std::ostream* os)
const { codec_->info_all(os); }
169 template <
typename ProtobufMessage>
unsigned id()
const
171 return id(ProtobufMessage::descriptor());
182 codec_->encode(bytes,
msg, header_only);
187 codec_->decode(bytes,
msg, header_only);
192 void validate(
const google::protobuf::Descriptor* desc)
195 loaded_msgs_.insert(desc);
200 for (
const google::protobuf::Descriptor* p : descs)
validate(p);
203 void info(
const google::protobuf::Descriptor* desc, std::ostream* os)
const
205 codec_->info(desc, os);
208 void info_repeated(
const std::list<const google::protobuf::Descriptor*>& desc,
209 std::ostream* os)
const
211 for (
const google::protobuf::Descriptor* p : desc)
info(p, os);
214 unsigned id(
const google::protobuf::Descriptor* desc)
const
216 return desc->options().GetExtension(dccl::msg).id();
219 template <
typename GoogleProtobufMessagePo
inter>
223 for (
const GoogleProtobufMessagePointer&
msg : msgs) out +=
size(*
msg);
227 template <
typename GoogleProtobufMessagePo
inter>
228 GoogleProtobufMessagePointer
decode(
const std::string& bytes,
bool header_only =
false)
230 return codec_->decode<GoogleProtobufMessagePointer>(bytes, header_only);
233 template <
typename GoogleProtobufMessagePo
inter>
237 for (
const GoogleProtobufMessagePointer&
msg : msgs)
247 template <
typename GoogleProtobufMessagePo
inter>
248 std::list<GoogleProtobufMessagePointer>
decode_repeated(
const std::string& orig_bytes)
250 std::string bytes = orig_bytes;
251 std::list<GoogleProtobufMessagePointer> out;
252 while (!bytes.empty())
256 out.push_back(decode<GoogleProtobufMessagePointer>(bytes));
257 unsigned last_size =
size(*out.back());
260 bytes.erase(0, last_size);
262 catch (dccl::Exception& e)
270 <<
" but returning parts already decoded" << std::endl;
278 template <
typename DCCLTypedFieldCodecU
int32>
void add_id_codec(
const std::string& identifier)
280#ifdef DCCL_VERSION_4_1_OR_NEWER
281 codec()->manager().add<DCCLTypedFieldCodecUint32>(identifier);
283 dccl::FieldCodecManager::add<DCCLTypedFieldCodecUint32>(identifier);
289 codec_.reset(
new dccl::Codec(identifier));
293 for (
auto loaded_msg : loaded_msgs_)
299 catch (dccl::Exception& e)
302 glog <<
"Failed to reload " << loaded_msg->full_name()
303 <<
" after ID codec change: " << e.what() << std::endl;
319 void process_cfg(
bool new_id_codec)
323 std::set<unsigned> skip_crypto_ids;
335 void dlog_message(
const std::string&
msg, dccl::logger::Verbosity vrb, dccl::logger::Group grp)
337 std::string glog_group =
338 (grp == dccl::logger::DECODE) ? glog_decode_group_ : glog_encode_group_;
339 std::string prefix = (grp == dccl::logger::SIZE) ?
" {size} " :
"";
343 case dccl::logger::WARN_PLUS:
346 case dccl::logger::INFO_PLUS:
348 case dccl::logger::DEBUG1_PLUS:
350 case dccl::logger::DEBUG2_PLUS:
352 case dccl::logger::DEBUG3_PLUS:
360 static std::string glog_encode_group_;
361 static std::string glog_decode_group_;
363 protobuf::DCCLConfig cfg_;
365 std::shared_ptr<dccl::Codec> codec_;
367 std::set<void*> loaded_libs_;
368 std::set<const google::protobuf::Descriptor*> loaded_msgs_;
std::shared_ptr< dccl::Codec > codec()
Return the underlying dccl::Codec that is used by this wrapper.
static DCCLCodec * get()
DCCLCodec is a singleton class; use this to get a pointer to the class.
std::string encode_repeated(const std::list< GoogleProtobufMessagePointer > &msgs)
void encode(std::string *bytes, const google::protobuf::Message &msg, bool header_only=false)
void info(const google::protobuf::Descriptor *desc, std::ostream *os) const
unsigned id(const google::protobuf::Descriptor *desc) const
unsigned size(const google::protobuf::Message &msg)
void load_shared_library_codecs(void *dl_handle)
void set_cfg(const protobuf::DCCLConfig &cfg)
void info_repeated(const std::list< const google::protobuf::Descriptor * > &desc, std::ostream *os) const
void info_all(std::ostream *os) const
static const std::string & glog_decode_group()
unsigned id_from_encoded(const std::string &bytes)
void merge_cfg(const protobuf::DCCLConfig &cfg)
void validate_repeated(const std::list< const google::protobuf::Descriptor * > &descs)
void add_id_codec(const std::string &identifier)
void info(std::ostream *os) const
unsigned size_repeated(const std::list< GoogleProtobufMessagePointer > &msgs)
void set_id_codec(const std::string &identifier)
static const std::string & glog_encode_group()
void decode(const std::string &bytes, google::protobuf::Message *msg, bool header_only=false)
void validate(const google::protobuf::Descriptor *desc)
GoogleProtobufMessagePointer decode(const std::string &bytes, bool header_only=false)
std::list< GoogleProtobufMessagePointer > decode_repeated(const std::string &orig_bytes)
int skip_crypto_for_id_size() const
bool has_id_codec() const
const std::string & id_codec() const
uint32_t skip_crypto_for_id(int index) const
void MergeFrom(const DCCLConfig &from)
void CopyFrom(const DCCLConfig &from)
bool has_crypto_passphrase() const
const std::string & crypto_passphrase() const
bool is(goby::util::logger::Verbosity verbosity)
goby::util::logger::GroupSetter group(std::string n)
dccl::v2::DefaultBoolCodec DCCLDefaultBoolCodec
dccl::FieldCodecBase DCCLFieldCodecBase
dccl::FieldCodecManager DCCLFieldCodecManager
dccl::v2::DefaultBytesCodec DCCLDefaultBytesCodec
std::ostream & operator<<(std::ostream &os, const MACManager &mac)
dccl::DefaultIdentifierCodec DCCLDefaultIdentifierCodec
dccl::v2::DefaultEnumCodec DCCLDefaultEnumCodec
dccl::NullValueException DCCLNullValueException
dccl::Exception DCCLException
dccl::v2::DefaultStringCodec DCCLDefaultStringCodec
dccl::v2::DefaultMessageCodec DCCLDefaultMessageCodec
void hex_encode(const std::string &in, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of in is written to index 0 ...
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
util::FlexOstream glog
Access the Goby logger through this object.
dccl::FieldCodecBase DCCLFieldCodecBase
dccl::FieldCodecBase DCCLFieldCodecBase
dccl::FieldCodecBase DCCLFieldCodecBase