serialize-packed.h 5.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Kenton Varda's avatar
Kenton Varda committed
24 25
#ifndef CAPNP_SERIALIZE_PACKED_H_
#define CAPNP_SERIALIZE_PACKED_H_
26 27 28

#include "serialize.h"

29
namespace capnp {
30

31
namespace _ {  // private
32

33
class PackedInputStream: public kj::InputStream {
34 35 36 37
  // An input stream that unpacks packed data with a picky constraint:  The caller must read data
  // in the exact same size and sequence as the data was written to PackedOutputStream.

public:
38
  explicit PackedInputStream(kj::BufferedInputStream& inner);
39
  KJ_DISALLOW_COPY(PackedInputStream);
40
  ~PackedInputStream() noexcept(false);
41 42

  // implements InputStream ------------------------------------------
43
  size_t tryRead(void* buffer, size_t minBytes, size_t maxBytes) override;
44 45 46
  void skip(size_t bytes) override;

private:
47
  kj::BufferedInputStream& inner;
48 49
};

50
class PackedOutputStream: public kj::OutputStream {
51
public:
52
  explicit PackedOutputStream(kj::BufferedOutputStream& inner);
53
  KJ_DISALLOW_COPY(PackedOutputStream);
54
  ~PackedOutputStream() noexcept(false);
55 56 57 58 59

  // implements OutputStream -----------------------------------------
  void write(const void* buffer, size_t bytes) override;

private:
60
  kj::BufferedOutputStream& inner;
61 62
};

63
}  // namespace _ (private)
64

65
class PackedMessageReader: private _::PackedInputStream, public InputStreamMessageReader {
66
public:
67
  PackedMessageReader(kj::BufferedInputStream& inputStream, ReaderOptions options = ReaderOptions(),
68 69
                      kj::ArrayPtr<word> scratchSpace = nullptr);
  KJ_DISALLOW_COPY(PackedMessageReader);
70
  ~PackedMessageReader() noexcept(false);
71 72
};

73
class PackedFdMessageReader: private kj::FdInputStream, private kj::BufferedInputStreamWrapper,
74 75 76
                             public PackedMessageReader {
public:
  PackedFdMessageReader(int fd, ReaderOptions options = ReaderOptions(),
77
                        kj::ArrayPtr<word> scratchSpace = nullptr);
78 79 80 81
  // Read message from a file descriptor, without taking ownership of the descriptor.
  // Note that if you want to reuse the descriptor after the reader is destroyed, you'll need to
  // seek it, since otherwise the position is unspecified.

82
  PackedFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
83
                        kj::ArrayPtr<word> scratchSpace = nullptr);
84 85
  // Read a message from a file descriptor, taking ownership of the descriptor.

86
  KJ_DISALLOW_COPY(PackedFdMessageReader);
87

88
  ~PackedFdMessageReader() noexcept(false);
89 90
};

91 92
void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder);
void writePackedMessage(kj::BufferedOutputStream& output,
93
                        kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
94 95
// Write a packed message to a buffered output stream.

96 97
void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder);
void writePackedMessage(kj::OutputStream& output,
98
                        kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
99 100 101 102 103
// Write a packed message to an unbuffered output stream.  If you intend to write multiple messages
// in succession, consider wrapping your output in a buffered stream in order to reduce system
// call overhead.

void writePackedMessageToFd(int fd, MessageBuilder& builder);
104
void writePackedMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
105 106 107 108 109
// Write a single packed message to the file descriptor.

// =======================================================================================
// inline stuff

110
inline void writePackedMessage(kj::BufferedOutputStream& output, MessageBuilder& builder) {
111 112 113
  writePackedMessage(output, builder.getSegmentsForOutput());
}

114
inline void writePackedMessage(kj::OutputStream& output, MessageBuilder& builder) {
115 116 117 118 119 120 121
  writePackedMessage(output, builder.getSegmentsForOutput());
}

inline void writePackedMessageToFd(int fd, MessageBuilder& builder) {
  writePackedMessageToFd(fd, builder.getSegmentsForOutput());
}

122
}  // namespace capnp
123

Kenton Varda's avatar
Kenton Varda committed
124
#endif  // CAPNP_SERIALIZE_PACKED_H_