2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* serialize.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef FLOW_SERIALIZE_H
|
|
|
|
#define FLOW_SERIALIZE_H
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2019-02-21 07:37:13 +08:00
|
|
|
#include <array>
|
2017-05-26 04:48:44 +08:00
|
|
|
#include <set>
|
2019-06-19 05:49:04 +08:00
|
|
|
#include "flow/ProtocolVersion.h"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "flow/Error.h"
|
|
|
|
#include "flow/Arena.h"
|
2019-01-26 07:10:20 +08:00
|
|
|
#include "flow/FileIdentifier.h"
|
|
|
|
#include "flow/ObjectSerializer.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include <algorithm>
|
2020-08-15 02:18:54 +08:00
|
|
|
#include <deque>
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2017-10-28 07:54:44 +08:00
|
|
|
// Though similar, is_binary_serializable cannot be replaced by std::is_pod, as doing so would prefer
|
|
|
|
// memcpy over a defined serialize() method on a POD struct. As not all of our structs are packed,
|
|
|
|
// this would both inflate message sizes by transmitting padding, and mean that we're transmitting
|
|
|
|
// undefined bytes over the wire.
|
|
|
|
// A more intelligent SFINAE that does "binarySerialize if POD and no serialize() is defined" could
|
|
|
|
// replace the usage of is_binary_serializable.
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class T>
|
|
|
|
struct is_binary_serializable { enum { value = 0 }; };
|
|
|
|
|
|
|
|
#define BINARY_SERIALIZABLE( T ) template<> struct is_binary_serializable<T> { enum { value = 1 }; };
|
|
|
|
|
2017-08-04 07:16:36 +08:00
|
|
|
BINARY_SERIALIZABLE( int8_t );
|
2017-05-26 04:48:44 +08:00
|
|
|
BINARY_SERIALIZABLE( uint8_t );
|
|
|
|
BINARY_SERIALIZABLE( int16_t );
|
|
|
|
BINARY_SERIALIZABLE( uint16_t );
|
|
|
|
BINARY_SERIALIZABLE( int32_t );
|
|
|
|
BINARY_SERIALIZABLE( uint32_t );
|
|
|
|
BINARY_SERIALIZABLE( int64_t );
|
|
|
|
BINARY_SERIALIZABLE( uint64_t );
|
|
|
|
BINARY_SERIALIZABLE( bool );
|
|
|
|
BINARY_SERIALIZABLE( double );
|
2019-06-19 05:49:04 +08:00
|
|
|
BINARY_SERIALIZABLE( ProtocolVersion );
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct scalar_traits<ProtocolVersion> : std::true_type {
|
|
|
|
constexpr static size_t size = sizeof(uint64_t);
|
|
|
|
|
|
|
|
static void save(uint8_t* out, const ProtocolVersion& v) {
|
|
|
|
*reinterpret_cast<uint64_t*>(out) = v.versionWithFlags();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Context>
|
|
|
|
static void load(const uint8_t* i, ProtocolVersion& out, Context& context) {
|
|
|
|
const uint64_t* in = reinterpret_cast<const uint64_t*>(i);
|
|
|
|
out = ProtocolVersion(*in);
|
|
|
|
}
|
|
|
|
};
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
template <class Archive, class Item>
|
|
|
|
inline typename Archive::WRITER& operator << (Archive& ar, const Item& item ) {
|
2020-06-21 10:03:33 +08:00
|
|
|
save(ar, item);
|
2017-05-26 04:48:44 +08:00
|
|
|
return ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive, class Item>
|
|
|
|
inline typename Archive::READER& operator >> (Archive& ar, Item& item ) {
|
2020-07-10 05:26:23 +08:00
|
|
|
ar.deserialize(item);
|
2017-05-26 04:48:44 +08:00
|
|
|
return ar;
|
|
|
|
}
|
|
|
|
|
2018-12-29 02:49:26 +08:00
|
|
|
template <class Archive, class Item, class... Items>
|
|
|
|
typename Archive::WRITER& serializer(Archive& ar, const Item& item, const Items&... items) {
|
2017-05-26 04:48:44 +08:00
|
|
|
save(ar, item);
|
2020-03-03 06:32:36 +08:00
|
|
|
if constexpr (sizeof...(Items) > 0) {
|
|
|
|
serializer(ar, items...);
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
return ar;
|
|
|
|
}
|
|
|
|
|
2018-12-29 02:49:26 +08:00
|
|
|
template <class Archive, class Item, class... Items>
|
|
|
|
typename Archive::READER& serializer(Archive& ar, Item& item, Items&... items) {
|
2017-05-26 04:48:44 +08:00
|
|
|
load(ar, item);
|
2020-03-03 06:32:36 +08:00
|
|
|
if constexpr (sizeof...(Items) > 0) {
|
|
|
|
serializer(ar, items...);
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
return ar;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive, class T, class Enable = void>
|
|
|
|
class Serializer {
|
|
|
|
public:
|
|
|
|
static void serialize( Archive& ar, T& t ) {
|
|
|
|
t.serialize(ar);
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Ar, class T>
|
|
|
|
inline void save( Ar& ar, const T& value ) {
|
|
|
|
Serializer<Ar,T>::serialize(ar, const_cast<T&>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Ar, class T>
|
|
|
|
inline void load( Ar& ar, T& value ) {
|
|
|
|
Serializer<Ar,T>::serialize(ar, value);
|
|
|
|
}
|
|
|
|
|
2019-01-26 07:10:20 +08:00
|
|
|
template <class CharT, class Traits, class Allocator>
|
|
|
|
struct FileIdentifierFor<std::basic_string<CharT, Traits, Allocator>> {
|
|
|
|
constexpr static FileIdentifier value = 15694229;
|
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class Archive>
|
|
|
|
inline void load( Archive& ar, std::string& value ) {
|
|
|
|
int32_t length;
|
|
|
|
ar >> length;
|
|
|
|
value.resize(length);
|
|
|
|
ar.serializeBytes( &value[0], (int)value.length() );
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive>
|
|
|
|
inline void save( Archive& ar, const std::string& value ) {
|
|
|
|
ar << (int32_t)value.length();
|
|
|
|
ar.serializeBytes( (void*)&value[0], (int)value.length() );
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive, class T>
|
2017-10-28 07:54:44 +08:00
|
|
|
class Serializer< Archive, T, typename std::enable_if< is_binary_serializable<T>::value >::type> {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
|
|
|
static void serialize( Archive& ar, T& t ) {
|
|
|
|
ar.serializeBinaryItem(t);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Archive, class T1, class T2>
|
|
|
|
class Serializer< Archive, std::pair<T1,T2>, void > {
|
|
|
|
public:
|
|
|
|
static void serialize( Archive& ar, std::pair<T1, T2>& p ) {
|
2018-12-29 02:49:26 +08:00
|
|
|
serializer(ar, p.first, p.second);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-26 07:10:20 +08:00
|
|
|
template <class T, class Allocator>
|
2020-07-15 07:26:16 +08:00
|
|
|
struct FileIdentifierFor<std::vector<T, Allocator>> : ComposedIdentifierExternal<T, 5> {};
|
|
|
|
|
|
|
|
template <class T, class Allocator>
|
|
|
|
struct CompositionDepthFor<std::vector<T, Allocator>> : std::integral_constant<int, CompositionDepthFor<T>::value + 1> {
|
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class Archive, class T>
|
|
|
|
inline void save( Archive& ar, const std::vector<T>& value ) {
|
|
|
|
ar << (int)value.size();
|
|
|
|
for(auto it = value.begin(); it != value.end(); ++it)
|
|
|
|
ar << *it;
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
template <class Archive, class T>
|
|
|
|
inline void load( Archive& ar, std::vector<T>& value ) {
|
2020-08-15 02:18:54 +08:00
|
|
|
int s;
|
|
|
|
ar >> s;
|
|
|
|
value.clear();
|
|
|
|
value.reserve(s);
|
|
|
|
for (int i = 0; i < s; i++) {
|
|
|
|
value.push_back(T());
|
|
|
|
ar >> value[i];
|
|
|
|
}
|
2020-08-30 03:35:31 +08:00
|
|
|
ASSERT(ar.protocolVersion().isValid());
|
2020-08-15 02:18:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive, class T>
|
2020-08-30 03:35:31 +08:00
|
|
|
inline void save(Archive& ar, const std::deque<T>& value) {
|
2020-08-15 02:18:54 +08:00
|
|
|
ar << (int)value.size();
|
2020-08-30 03:35:31 +08:00
|
|
|
for (auto it = value.begin(); it != value.end(); ++it) ar << *it;
|
|
|
|
ASSERT(ar.protocolVersion().isValid());
|
2020-08-15 02:18:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive, class T>
|
2020-08-30 03:35:31 +08:00
|
|
|
inline void load(Archive& ar, std::deque<T>& value) {
|
2017-05-26 04:48:44 +08:00
|
|
|
int s;
|
|
|
|
ar >> s;
|
|
|
|
value.clear();
|
|
|
|
value.reserve(s);
|
|
|
|
for (int i = 0; i < s; i++) {
|
|
|
|
value.push_back(T());
|
|
|
|
ar >> value[i];
|
|
|
|
}
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2019-02-21 07:37:13 +08:00
|
|
|
template <class Archive, class T, size_t N>
|
|
|
|
inline void save( Archive& ar, const std::array<T, N>& value ) {
|
|
|
|
for(int ii = 0; ii < N; ++ii)
|
|
|
|
ar << value[ii];
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2019-02-21 07:37:13 +08:00
|
|
|
}
|
|
|
|
template <class Archive, class T, size_t N>
|
|
|
|
inline void load( Archive& ar, std::array<T, N>& value ) {
|
|
|
|
for (int ii = 0; ii < N; ii++) {
|
|
|
|
ar >> value[ii];
|
|
|
|
}
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2019-02-21 07:37:13 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class Archive, class T>
|
|
|
|
inline void save( Archive& ar, const std::set<T>& value ) {
|
|
|
|
ar << (int)value.size();
|
|
|
|
for(auto it = value.begin(); it != value.end(); ++it)
|
|
|
|
ar << *it;
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
template <class Archive, class T>
|
|
|
|
inline void load( Archive& ar, std::set<T>& value ) {
|
|
|
|
int s;
|
|
|
|
ar >> s;
|
|
|
|
value.clear();
|
|
|
|
T currentValue;
|
|
|
|
for (int i = 0; i < s; i++) {
|
|
|
|
ar >> currentValue;
|
|
|
|
value.insert(currentValue);
|
|
|
|
}
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2019-02-01 03:24:04 +08:00
|
|
|
template <class Archive, class K, class V>
|
|
|
|
inline void save( Archive& ar, const std::map<K, V>& value ) {
|
|
|
|
ar << (int)value.size();
|
|
|
|
for (const auto &it : value) {
|
|
|
|
ar << it.first << it.second;
|
|
|
|
}
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2019-02-01 03:24:04 +08:00
|
|
|
}
|
|
|
|
template <class Archive, class K, class V>
|
|
|
|
inline void load( Archive& ar, std::map<K, V>& value ) {
|
|
|
|
int s;
|
|
|
|
ar >> s;
|
|
|
|
value.clear();
|
|
|
|
for (int i = 0; i < s; ++i) {
|
|
|
|
std::pair<K, V> p;
|
|
|
|
ar >> p.first >> p.second;
|
|
|
|
value.emplace(p);
|
|
|
|
}
|
2019-06-19 08:55:27 +08:00
|
|
|
ASSERT( ar.protocolVersion().isValid() );
|
2019-02-01 03:24:04 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
#pragma intrinsic (memcpy)
|
|
|
|
|
|
|
|
#if VALGRIND
|
|
|
|
static bool valgrindCheck( const void* data, int bytes, const char* context ) {
|
|
|
|
auto first= VALGRIND_CHECK_MEM_IS_DEFINED( data, bytes );
|
|
|
|
if (first) {
|
|
|
|
int und=0;
|
|
|
|
for(int b=0; b<bytes; b++)
|
|
|
|
if (VALGRIND_CHECK_MEM_IS_DEFINED( (uint8_t*)data+b, 1 ))
|
|
|
|
und++;
|
|
|
|
TraceEvent(SevError, "UndefinedData").detail("In", context).detail("Size", bytes).detail("Undefined", und).detail("FirstAt", (int64_t)first-(int64_t)data);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static inline bool valgrindCheck( const void* data, int bytes, const char* context ) { return true; }
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct _IncludeVersion {
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion v;
|
|
|
|
explicit _IncludeVersion( ProtocolVersion defaultVersion ) : v(defaultVersion) {
|
|
|
|
ASSERT( defaultVersion.isValid() );
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
template <class Ar>
|
|
|
|
void write( Ar& ar ) {
|
|
|
|
ar.setProtocolVersion(v);
|
|
|
|
ar << v;
|
|
|
|
}
|
|
|
|
template <class Ar>
|
|
|
|
void read( Ar& ar ) {
|
|
|
|
ar >> v;
|
2019-06-19 05:49:04 +08:00
|
|
|
if (!v.isValid()) {
|
2017-05-26 04:48:44 +08:00
|
|
|
auto err = incompatible_protocol_version();
|
2020-02-22 11:09:16 +08:00
|
|
|
TraceEvent(SevWarnAlways, "InvalidSerializationVersion").error(err).detailf("Version", "%llx", v.versionWithFlags());
|
2017-05-26 04:48:44 +08:00
|
|
|
throw err;
|
|
|
|
}
|
2020-07-10 05:39:35 +08:00
|
|
|
if (v >= minInvalidProtocolVersion) {
|
|
|
|
// Downgrades are only supported for one minor version
|
2017-05-26 04:48:44 +08:00
|
|
|
auto err = incompatible_protocol_version();
|
2019-06-21 00:29:01 +08:00
|
|
|
TraceEvent(SevError, "FutureProtocolVersion").error(err).detailf("Version", "%llx", v.versionWithFlags());
|
2017-05-26 04:48:44 +08:00
|
|
|
throw err;
|
|
|
|
}
|
|
|
|
ar.setProtocolVersion(v);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
struct _AssumeVersion {
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion v;
|
|
|
|
explicit _AssumeVersion( ProtocolVersion version );
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class Ar> void write( Ar& ar ) { ar.setProtocolVersion(v); }
|
|
|
|
template <class Ar> void read( Ar& ar ) { ar.setProtocolVersion(v); }
|
|
|
|
};
|
|
|
|
struct _Unversioned {
|
2019-06-19 05:49:04 +08:00
|
|
|
template <class Ar> void write( Ar& ar ) {}
|
|
|
|
template <class Ar> void read( Ar& ar ) {}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// These functions return valid options to the VersionOptions parameter of the constructor of each archive type
|
2019-06-19 05:49:04 +08:00
|
|
|
inline _IncludeVersion IncludeVersion( ProtocolVersion defaultVersion = currentProtocolVersion ) { return _IncludeVersion(defaultVersion); }
|
|
|
|
inline _AssumeVersion AssumeVersion( ProtocolVersion version ) { return _AssumeVersion(version); }
|
2017-05-26 04:48:44 +08:00
|
|
|
inline _Unversioned Unversioned() { return _Unversioned(); }
|
|
|
|
|
2019-03-30 04:21:15 +08:00
|
|
|
//static uint64_t size_limits[] = { 0ULL, 255ULL, 65535ULL, 16777215ULL, 4294967295ULL, 1099511627775ULL, 281474976710655ULL, 72057594037927935ULL, 18446744073709551615ULL };
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
class BinaryWriter : NonCopyable {
|
|
|
|
public:
|
|
|
|
static const int isDeserializing = 0;
|
2019-06-30 02:20:33 +08:00
|
|
|
static constexpr bool isSerializing = true;
|
2017-05-26 04:48:44 +08:00
|
|
|
typedef BinaryWriter WRITER;
|
|
|
|
|
|
|
|
void serializeBytes( StringRef bytes ) {
|
|
|
|
serializeBytes(bytes.begin(), bytes.size());
|
|
|
|
}
|
|
|
|
void serializeBytes(const void* data, int bytes) {
|
2019-12-04 05:09:29 +08:00
|
|
|
if (bytes > 0) {
|
|
|
|
valgrindCheck(data, bytes, "serializeBytes");
|
|
|
|
void* p = writeBytes(bytes);
|
|
|
|
memcpy(p, data, bytes);
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
void serializeBinaryItem( const T& t ) {
|
|
|
|
*(T*)writeBytes(sizeof(T)) = t;
|
|
|
|
}
|
|
|
|
void* getData() { return data; }
|
|
|
|
int getLength() { return size; }
|
2019-03-29 02:52:50 +08:00
|
|
|
Standalone<StringRef> toValue() { return Standalone<StringRef>( StringRef(data,size), arena ); }
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class VersionOptions>
|
2020-08-28 06:31:24 +08:00
|
|
|
explicit BinaryWriter( VersionOptions vo ) : data(nullptr), size(0), allocated(0) { vo.write(*this); }
|
2019-03-27 08:27:33 +08:00
|
|
|
BinaryWriter( BinaryWriter&& rhs ) : arena(std::move(rhs.arena)), data(rhs.data), size(rhs.size), allocated(rhs.allocated), m_protocolVersion(rhs.m_protocolVersion) {
|
2017-05-26 04:48:44 +08:00
|
|
|
rhs.size = 0;
|
|
|
|
rhs.allocated = 0;
|
|
|
|
rhs.data = 0;
|
|
|
|
}
|
|
|
|
void operator=( BinaryWriter&& r) {
|
2019-03-27 08:27:33 +08:00
|
|
|
arena = std::move(r.arena);
|
2017-05-26 04:48:44 +08:00
|
|
|
data = r.data;
|
|
|
|
size = r.size;
|
|
|
|
allocated = r.allocated;
|
|
|
|
m_protocolVersion = r.m_protocolVersion;
|
|
|
|
r.size = 0;
|
|
|
|
r.allocated = 0;
|
|
|
|
r.data = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class VersionOptions>
|
|
|
|
static Standalone<StringRef> toValue( T const& t, VersionOptions vo ) {
|
|
|
|
BinaryWriter wr(vo);
|
|
|
|
wr << t;
|
2019-03-29 02:52:50 +08:00
|
|
|
return wr.toValue();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int bytesNeeded( uint64_t val ) {
|
|
|
|
int n;
|
|
|
|
for( n=1; n<8 && (val>>(n*8)); ++n );
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeAsTuple( StringRef str ) {
|
|
|
|
size_t last_pos = 0;
|
|
|
|
|
|
|
|
serializeBytes(LiteralStringRef("\x01"));
|
|
|
|
|
|
|
|
for (size_t pos = 0; pos < str.size(); ++pos) {
|
|
|
|
if (str[pos] == '\x00') {
|
|
|
|
serializeBytes(str.substr(last_pos,pos - last_pos));
|
|
|
|
serializeBytes(LiteralStringRef("\x00\xff"));
|
|
|
|
last_pos = pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
serializeBytes(str.substr(last_pos,str.size() - last_pos));
|
|
|
|
serializeBytes(LiteralStringRef("\x00"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeAsTuple( bool t ) {
|
|
|
|
if(!t) {
|
|
|
|
void* p = writeBytes(1);
|
|
|
|
((uint8_t*)p)[0] = (uint8_t)20;
|
|
|
|
} else {
|
|
|
|
void* p = writeBytes(2);
|
|
|
|
((uint8_t*)p)[0] = (uint8_t)21;
|
|
|
|
((uint8_t*)p)[1] = (uint8_t)1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeAsTuple( uint64_t t ) {
|
|
|
|
if(t == 0) {
|
|
|
|
void* p = writeBytes(1);
|
|
|
|
((uint8_t*)p)[0] = (uint8_t)20;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//int n = ( std::lower_bound(size_limits, size_limits+9, t) - size_limits );
|
|
|
|
//ASSERT( n <= 8 );
|
|
|
|
int n = bytesNeeded(t);
|
|
|
|
void* p = writeBytes(n+1);
|
|
|
|
((uint8_t*)p)[0] = (uint8_t)(20+n);
|
|
|
|
uint64_t x = bigEndian64(t);
|
|
|
|
memcpy((uint8_t*)p+1, (uint8_t*)&x+(8-n), n);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeAsTuple( int64_t t ) {
|
|
|
|
if(t == 0) {
|
|
|
|
void* p = writeBytes(1);
|
|
|
|
((uint8_t*)p)[0] = (uint8_t)20;
|
|
|
|
} else if(t > 0) {
|
|
|
|
//int n = ( std::lower_bound(size_limits, size_limits+9, t) - size_limits );
|
|
|
|
//ASSERT( n <= 9 );
|
|
|
|
int n = bytesNeeded(t);
|
|
|
|
|
|
|
|
void* p = writeBytes(n+1);
|
|
|
|
((uint8_t*)p)[0] = (uint8_t)(20+n);
|
|
|
|
uint64_t x = bigEndian64((uint64_t)t);
|
|
|
|
memcpy((uint8_t*)p+1, (uint8_t*)&x+(8-n), n);
|
|
|
|
} else {
|
|
|
|
//int n = ( std::lower_bound(size_limits, size_limits+9, -t) - size_limits );
|
|
|
|
//ASSERT( n <= 9 );
|
|
|
|
int n = bytesNeeded(-t);
|
|
|
|
|
|
|
|
void* p = writeBytes(n+1);
|
|
|
|
((uint8_t*)p)[0] = (uint8_t)(20-n);
|
|
|
|
uint64_t x = bigEndian64(t-1);
|
|
|
|
memcpy((uint8_t*)p+1, (uint8_t*)&x+(8-n), n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion protocolVersion() const { return m_protocolVersion; }
|
|
|
|
void setProtocolVersion(ProtocolVersion pv) { m_protocolVersion = pv; }
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2019-03-27 08:27:33 +08:00
|
|
|
Arena arena;
|
2017-05-26 04:48:44 +08:00
|
|
|
uint8_t* data;
|
|
|
|
int size, allocated;
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion m_protocolVersion;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void* writeBytes(int s) {
|
|
|
|
int p = size;
|
|
|
|
size += s;
|
|
|
|
if (size > allocated) {
|
2019-03-28 10:54:58 +08:00
|
|
|
if(size <= 512-sizeof(ArenaBlock)) {
|
|
|
|
allocated = 512-sizeof(ArenaBlock);
|
|
|
|
} else if(size <= 4096-sizeof(ArenaBlock)) {
|
|
|
|
allocated = 4096-sizeof(ArenaBlock);
|
2019-03-27 03:06:39 +08:00
|
|
|
} else {
|
2019-03-27 08:27:33 +08:00
|
|
|
allocated = std::max(allocated*2, size);
|
2019-03-27 03:06:39 +08:00
|
|
|
}
|
2019-03-27 08:27:33 +08:00
|
|
|
Arena newArena;
|
|
|
|
uint8_t* newData = new ( newArena ) uint8_t[ allocated ];
|
2019-12-04 05:09:29 +08:00
|
|
|
if (p > 0) {
|
|
|
|
memcpy(newData, data, p);
|
|
|
|
}
|
2019-03-27 08:27:33 +08:00
|
|
|
arena = newArena;
|
2017-05-26 04:48:44 +08:00
|
|
|
data = newData;
|
|
|
|
}
|
|
|
|
return data+p;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// A known-length memory segment and an unknown-length memory segment which can be written to as a whole.
|
|
|
|
struct SplitBuffer {
|
|
|
|
void write(const void* data, int length);
|
|
|
|
void write(const void* data, int length, int offset);
|
|
|
|
void writeAndShrink(const void* data, int length);
|
|
|
|
uint8_t *begin, *next;
|
|
|
|
int first_length;
|
|
|
|
};
|
|
|
|
|
|
|
|
// A writer that can serialize to a SplitBuffer
|
|
|
|
class OverWriter {
|
|
|
|
public:
|
|
|
|
typedef OverWriter WRITER;
|
|
|
|
|
|
|
|
template <class VersionOptions>
|
|
|
|
explicit OverWriter(SplitBuffer buf, VersionOptions vo) : buf(buf), len(std::numeric_limits<int>::max()) { vo.write(*this); }
|
|
|
|
|
|
|
|
template <class VersionOptions>
|
|
|
|
explicit OverWriter(void *ptr, int len, VersionOptions vo) : len(len) {
|
|
|
|
buf.begin = (uint8_t *)ptr;
|
|
|
|
buf.first_length = len;
|
|
|
|
vo.write(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void serializeBytes( StringRef bytes ) {
|
|
|
|
serializeBytes(bytes.begin(), bytes.size());
|
|
|
|
}
|
|
|
|
void serializeBytes(const void* data, int bytes) {
|
|
|
|
valgrindCheck( data, bytes, "serializeBytes" );
|
|
|
|
writeBytes(data, bytes);
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
void serializeBinaryItem( const T& t ) {
|
|
|
|
writeBytes(&t, sizeof(T));
|
|
|
|
}
|
|
|
|
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion protocolVersion() const { return m_protocolVersion; }
|
|
|
|
void setProtocolVersion(ProtocolVersion pv) { m_protocolVersion = pv; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
int len;
|
|
|
|
SplitBuffer buf;
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion m_protocolVersion;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void writeBytes(const void *data, int wlen) {
|
|
|
|
ASSERT(wlen <= len);
|
|
|
|
buf.writeAndShrink(data, wlen);
|
|
|
|
len -= wlen;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
template<class Impl>
|
|
|
|
class _Reader {
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
|
|
|
static const int isDeserializing = 1;
|
2019-06-30 02:20:33 +08:00
|
|
|
static constexpr bool isSerializing = false;
|
2020-07-07 14:14:34 +08:00
|
|
|
using READER = Impl;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-07-29 15:31:55 +08:00
|
|
|
const void *peekBytes(int bytes) const {
|
2020-07-07 14:14:34 +08:00
|
|
|
ASSERT(begin + bytes <= end);
|
2017-05-26 04:48:44 +08:00
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
void serializeBytes(void *data, int bytes) {
|
|
|
|
memcpy(data, static_cast<Impl*>(this)->readBytes(bytes), bytes);
|
2019-01-26 07:10:20 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class T>
|
|
|
|
void serializeBinaryItem( T& t ) {
|
2020-07-07 14:14:34 +08:00
|
|
|
t = *(T*)(static_cast<Impl*>(this)->readBytes(sizeof(T)));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
Arena &arena() { return m_pool; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion protocolVersion() const { return m_protocolVersion; }
|
|
|
|
void setProtocolVersion(ProtocolVersion pv) { m_protocolVersion = pv; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
bool empty() const { return begin == end; }
|
|
|
|
|
2018-03-17 02:40:21 +08:00
|
|
|
void checkpoint() {
|
|
|
|
check = begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rewind() {
|
2020-07-07 14:14:34 +08:00
|
|
|
ASSERT(check != nullptr);
|
2018-03-17 02:40:21 +08:00
|
|
|
begin = check;
|
2020-07-07 14:14:34 +08:00
|
|
|
check = nullptr;
|
2018-03-17 02:40:21 +08:00
|
|
|
}
|
|
|
|
|
2020-07-10 05:26:23 +08:00
|
|
|
protected:
|
2020-07-08 07:35:46 +08:00
|
|
|
_Reader(const char* begin, const char* end) : begin(begin), end(end) {}
|
|
|
|
_Reader(const char* begin, const char* end, const Arena& arena) : begin(begin), end(end), m_pool(arena) {}
|
|
|
|
|
|
|
|
const char *begin, *end;
|
|
|
|
const char* check = nullptr;
|
2017-05-26 04:48:44 +08:00
|
|
|
Arena m_pool;
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion m_protocolVersion;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
class ArenaReader : public _Reader<ArenaReader> {
|
2020-07-10 05:26:23 +08:00
|
|
|
Optional<ArenaObjectReader> arenaObjectReader;
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
public:
|
2020-07-07 14:14:34 +08:00
|
|
|
const void* readBytes( int bytes ) {
|
|
|
|
const char* b = begin;
|
|
|
|
const char* e = b + bytes;
|
|
|
|
ASSERT( e <= end );
|
|
|
|
begin = e;
|
|
|
|
return b;
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
const uint8_t* arenaRead( int bytes ) {
|
|
|
|
return (const uint8_t*)readBytes(bytes);
|
2019-02-08 09:02:36 +08:00
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
const void* peekBytes(int bytes) const {
|
2019-02-08 09:02:36 +08:00
|
|
|
ASSERT( begin + bytes <= end );
|
|
|
|
return begin;
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
StringRef arenaReadAll() const {
|
|
|
|
return StringRef(reinterpret_cast<const uint8_t*>(begin), end - begin);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2019-02-08 09:02:36 +08:00
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
template <class VersionOptions>
|
2020-07-08 07:35:46 +08:00
|
|
|
ArenaReader(Arena const& arena, const StringRef& input, VersionOptions vo)
|
|
|
|
: _Reader(reinterpret_cast<const char*>(input.begin()), reinterpret_cast<const char*>(input.end()), arena) {
|
2020-07-07 14:14:34 +08:00
|
|
|
vo.read(*this);
|
2020-07-10 05:26:23 +08:00
|
|
|
if (m_protocolVersion.hasObjectSerializerFlag()) {
|
|
|
|
arenaObjectReader = ArenaObjectReader(arena, input, vo);
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2020-07-10 05:26:23 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
template <class T>
|
2020-07-10 05:26:23 +08:00
|
|
|
void deserialize(T& t) {
|
|
|
|
if constexpr (HasFileIdentifier<T>::value) {
|
|
|
|
if (arenaObjectReader.present()) {
|
|
|
|
arenaObjectReader.get().deserialize(t);
|
|
|
|
} else {
|
|
|
|
load(*this, t);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
load(*this, t);
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2020-07-07 14:14:34 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class BinaryReader : public _Reader<BinaryReader> {
|
2020-07-10 05:26:23 +08:00
|
|
|
Optional<ObjectReader> objectReader;
|
2020-07-08 07:03:12 +08:00
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
public:
|
|
|
|
const void* readBytes( int bytes );
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
const uint8_t* arenaRead( int bytes ) {
|
|
|
|
// Reads and returns the next bytes.
|
|
|
|
// The returned pointer has the lifetime of this.arena()
|
|
|
|
// Could be implemented zero-copy if [begin,end) was in this.arena() already; for now is a copy
|
2020-07-07 14:14:34 +08:00
|
|
|
if (!bytes) return nullptr;
|
2017-05-26 04:48:44 +08:00
|
|
|
uint8_t* dat = new (arena()) uint8_t[ bytes ];
|
|
|
|
serializeBytes( dat, bytes );
|
|
|
|
return dat;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T, class VersionOptions>
|
|
|
|
static T fromStringRef( StringRef sr, VersionOptions vo ) {
|
|
|
|
T t;
|
|
|
|
BinaryReader r(sr, vo);
|
|
|
|
r >> t;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion protocolVersion() const { return m_protocolVersion; }
|
|
|
|
void setProtocolVersion(ProtocolVersion pv) { m_protocolVersion = pv; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
void assertEnd() const { ASSERT(begin == end); }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
bool empty() const { return begin == end; }
|
|
|
|
|
2020-07-07 14:14:34 +08:00
|
|
|
template <class VersionOptions>
|
2020-07-08 07:35:46 +08:00
|
|
|
BinaryReader(const void* data, int length, VersionOptions vo)
|
|
|
|
: _Reader(reinterpret_cast<const char*>(data), reinterpret_cast<const char*>(data) + length) {
|
2020-07-10 05:26:23 +08:00
|
|
|
readVersion(vo);
|
2019-02-08 09:02:36 +08:00
|
|
|
}
|
2020-07-07 14:14:34 +08:00
|
|
|
template <class VersionOptions>
|
2020-07-08 07:35:46 +08:00
|
|
|
BinaryReader(const StringRef& s, VersionOptions vo)
|
|
|
|
: _Reader(reinterpret_cast<const char*>(s.begin()), reinterpret_cast<const char*>(s.end())) {
|
2020-07-10 05:26:23 +08:00
|
|
|
readVersion(vo);
|
2020-07-07 14:14:34 +08:00
|
|
|
}
|
|
|
|
template <class VersionOptions>
|
2020-07-08 07:35:46 +08:00
|
|
|
BinaryReader(const std::string& s, VersionOptions vo) : _Reader(s.c_str(), s.c_str() + s.size()) {
|
2020-07-10 05:26:23 +08:00
|
|
|
readVersion(vo);
|
2019-02-08 09:02:36 +08:00
|
|
|
}
|
|
|
|
|
2020-07-08 07:03:12 +08:00
|
|
|
template<class T>
|
|
|
|
void deserialize(T &t) {
|
2020-07-10 05:26:23 +08:00
|
|
|
if constexpr (HasFileIdentifier<T>::value) {
|
|
|
|
if (objectReader.present()) {
|
|
|
|
objectReader.get().deserialize(t);
|
|
|
|
} else {
|
|
|
|
load(*this, t);
|
|
|
|
}
|
2020-07-08 07:03:12 +08:00
|
|
|
} else {
|
2020-07-10 05:26:23 +08:00
|
|
|
load(*this, t);
|
2020-07-08 07:03:12 +08:00
|
|
|
}
|
|
|
|
}
|
2019-02-08 09:02:36 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2020-07-10 05:26:23 +08:00
|
|
|
template <class VersionOptions>
|
|
|
|
void readVersion(VersionOptions vo) {
|
|
|
|
vo.read(*this);
|
|
|
|
if (m_protocolVersion.hasObjectSerializerFlag()) {
|
|
|
|
objectReader = ObjectReader(reinterpret_cast<const uint8_t*>(begin), AssumeVersion(m_protocolVersion));
|
|
|
|
}
|
2019-02-08 09:02:36 +08:00
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2020-06-21 10:03:33 +08:00
|
|
|
class SendBuffer {
|
2020-07-03 04:13:30 +08:00
|
|
|
protected:
|
2020-06-21 10:03:33 +08:00
|
|
|
uint8_t* _data;
|
|
|
|
|
|
|
|
public:
|
2020-06-21 13:38:40 +08:00
|
|
|
inline uint8_t const* data() const { return _data; }
|
2020-07-03 04:13:30 +08:00
|
|
|
inline uint8_t* data() { return _data; }
|
2017-05-26 04:48:44 +08:00
|
|
|
SendBuffer* next;
|
2019-07-02 00:57:50 +08:00
|
|
|
int bytes_written, bytes_sent;
|
2020-06-26 11:44:43 +08:00
|
|
|
int bytes_unsent() const {
|
|
|
|
return bytes_written - bytes_sent;
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2019-07-02 00:57:50 +08:00
|
|
|
struct PacketBuffer : SendBuffer {
|
|
|
|
private:
|
2017-05-26 04:48:44 +08:00
|
|
|
int reference_count;
|
2019-07-02 00:57:50 +08:00
|
|
|
uint32_t size_;
|
2020-06-26 11:44:43 +08:00
|
|
|
static constexpr size_t PACKET_BUFFER_MIN_SIZE = 16384;
|
2019-07-02 00:57:50 +08:00
|
|
|
static constexpr size_t PACKET_BUFFER_OVERHEAD = 32;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2019-07-02 00:57:50 +08:00
|
|
|
public:
|
2020-06-21 10:03:33 +08:00
|
|
|
size_t size() const { return size_; }
|
2019-07-02 00:57:50 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
explicit PacketBuffer(size_t size) : reference_count(1), size_(size) {
|
2020-06-21 10:03:33 +08:00
|
|
|
next = nullptr;
|
2017-05-26 04:48:44 +08:00
|
|
|
bytes_written = bytes_sent = 0;
|
2020-07-03 04:13:30 +08:00
|
|
|
_data = reinterpret_cast<uint8_t*>(this + 1);
|
2019-07-02 00:57:50 +08:00
|
|
|
static_assert(sizeof(PacketBuffer) == PACKET_BUFFER_OVERHEAD);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
static PacketBuffer* create(size_t size = 0) {
|
2020-06-26 11:44:43 +08:00
|
|
|
size = std::max(size, PACKET_BUFFER_MIN_SIZE - PACKET_BUFFER_OVERHEAD);
|
|
|
|
if (size == PACKET_BUFFER_MIN_SIZE - PACKET_BUFFER_OVERHEAD) {
|
|
|
|
return new (FastAllocator<PACKET_BUFFER_MIN_SIZE>::allocate()) PacketBuffer{ size };
|
2019-07-02 00:57:50 +08:00
|
|
|
}
|
|
|
|
uint8_t* mem = new uint8_t[size + PACKET_BUFFER_OVERHEAD];
|
|
|
|
return new (mem) PacketBuffer{ size };
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2020-06-21 10:03:33 +08:00
|
|
|
PacketBuffer* nextPacketBuffer() { return static_cast<PacketBuffer*>(next); }
|
2017-05-26 04:48:44 +08:00
|
|
|
void addref() { ++reference_count; }
|
2019-07-02 00:57:50 +08:00
|
|
|
void delref() {
|
|
|
|
if (!--reference_count) {
|
2020-06-26 11:44:43 +08:00
|
|
|
if (size_ == PACKET_BUFFER_MIN_SIZE - PACKET_BUFFER_OVERHEAD) {
|
|
|
|
FastAllocator<PACKET_BUFFER_MIN_SIZE>::release(this);
|
2019-07-02 00:57:50 +08:00
|
|
|
} else {
|
|
|
|
delete[] this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int bytes_unwritten() const { return size_ - bytes_written; }
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct PacketWriter {
|
|
|
|
static const int isDeserializing = 0;
|
2019-07-02 02:29:06 +08:00
|
|
|
static constexpr bool isSerializing = true;
|
2017-05-26 04:48:44 +08:00
|
|
|
typedef PacketWriter WRITER;
|
|
|
|
|
|
|
|
PacketBuffer* buffer;
|
2020-08-28 06:31:24 +08:00
|
|
|
struct ReliablePacket *reliable; // nullptr if this is unreliable; otherwise the last entry in the ReliablePacket::cont chain
|
2017-05-26 04:48:44 +08:00
|
|
|
int length;
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion m_protocolVersion;
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-08-28 06:31:24 +08:00
|
|
|
// reliable is nullptr if this is an unreliable packet, or points to a ReliablePacket. PacketWriter is responsible
|
2017-05-26 04:48:44 +08:00
|
|
|
// for filling in reliable->buffer, ->cont, ->begin, and ->end, but not ->prev or ->next.
|
|
|
|
template <class VersionOptions>
|
|
|
|
PacketWriter(PacketBuffer* buf, ReliablePacket* reliable, VersionOptions vo) { init(buf, reliable); vo.read(*this); }
|
|
|
|
|
|
|
|
void serializeBytes(const void* data, int bytes) {
|
|
|
|
if (bytes <= buffer->bytes_unwritten()) {
|
2019-07-02 00:57:50 +08:00
|
|
|
memcpy(buffer->data() + buffer->bytes_written, data, bytes);
|
2017-05-26 04:48:44 +08:00
|
|
|
buffer->bytes_written += bytes;
|
|
|
|
} else {
|
|
|
|
serializeBytesAcrossBoundary(data, bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void serializeBytesAcrossBoundary(const void* data, int bytes);
|
|
|
|
void writeAhead( int bytes, struct SplitBuffer* );
|
2019-07-02 09:14:32 +08:00
|
|
|
void nextBuffer(size_t size = 0 /* downstream it will default to at least 4k minus some padding */);
|
2017-05-26 04:48:44 +08:00
|
|
|
PacketBuffer* finish();
|
2020-06-21 10:03:33 +08:00
|
|
|
int size() const { return length; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
void serializeBytes( StringRef bytes ) {
|
|
|
|
serializeBytes(bytes.begin(), bytes.size());
|
|
|
|
}
|
|
|
|
template <class T>
|
|
|
|
void serializeBinaryItem( const T& t ) {
|
|
|
|
if (sizeof(T) <= buffer->bytes_unwritten()) {
|
2019-07-02 00:57:50 +08:00
|
|
|
*(T*)(buffer->data() + buffer->bytes_written) = t;
|
2017-05-26 04:48:44 +08:00
|
|
|
buffer->bytes_written += sizeof(T);
|
|
|
|
} else {
|
|
|
|
serializeBytesAcrossBoundary(&t, sizeof(T));
|
|
|
|
}
|
|
|
|
}
|
2019-06-19 05:49:04 +08:00
|
|
|
ProtocolVersion protocolVersion() const { return m_protocolVersion; }
|
|
|
|
void setProtocolVersion(ProtocolVersion pv) { m_protocolVersion = pv; }
|
2019-07-02 09:14:32 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
private:
|
2019-07-02 09:14:32 +08:00
|
|
|
uint8_t* writeBytes(size_t size) {
|
|
|
|
if (size > buffer->bytes_unwritten()) {
|
|
|
|
nextBuffer(size);
|
|
|
|
ASSERT(buffer->size() >= size);
|
|
|
|
}
|
|
|
|
uint8_t* result = buffer->data() + buffer->bytes_written;
|
|
|
|
buffer->bytes_written += size;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class, class>
|
|
|
|
friend class MakeSerializeSource;
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
void init( PacketBuffer* buf, ReliablePacket* reliable );
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ISerializeSource {
|
2020-02-13 02:41:52 +08:00
|
|
|
virtual void serializePacketWriter(PacketWriter&) const = 0;
|
2019-04-18 01:49:19 +08:00
|
|
|
virtual void serializeObjectWriter(ObjectWriter&) const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
2019-01-29 11:38:13 +08:00
|
|
|
template <class T, class V>
|
2017-05-26 04:48:44 +08:00
|
|
|
struct MakeSerializeSource : ISerializeSource {
|
2019-01-29 11:38:13 +08:00
|
|
|
using value_type = V;
|
2020-02-13 02:41:52 +08:00
|
|
|
virtual void serializePacketWriter(PacketWriter& w) const {
|
|
|
|
ObjectWriter writer([&](size_t size) { return w.writeBytes(size); }, AssumeVersion(w.protocolVersion()));
|
|
|
|
writer.serialize(get()); // Writes directly into buffer supplied by |w|
|
2019-01-26 07:10:20 +08:00
|
|
|
}
|
2019-01-29 11:38:13 +08:00
|
|
|
virtual value_type const& get() const = 0;
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2019-01-29 11:38:13 +08:00
|
|
|
struct SerializeSource : MakeSerializeSource<SerializeSource<T>, T> {
|
|
|
|
using value_type = T;
|
2017-05-26 04:48:44 +08:00
|
|
|
T const& value;
|
|
|
|
SerializeSource(T const& value) : value(value) {}
|
2019-04-18 01:49:19 +08:00
|
|
|
virtual void serializeObjectWriter(ObjectWriter& w) const {
|
2019-01-31 05:53:23 +08:00
|
|
|
w.serialize(value);
|
|
|
|
}
|
2019-01-26 07:10:20 +08:00
|
|
|
virtual T const& get() const { return value; }
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|