summaryrefslogtreecommitdiff
path: root/emulator
diff options
context:
space:
mode:
authorSergey Prigogin <sprigogin@google.com>2022-02-28 13:55:36 -0800
committerSergey Prigogin <sprigogin@google.com>2022-02-28 14:25:32 -0800
commit95a5e8b50470150f29d4a86b7cca0c2724aca80f (patch)
treef552e5f8ea7b48fa0ef74356c8cab1ffddaca389 /emulator
parent4c71add77e05e8800358965c5113c2d01662a913 (diff)
downloadidea-95a5e8b50470150f29d4a86b7cca0c2724aca80f.tar.gz
Add the Base128OutputStream class
Test: later Bug: N/A Change-Id: I4271f6e05cfccc603d25f558c468bd02d632c647
Diffstat (limited to 'emulator')
-rw-r--r--emulator/screen-sharing-agent/app/src/main/cpp/CMakeLists.txt1
-rw-r--r--emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.cc115
-rw-r--r--emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.h59
3 files changed, 175 insertions, 0 deletions
diff --git a/emulator/screen-sharing-agent/app/src/main/cpp/CMakeLists.txt b/emulator/screen-sharing-agent/app/src/main/cpp/CMakeLists.txt
index ac076ec4bc2..7802a31464d 100644
--- a/emulator/screen-sharing-agent/app/src/main/cpp/CMakeLists.txt
+++ b/emulator/screen-sharing-agent/app/src/main/cpp/CMakeLists.txt
@@ -26,6 +26,7 @@ add_library(
accessors/window_manager.cc
agent.cc
base128_input_stream.cc
+ base128_output_stream.cc
control_messages.cc
controller.cc
display_streamer.cc
diff --git a/emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.cc b/emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.cc
new file mode 100644
index 00000000000..6b08c898e2b
--- /dev/null
+++ b/emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.cc
@@ -0,0 +1,115 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * 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
+ *
+ * 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.
+ */
+
+#include "base128_output_stream.h"
+
+#include <unistd.h>
+
+#include <memory>
+
+#include "io_exception.h"
+
+namespace screensharing {
+
+using namespace std;
+
+Base128OutputStream::Base128OutputStream(int fd, size_t buffer_size)
+ : fd_(fd),
+ buffer_(new uint8_t[buffer_size]),
+ buffer_capacity_(buffer_size),
+ offset_(0),
+ data_end_(0) {
+}
+
+Base128OutputStream::~Base128OutputStream() {
+ close(fd_);
+ delete [] buffer_;
+}
+
+int Base128OutputStream::Close() {
+ Flush();
+ return close(fd_);
+}
+
+void Base128OutputStream::Flush() {
+ while (offset_ > 0) {
+ auto n = write(fd_, buffer_, offset_);
+ if (n < 0) {
+ if (errno == EBADFD || errno == EPIPE) {
+ throw EndOfFile();
+ }
+ throw IoException();
+ }
+ assert(n <= offset_); // By contract of the write function.
+ offset_ -= n;
+ if (offset_ > 0) {
+ memcpy(buffer_, buffer_ + n, offset_);
+ }
+ }
+}
+
+void Base128OutputStream::WriteByte(uint8_t byte) {
+ if (offset_ == buffer_capacity_) {
+ Flush();
+ }
+ buffer_[offset_++] = byte;
+}
+
+void Base128OutputStream::WriteBytes(const string& bytes) {
+ WriteInt32(bytes.size());
+ for (auto b : bytes) {
+ WriteByte(b);
+ }
+}
+
+void Base128OutputStream::WriteUInt16(uint16_t value) {
+ do {
+ uint8_t b = value & 0x7F;
+ value >>= 7;
+ if (value != 0) {
+ b |= 0x80;
+ }
+ WriteByte(b);
+ } while (value != 0);
+}
+
+void Base128OutputStream::WriteUInt32(uint32_t value) {
+ do {
+ uint8_t b = value & 0x7F;
+ value >>= 7;
+ if (value != 0) {
+ b |= 0x80;
+ }
+ WriteByte(b);
+ } while (value != 0);
+}
+
+void Base128OutputStream::WriteUInt64(uint64_t value) {
+ do {
+ uint8_t b = value & 0x7F;
+ value >>= 7;
+ if (value != 0) {
+ b |= 0x80;
+ }
+ WriteByte(b);
+ } while (value != 0);
+}
+
+void Base128OutputStream::WriteBool(bool value) {
+ WriteByte(value ? 1 : 0);
+}
+
+} // namespace screensharing
diff --git a/emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.h b/emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.h
new file mode 100644
index 00000000000..4babbf04cb1
--- /dev/null
+++ b/emulator/screen-sharing-agent/app/src/main/cpp/base128_output_stream.h
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2022 The Android Open Source Project
+ *
+ * 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
+ *
+ * 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.
+ */
+
+#pragma once
+
+#include <cerrno>
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+#include <string>
+
+#include "common.h"
+
+namespace screensharing {
+
+// An output stream that uses the unsigned little endian base 128 (<a xref="https://en.wikipedia.org/wiki/LEB128">LEB128</a>)
+// variable-length encoding for integer values.
+// See Base128InputStream.
+class Base128OutputStream {
+public:
+ Base128OutputStream(int fd, size_t buffer_size);
+ ~Base128OutputStream();
+
+ int Close();
+ void Flush();
+ void WriteByte(uint8_t byte);
+ void WriteBytes(const std::string& bytes);
+ void WriteUInt16(uint16_t value);
+ void WriteInt16(int16_t value) { WriteUInt16(value); }
+ void WriteUInt32(uint32_t value);
+ void WriteInt32(int32_t value) { WriteUInt32(value); }
+ void WriteUInt64(uint64_t value);
+ void WriteInt64(int64_t value) { WriteUInt64(value); }
+ void WriteBool(bool value);
+
+private:
+ int fd_;
+ uint8_t* buffer_;
+ size_t buffer_capacity_;
+ size_t offset_;
+ size_t data_end_;
+
+ DISALLOW_COPY_AND_ASSIGN(Base128OutputStream);
+};
+
+} // namespace screensharing