third_party.pigweed.src/pw_rpc/channel.cc
Wyatt Hepler 671946e175 pw_rpc: Split public and internal interfaces
- Make an internal version of Server, which will expose functions for
  working with Reader/Writer objects
- Make an internal version of Channel, which provides an interface for
  sending packets.
- Define the Channel::OutputBuffer class for handling buffers acquired
  from a ChannelOutput.

Change-Id: Ieea29c1392726cd2eb15008e19004cde05c7f43b
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/12160
Commit-Queue: Wyatt Hepler <hepler@google.com>
Reviewed-by: Alexei Frolov <frolv@google.com>
2020-06-18 23:13:33 +00:00

45 lines
1.4 KiB
C++

// Copyright 2020 The Pigweed Authors
//
// 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
//
// https://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.
#include "pw_rpc/internal/channel.h"
#include "pw_log/log.h"
#include "pw_rpc/internal/packet.h"
namespace pw::rpc::internal {
using std::byte;
span<byte> Channel::OutputBuffer::payload(const Packet& packet) const {
const size_t reserved_size = packet.MinEncodedSizeBytes();
return reserved_size <= buffer_.size() ? buffer_.subspan(reserved_size)
: span<byte>();
}
Status Channel::Send(OutputBuffer& buffer, const internal::Packet& packet) {
StatusWithSize encoded = packet.Encode(buffer.buffer_);
buffer.buffer_ = {};
if (!encoded.ok()) {
PW_LOG_ERROR("Failed to encode response packet to channel buffer");
output().SendAndReleaseBuffer(0);
return Status::INTERNAL;
}
output().SendAndReleaseBuffer(encoded.size());
return Status::OK;
}
} // namespace pw::rpc::internal