From e639d8ddf0e3c2c9426caa508fd984666c17605a Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Tue, 28 Nov 2017 11:42:44 +0900 Subject: [PATCH] Implement internal class for ipc-port-parcel APIs Change-Id: Iffff2ac8d991106d3a81cc8902a24aa07ba9d6ab Signed-off-by: Junghoon Park --- src/parcel-internal.cc | 133 +++++++++++++++++++++++++ src/parcel-internal.h | 81 +++++++++++++++ src/{rpc_port_parcel.cc => rpc-port-parcel.cc} | 0 src/{rpc_port.cc => rpc-port.cc} | 0 4 files changed, 214 insertions(+) create mode 100644 src/parcel-internal.cc create mode 100644 src/parcel-internal.h rename src/{rpc_port_parcel.cc => rpc-port-parcel.cc} (100%) rename src/{rpc_port.cc => rpc-port.cc} (100%) diff --git a/src/parcel-internal.cc b/src/parcel-internal.cc new file mode 100644 index 0000000..9868c57 --- /dev/null +++ b/src/parcel-internal.cc @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 + +#include "parcel-internal.h" + +namespace rpc_port { + +Parcel::Parcel(const unsigned char* buf, unsigned int size) + : data_(buf, buf + size) { + reader_ = data_.begin(); +} + +void Parcel::WriteByte(char b) { + Write(b); +} + +void Parcel::WriteInt16(short i) { + Write(i); +} + +void Parcel::WriteInt32(int i) { + Write(i); +} + +void Parcel::WriteInt64(long long i) { + Write(i); +} + +void Parcel::WriteFloat(float f) { + Write(f); +} + +void Parcel::WriteDouble(double d) { + Write(d); +} + +void Parcel::WriteString(const char* str) { + int l = std::strlen(str) + 1; + + Write(l); + std::copy(str, str + l, std::back_inserter(data_)); +} + +void Parcel::WriteBool(bool b) { + Write(b); +} + +void Parcel::WriteBundle(bundle* b) { + bundle_raw *r; + int len; + bundle_encode(b, &r, &len); + unsigned char* c = reinterpret_cast(r); + + Write(len + 1); + std::copy(c, c + len + 1, std::back_inserter(data_)); +} + +void Parcel::WriteArrayCount(int count) { + Write(count); +} + +char Parcel::ReadByte() { + return Read(); +} + +short Parcel::ReadInt16() { + return Read(); +} + +int Parcel::ReadInt32() { + return Read(); +} + +long long Parcel::ReadInt64() { + return Read(); +} + +float Parcel::ReadFloat() { + return Read(); +} + +double Parcel::ReadDouble() { + return Read(); +} + +std::string Parcel::ReadString() { + int l = Read(); + + std::string str(reader_, reader_ + l); + reader_ += l; + + return str; +} + +bool Parcel::ReadBool() { + return Read(); +} + +bundle* Parcel::ReadBundle() { + int l = Read(); + + std::string str(reader_, reader_ + l); + reader_ += l; + + bundle* b = bundle_decode( + reinterpret_cast(str.c_str()), l - 1); + + return b; +} + +int Parcel::ReadArrayCount() { + return Read(); +} + +const std::vector& Parcel::GetRaw() { + return data_; +} + +} // namespace rpc_port \ No newline at end of file diff --git a/src/parcel-internal.h b/src/parcel-internal.h new file mode 100644 index 0000000..f63960e --- /dev/null +++ b/src/parcel-internal.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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. + */ + +#ifndef __PARCEL_INTERNAL_H__ +#define __PARCEL_INTERNAL_H__ + +#include + +#include +#include +#include +#include + +namespace rpc_port { + +class Parcel { + public: + Parcel() = default; + Parcel(const unsigned char* buf, unsigned int size); + + void WriteByte(char b); + void WriteInt16(short i); + void WriteInt32(int i); + void WriteInt64(long long i); + void WriteFloat(float f); + void WriteDouble(double d); + void WriteString(const char* str); + void WriteBool(bool b); + void WriteBundle(bundle* b); + void WriteArrayCount(int count); + char ReadByte(); + short ReadInt16(); + int ReadInt32(); + long long ReadInt64(); + float ReadFloat(); + double ReadDouble(); + std::string ReadString(); + bool ReadBool(); + bundle* ReadBundle(); + int ReadArrayCount(); + const std::vector& GetRaw(); + + private: + template + void Write(T d) { + unsigned char* p = reinterpret_cast(&d); + + std::copy(p, p + sizeof(T), std::back_inserter(data_)); + } + + template + T Read() { + T d; + unsigned char* p = reinterpret_cast(&d); + + std::copy(reader_, reader_ + sizeof(T), p); + reader_ += sizeof(T); + return d; + } + + private: + std::vector data_; + std::vector::iterator reader_; +}; + +} + +#endif // __PARCEL_INTERNAL_H__ \ No newline at end of file diff --git a/src/rpc_port_parcel.cc b/src/rpc-port-parcel.cc similarity index 100% rename from src/rpc_port_parcel.cc rename to src/rpc-port-parcel.cc diff --git a/src/rpc_port.cc b/src/rpc-port.cc similarity index 100% rename from src/rpc_port.cc rename to src/rpc-port.cc -- 2.7.4