/* * Copyright (C) 2017 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. */ #ifndef SRC_IPC_BUFFERED_FRAME_DESERIALIZER_H_ #define SRC_IPC_BUFFERED_FRAME_DESERIALIZER_H_ #include <stddef.h> #include <list> #include <memory> #include "perfetto/ext/base/paged_memory.h" #include "perfetto/ext/base/utils.h" #include "perfetto/ext/ipc/basic_types.h" namespace perfetto { namespace protos { namespace gen { class IPCFrame; } // namespace gen } // namespace protos namespace ipc { Frame; // Deserializes incoming frames, taking care of buffering and tokenization. // Used by both host and client to decode incoming frames. // // Which problem does it solve? // ---------------------------- // The wire protocol is as follows: // [32-bit frame size][proto-encoded Frame], e.g: // [06 00 00 00][00 11 22 33 44 55 66] // [02 00 00 00][AA BB] // [04 00 00 00][CC DD EE FF] // However, given that the socket works in SOCK_STREAM mode, the recv() calls // might see the following: // 06 00 00 // 00 00 11 22 33 44 55 // 66 02 00 00 00 ... // This class takes care of buffering efficiently the data received, without // making any assumption on how the incoming data will be chunked by the socket. // For instance, it is possible that a recv() doesn't produce any frame (because // it received only a part of the frame) or produces more than one frame. // // Usage // ----- // Both host and client use this as follows: // // auto buf = rpc_frame_decoder.BeginReceive(); // size_t rsize = socket.recv(buf.first, buf.second); // rpc_frame_decoder.EndReceive(rsize); // while (Frame frame = rpc_frame_decoder.PopNextFrame()) { // ... process |frame| // } // // Design goals: // ------------- // - Optimize for the realistic case of each recv() receiving one or more // whole frames. In this case no memmove is performed. // - Guarantee that frames lay in a virtually contiguous memory area. // This allows to use the protobuf-lite deserialization API (scattered // deserialization is supported only by libprotobuf-full). // - Put a hard boundary to the size of the incoming buffer. This is to prevent // that a malicious sends an abnormally large frame and OOMs us. // - Simplicity: just use a linear mmap region. No reallocations or scattering. // Takes care of madvise()-ing unused memory. class BufferedFrameDeserializer { … }; } // namespace ipc } // namespace perfetto #endif // SRC_IPC_BUFFERED_FRAME_DESERIALIZER_H_