forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement StandardMethodCodec for C++ shells (flutter#8598)
Adds StandardMethodCodec support to the C++ client wrapper. This makes it substantially easier to add Windows and Linux support for existing plugins, as StandardMethodCodec is the default plugin protocol. Fixes flutter/flutter#30670 Does not include extensibility for the codec, which will be added later.
- Loading branch information
1 parent
388124f
commit 4805d72
Showing
15 changed files
with
1,046 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
shell/platform/common/cpp/client_wrapper/byte_stream_wrappers.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_BYTE_STREAM_WRAPPERS_H_ | ||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_BYTE_STREAM_WRAPPERS_H_ | ||
|
||
// Utility classes for interacting with a buffer of bytes as a stream, for use | ||
// in message channel codecs. | ||
|
||
#include <cstdint> | ||
#include <cstring> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
namespace flutter { | ||
|
||
// Wraps an array of bytes with utility methods for treating it as a readable | ||
// stream. | ||
class ByteBufferStreamReader { | ||
public: | ||
// Createa a reader reading from |bytes|, which must have a length of |size|. | ||
// |bytes| must remain valid for the lifetime of this object. | ||
explicit ByteBufferStreamReader(const uint8_t* bytes, size_t size) | ||
: bytes_(bytes), size_(size) {} | ||
|
||
// Reads and returns the next byte from the stream. | ||
uint8_t ReadByte() { | ||
if (location_ >= size_) { | ||
std::cerr << "Invalid read in StandardCodecByteStreamReader" << std::endl; | ||
return 0; | ||
} | ||
return bytes_[location_++]; | ||
} | ||
|
||
// Reads the next |length| bytes from the stream into |buffer|. The caller | ||
// is responsible for ensuring that |buffer| is large enough. | ||
void ReadBytes(uint8_t* buffer, size_t length) { | ||
if (location_ + length > size_) { | ||
std::cerr << "Invalid read in StandardCodecByteStreamReader" << std::endl; | ||
return; | ||
} | ||
std::memcpy(buffer, &bytes_[location_], length); | ||
location_ += length; | ||
} | ||
|
||
// Advances the read cursor to the next multiple of |alignment| relative to | ||
// the start of the wrapped byte buffer, unless it is already aligned. | ||
void ReadAlignment(uint8_t alignment) { | ||
uint8_t mod = location_ % alignment; | ||
if (mod) { | ||
location_ += alignment - mod; | ||
} | ||
} | ||
|
||
private: | ||
// The buffer to read from. | ||
const uint8_t* bytes_; | ||
// The total size of the buffer. | ||
size_t size_; | ||
// The current read location. | ||
size_t location_ = 0; | ||
}; | ||
|
||
// Wraps an array of bytes with utility methods for treating it as a writable | ||
// stream. | ||
class ByteBufferStreamWriter { | ||
public: | ||
// Createa a writter that writes into |buffer|. | ||
// |buffer| must remain valid for the lifetime of this object. | ||
explicit ByteBufferStreamWriter(std::vector<uint8_t>* buffer) | ||
: bytes_(buffer) { | ||
assert(buffer); | ||
} | ||
|
||
// Writes |byte| to the wrapped buffer. | ||
void WriteByte(uint8_t byte) { bytes_->push_back(byte); } | ||
|
||
// Writes the next |length| bytes from |bytes| into the wrapped buffer. | ||
// The caller is responsible for ensuring that |buffer| is large enough. | ||
void WriteBytes(const uint8_t* bytes, size_t length) { | ||
assert(length > 0); | ||
bytes_->insert(bytes_->end(), bytes, bytes + length); | ||
} | ||
|
||
// Writes 0s until the next multiple of |alignment| relative to | ||
// the start of the wrapped byte buffer, unless the write positition is | ||
// already aligned. | ||
void WriteAlignment(uint8_t alignment) { | ||
uint8_t mod = bytes_->size() % alignment; | ||
if (mod) { | ||
for (int i = 0; i < alignment - mod; ++i) { | ||
WriteByte(0); | ||
} | ||
} | ||
} | ||
|
||
private: | ||
// The buffer to write to. | ||
std::vector<uint8_t>* bytes_; | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_BYTE_STREAM_WRAPPERS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
shell/platform/common/cpp/client_wrapper/include/flutter/standard_message_codec.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_MESSAGE_CODEC_H_ | ||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_MESSAGE_CODEC_H_ | ||
|
||
#include "encodable_value.h" | ||
#include "message_codec.h" | ||
|
||
namespace flutter { | ||
|
||
// A binary message encoding/decoding mechanism for communications to/from the | ||
// Flutter engine via message channels. | ||
class StandardMessageCodec : public MessageCodec<EncodableValue> { | ||
public: | ||
// Returns the shared instance of the codec. | ||
static const StandardMessageCodec& GetInstance(); | ||
|
||
~StandardMessageCodec(); | ||
|
||
// Prevent copying. | ||
StandardMessageCodec(StandardMessageCodec const&) = delete; | ||
StandardMessageCodec& operator=(StandardMessageCodec const&) = delete; | ||
|
||
protected: | ||
// Instances should be obtained via GetInstance. | ||
StandardMessageCodec(); | ||
|
||
// |flutter::MessageCodec| | ||
std::unique_ptr<EncodableValue> DecodeMessageInternal( | ||
const uint8_t* binary_message, | ||
const size_t message_size) const override; | ||
|
||
// |flutter::MessageCodec| | ||
std::unique_ptr<std::vector<uint8_t>> EncodeMessageInternal( | ||
const EncodableValue& message) const override; | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_MESSAGE_CODEC_H_ |
52 changes: 52 additions & 0 deletions
52
shell/platform/common/cpp/client_wrapper/include/flutter/standard_method_codec.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_METHOD_CODEC_H_ | ||
#define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_METHOD_CODEC_H_ | ||
|
||
#include "encodable_value.h" | ||
#include "method_call.h" | ||
#include "method_codec.h" | ||
|
||
namespace flutter { | ||
|
||
// An implementation of MethodCodec that uses a binary serialization. | ||
class StandardMethodCodec : public MethodCodec<EncodableValue> { | ||
public: | ||
// Returns the shared instance of the codec. | ||
static const StandardMethodCodec& GetInstance(); | ||
|
||
~StandardMethodCodec() = default; | ||
|
||
// Prevent copying. | ||
StandardMethodCodec(StandardMethodCodec const&) = delete; | ||
StandardMethodCodec& operator=(StandardMethodCodec const&) = delete; | ||
|
||
protected: | ||
// Instances should be obtained via GetInstance. | ||
StandardMethodCodec() = default; | ||
|
||
// |flutter::MethodCodec| | ||
std::unique_ptr<MethodCall<EncodableValue>> DecodeMethodCallInternal( | ||
const uint8_t* message, | ||
const size_t message_size) const override; | ||
|
||
// |flutter::MethodCodec| | ||
std::unique_ptr<std::vector<uint8_t>> EncodeMethodCallInternal( | ||
const MethodCall<EncodableValue>& method_call) const override; | ||
|
||
// |flutter::MethodCodec| | ||
std::unique_ptr<std::vector<uint8_t>> EncodeSuccessEnvelopeInternal( | ||
const EncodableValue* result) const override; | ||
|
||
// |flutter::MethodCodec| | ||
std::unique_ptr<std::vector<uint8_t>> EncodeErrorEnvelopeInternal( | ||
const std::string& error_code, | ||
const std::string& error_message, | ||
const EncodableValue* error_details) const override; | ||
}; | ||
|
||
} // namespace flutter | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_STANDARD_METHOD_CODEC_H_ |
Oops, something went wrong.