comparison win64-msvc/include/capnp/serialize.h @ 47:d93140aac40b

Current Capnp libs and headers from git
author Chris Cannam
date Thu, 20 Oct 2016 18:15:38 +0100
parents
children 0f2d93caa50c
comparison
equal deleted inserted replaced
46:efe5b9f38b13 47:d93140aac40b
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
2 // Licensed under the MIT License:
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21
22 // This file implements a simple serialization format for Cap'n Proto messages. The format
23 // is as follows:
24 //
25 // * 32-bit little-endian segment count (4 bytes).
26 // * 32-bit little-endian size of each segment (4*(segment count) bytes).
27 // * Padding so that subsequent data is 64-bit-aligned (0 or 4 bytes). (I.e., if there are an even
28 // number of segments, there are 4 bytes of zeros here, otherwise there is no padding.)
29 // * Data from each segment, in order (8*sum(segment sizes) bytes)
30 //
31 // This format has some important properties:
32 // - It is self-delimiting, so multiple messages may be written to a stream without any external
33 // delimiter.
34 // - The total size and position of each segment can be determined by reading only the first part
35 // of the message, allowing lazy and random-access reading of the segment data.
36 // - A message is always at least 8 bytes.
37 // - A single-segment message can be read entirely in two system calls with no buffering.
38 // - A multi-segment message can be read entirely in three system calls with no buffering.
39 // - The format is appropriate for mmap()ing since all data is aligned.
40
41 #ifndef CAPNP_SERIALIZE_H_
42 #define CAPNP_SERIALIZE_H_
43
44 #if defined(__GNUC__) && !defined(CAPNP_HEADER_WARNINGS)
45 #pragma GCC system_header
46 #endif
47
48 #include "message.h"
49 #include <kj/io.h>
50
51 namespace capnp {
52
53 class FlatArrayMessageReader: public MessageReader {
54 // Parses a message from a flat array. Note that it makes sense to use this together with mmap()
55 // for extremely fast parsing.
56
57 public:
58 FlatArrayMessageReader(kj::ArrayPtr<const word> array, ReaderOptions options = ReaderOptions());
59 // The array must remain valid until the MessageReader is destroyed.
60
61 kj::ArrayPtr<const word> getSegment(uint id) override;
62
63 const word* getEnd() const { return end; }
64 // Get a pointer just past the end of the message as determined by reading the message header.
65 // This could actually be before the end of the input array. This pointer is useful e.g. if
66 // you know that the input array has extra stuff appended after the message and you want to
67 // get at it.
68
69 private:
70 // Optimize for single-segment case.
71 kj::ArrayPtr<const word> segment0;
72 kj::Array<kj::ArrayPtr<const word>> moreSegments;
73 const word* end;
74 };
75
76 kj::ArrayPtr<const word> initMessageBuilderFromFlatArrayCopy(
77 kj::ArrayPtr<const word> array, MessageBuilder& target,
78 ReaderOptions options = ReaderOptions());
79 // Convenience function which reads a message using `FlatArrayMessageReader` then copies the
80 // content into the target `MessageBuilder`, verifying that the message structure is valid
81 // (although not necessarily that it matches the desired schema).
82 //
83 // Returns an ArrayPtr containing any words left over in the array after consuming the whole
84 // message. This is useful when reading multiple messages that have been concatenated. See also
85 // FlatArrayMessageReader::getEnd().
86 //
87 // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one
88 // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not
89 // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.)
90
91 kj::Array<word> messageToFlatArray(MessageBuilder& builder);
92 // Constructs a flat array containing the entire content of the given message.
93 //
94 // To output the message as bytes, use `.asBytes()` on the returned word array. Keep in mind that
95 // `asBytes()` returns an ArrayPtr, so you have to save the Array as well to prevent it from being
96 // deleted. For example:
97 //
98 // kj::Array<capnp::word> words = messageToFlatArray(myMessage);
99 // kj::ArrayPtr<kj::byte> bytes = words.asBytes();
100 // write(fd, bytes.begin(), bytes.size());
101
102 kj::Array<word> messageToFlatArray(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
103 // Version of messageToFlatArray that takes a raw segment array.
104
105 size_t computeSerializedSizeInWords(MessageBuilder& builder);
106 // Returns the size, in words, that will be needed to serialize the message, including the header.
107
108 size_t computeSerializedSizeInWords(kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
109 // Version of computeSerializedSizeInWords that takes a raw segment array.
110
111 size_t expectedSizeInWordsFromPrefix(kj::ArrayPtr<const word> messagePrefix);
112 // Given a prefix of a serialized message, try to determine the expected total size of the message,
113 // in words. The returned size is based on the information known so far; it may be an underestimate
114 // if the prefix doesn't contain the full segment table.
115 //
116 // If the returned value is greater than `messagePrefix.size()`, then the message is not yet
117 // complete and the app cannot parse it yet. If the returned value is less than or equal to
118 // `messagePrefix.size()`, then the returned value is the exact total size of the message; any
119 // remaining bytes are part of the next message.
120 //
121 // This function is useful when reading messages from a stream in an asynchronous way, but when
122 // using the full KJ async infrastructure would be too difficult. Each time bytes are received,
123 // use this function to determine if an entire message is ready to be parsed.
124
125 // =======================================================================================
126
127 class InputStreamMessageReader: public MessageReader {
128 // A MessageReader that reads from an abstract kj::InputStream. See also StreamFdMessageReader
129 // for a subclass specific to file descriptors.
130
131 public:
132 InputStreamMessageReader(kj::InputStream& inputStream,
133 ReaderOptions options = ReaderOptions(),
134 kj::ArrayPtr<word> scratchSpace = nullptr);
135 ~InputStreamMessageReader() noexcept(false);
136
137 // implements MessageReader ----------------------------------------
138 kj::ArrayPtr<const word> getSegment(uint id) override;
139
140 private:
141 kj::InputStream& inputStream;
142 byte* readPos;
143
144 // Optimize for single-segment case.
145 kj::ArrayPtr<const word> segment0;
146 kj::Array<kj::ArrayPtr<const word>> moreSegments;
147
148 kj::Array<word> ownedSpace;
149 // Only if scratchSpace wasn't big enough.
150
151 kj::UnwindDetector unwindDetector;
152 };
153
154 void readMessageCopy(kj::InputStream& input, MessageBuilder& target,
155 ReaderOptions options = ReaderOptions(),
156 kj::ArrayPtr<word> scratchSpace = nullptr);
157 // Convenience function which reads a message using `InputStreamMessageReader` then copies the
158 // content into the target `MessageBuilder`, verifying that the message structure is valid
159 // (although not necessarily that it matches the desired schema).
160 //
161 // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one
162 // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not
163 // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.)
164
165 void writeMessage(kj::OutputStream& output, MessageBuilder& builder);
166 // Write the message to the given output stream.
167
168 void writeMessage(kj::OutputStream& output, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
169 // Write the segment array to the given output stream.
170
171 // =======================================================================================
172 // Specializations for reading from / writing to file descriptors.
173
174 class StreamFdMessageReader: private kj::FdInputStream, public InputStreamMessageReader {
175 // A MessageReader that reads from a steam-based file descriptor.
176
177 public:
178 StreamFdMessageReader(int fd, ReaderOptions options = ReaderOptions(),
179 kj::ArrayPtr<word> scratchSpace = nullptr)
180 : FdInputStream(fd), InputStreamMessageReader(*this, options, scratchSpace) {}
181 // Read message from a file descriptor, without taking ownership of the descriptor.
182
183 StreamFdMessageReader(kj::AutoCloseFd fd, ReaderOptions options = ReaderOptions(),
184 kj::ArrayPtr<word> scratchSpace = nullptr)
185 : FdInputStream(kj::mv(fd)), InputStreamMessageReader(*this, options, scratchSpace) {}
186 // Read a message from a file descriptor, taking ownership of the descriptor.
187
188 ~StreamFdMessageReader() noexcept(false);
189 };
190
191 void readMessageCopyFromFd(int fd, MessageBuilder& target,
192 ReaderOptions options = ReaderOptions(),
193 kj::ArrayPtr<word> scratchSpace = nullptr);
194 // Convenience function which reads a message using `StreamFdMessageReader` then copies the
195 // content into the target `MessageBuilder`, verifying that the message structure is valid
196 // (although not necessarily that it matches the desired schema).
197 //
198 // (Note that it's also possible to initialize a `MessageBuilder` directly without a copy using one
199 // of `MessageBuilder`'s constructors. However, this approach skips the validation step and is not
200 // safe to use on untrusted input. Therefore, we do not provide a convenience method for it.)
201
202 void writeMessageToFd(int fd, MessageBuilder& builder);
203 // Write the message to the given file descriptor.
204 //
205 // This function throws an exception on any I/O error. If your code is not exception-safe, be sure
206 // you catch this exception at the call site. If throwing an exception is not acceptable, you
207 // can implement your own OutputStream with arbitrary error handling and then use writeMessage().
208
209 void writeMessageToFd(int fd, kj::ArrayPtr<const kj::ArrayPtr<const word>> segments);
210 // Write the segment array to the given file descriptor.
211 //
212 // This function throws an exception on any I/O error. If your code is not exception-safe, be sure
213 // you catch this exception at the call site. If throwing an exception is not acceptable, you
214 // can implement your own OutputStream with arbitrary error handling and then use writeMessage().
215
216 // =======================================================================================
217 // inline stuff
218
219 inline kj::Array<word> messageToFlatArray(MessageBuilder& builder) {
220 return messageToFlatArray(builder.getSegmentsForOutput());
221 }
222
223 inline size_t computeSerializedSizeInWords(MessageBuilder& builder) {
224 return computeSerializedSizeInWords(builder.getSegmentsForOutput());
225 }
226
227 inline void writeMessage(kj::OutputStream& output, MessageBuilder& builder) {
228 writeMessage(output, builder.getSegmentsForOutput());
229 }
230
231 inline void writeMessageToFd(int fd, MessageBuilder& builder) {
232 writeMessageToFd(fd, builder.getSegmentsForOutput());
233 }
234
235 } // namespace capnp
236
237 #endif // SERIALIZE_H_