Chris@64: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors Chris@64: // Licensed under the MIT License: Chris@64: // Chris@64: // Permission is hereby granted, free of charge, to any person obtaining a copy Chris@64: // of this software and associated documentation files (the "Software"), to deal Chris@64: // in the Software without restriction, including without limitation the rights Chris@64: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell Chris@64: // copies of the Software, and to permit persons to whom the Software is Chris@64: // furnished to do so, subject to the following conditions: Chris@64: // Chris@64: // The above copyright notice and this permission notice shall be included in Chris@64: // all copies or substantial portions of the Software. Chris@64: // Chris@64: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR Chris@64: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, Chris@64: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE Chris@64: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER Chris@64: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, Chris@64: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN Chris@64: // THE SOFTWARE. Chris@64: Chris@64: #ifndef CAPNP_SERIALIZE_PACKED_H_ Chris@64: #define CAPNP_SERIALIZE_PACKED_H_ Chris@64: Chris@64: #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS) Chris@64: #pragma GCC system_header Chris@64: #endif Chris@64: Chris@64: #include "serialize.h" Chris@64: Chris@64: namespace capnp { Chris@64: Chris@64: namespace _ { // private Chris@64: Chris@64: class PackedInputStream: public kj::InputStream { Chris@64: // An input stream that unpacks packed data with a picky constraint: The caller must read data Chris@64: // in the exact same size and sequence as the data was written to PackedOutputStream. Chris@64: Chris@64: public: Chris@64: explicit PackedInputStream(kj::BufferedInputStream& inner); Chris@64: KJ_DISALLOW_COPY(PackedInputStream); Chris@64: ~PackedInputStream() noexcept(false); Chris@64: Chris@64: // implements InputStream ------------------------------------------ Chris@64: size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override; Chris@64: void skip(size_t bytes) override; Chris@64: Chris@64: private: Chris@64: kj::BufferedInputStream& inner; Chris@64: }; Chris@64: Chris@64: class PackedOutputStream: public kj::OutputStream { Chris@64: public: Chris@64: explicit PackedOutputStream(kj::BufferedOutputStream& inner); Chris@64: KJ_DISALLOW_COPY(PackedOutputStream); Chris@64: ~PackedOutputStream() noexcept(false); Chris@64: Chris@64: // implements OutputStream ----------------------------------------- Chris@64: void write(const void* buffer, size_t bytes) override; Chris@64: Chris@64: private: Chris@64: kj::BufferedOutputStream& inner; Chris@64: }; Chris@64: Chris@64: } // namespace _ (private) Chris@64: Chris@64: class PackedMessageReader: private _::PackedInputStream, public InputStreamMessageReader { Chris@64: public: Chris@64: PackedMessageReader(kj::BufferedInputStream& inputStream, ReaderOptions options = ReaderOptions(), Chris@64: kj::ArrayPtr scratchSpace = nullptr); Chris@64: KJ_DISALLOW_COPY(PackedMessageReader); Chris@64: ~PackedMessageReader() noexcept(false); Chris@64: }; Chris@64: Chris@64: class PackedFdMessageReader: private kj::FdInputStream, private kj::BufferedInputStreamWrapper, Chris@64: public PackedMessageReader { Chris@64: public: Chris@64: PackedFdMessageReader(int fd, ReaderOptions options = ReaderOptions(), Chris@64: kj::ArrayPtr scratchSpace = nullptr); Chris@64: // Read message from a file descriptor, without taking ownership of the descriptor. Chris@64: // Note that if you want to reuse the descriptor after the reader is destroyed, you'll need to Chris@64: // seek it, since otherwise the position is unspecified. Chris@64: Chris@64: PackedFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(), Chris@64: kj::ArrayPtr scratchSpace = nullptr); Chris@64: // Read a message from a file descriptor, taking ownership of the descriptor. Chris@64: Chris@64: KJ_DISALLOW_COPY(PackedFdMessageReader); Chris@64: Chris@64: ~PackedFdMessageReader() noexcept(false); Chris@64: }; Chris@64: Chris@64: void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder); Chris@64: void writePackedMessage(kj::BufferedOutputStream& output, Chris@64: kj::ArrayPtr> segments); Chris@64: // Write a packed message to a buffered output stream. Chris@64: Chris@64: void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder); Chris@64: void writePackedMessage(kj::OutputStream& output, Chris@64: kj::ArrayPtr> segments); Chris@64: // Write a packed message to an unbuffered output stream. If you intend to write multiple messages Chris@64: // in succession, consider wrapping your output in a buffered stream in order to reduce system Chris@64: // call overhead. Chris@64: Chris@64: void writePackedMessageToFd(int fd, MessageBuilder& builder); Chris@64: void writePackedMessageToFd(int fd, kj::ArrayPtr> segments); Chris@64: // Write a single packed message to the file descriptor. Chris@64: Chris@64: size_t computeUnpackedSizeInWords(kj::ArrayPtr packedBytes); Chris@64: // Computes the number of words to which the given packed bytes will unpack. Not intended for use Chris@64: // in performance-sensitive situations. Chris@64: Chris@64: // ======================================================================================= Chris@64: // inline stuff Chris@64: Chris@64: inline void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder) { Chris@64: writePackedMessage(output, builder.getSegmentsForOutput()); Chris@64: } Chris@64: Chris@64: inline void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder) { Chris@64: writePackedMessage(output, builder.getSegmentsForOutput()); Chris@64: } Chris@64: Chris@64: inline void writePackedMessageToFd(int fd, MessageBuilder& builder) { Chris@64: writePackedMessageToFd(fd, builder.getSegmentsForOutput()); Chris@64: } Chris@64: Chris@64: } // namespace capnp Chris@64: Chris@64: #endif // CAPNP_SERIALIZE_PACKED_H_