-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathD4StreamMarshaller.h
More file actions
201 lines (164 loc) · 6.76 KB
/
D4StreamMarshaller.h
File metadata and controls
201 lines (164 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// D4StreamMarshaller.h
// -*- mode: c++; c-basic-offset:4 -*-
// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.
// Copyright (c) 2002,2003,2012 OPeNDAP, Inc.
// Author: Patrick West <pwest@ucar.edu>,
// James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
#ifndef I_D4StreamMarshaller_h
#define I_D4StreamMarshaller_h 1
// By default, only support platforms that use IEEE754 for floating point values.
// Hacked up code leftover from an older version of the class; largely untested.
// jhrg 10/3/13
#define USE_XDR_FOR_IEEE754_ENCODING 0
#if USE_XDR_FOR_IEEE754_ENCODING
#ifdef WIN32
#include <rpc.h>
#include <winsock2.h>
#include <xdr.h>
#else
#include <netinet/in.h>
#include <rpc/types.h>
#include <rpc/xdr.h>
#endif
#endif
#include "crc.h"
#include "InternalErr.h"
#include "Marshaller.h"
namespace libdap {
class Vector;
class MarshallerThread;
/** @brief Marshaller that knows how to marshal/serialize dap data objects
* to a C++ iostream using DAP4's receiver-makes-right scheme. This code
* adds checksums to the stream and uses the xdr library to encode real
* values if the underlying representation is not IEEE 754. It also supports
* computing the checksum only.
*
* @note This class uses the Marshaller interface; it could be rewritten
* to use far fewer methods since all the put_*() methods take different
* types.
*/
class D4StreamMarshaller : public Marshaller {
#if USE_XDR_FOR_IEEE754_ENCODING
XDR d_scalar_sink;
char d_ieee754_buf[sizeof(dods_float64)]; // used to serialize a float or double
#endif
ostream &d_out;
bool d_write_data = true; // jhrg 1/27/12
bool d_compute_checksum = false; // ndp 08/03/25
Crc32 d_checksum;
MarshallerThread *tm = nullptr;
#if USE_XDR_FOR_IEEE754_ENCODING
void m_serialize_reals(char *val, int64_t num, int width, Type type);
#endif
public:
/**
* @brief Builds a DAP4 stream marshaller.
* @param out Destination stream.
* @param write_data True to emit serialized bytes to `out`.
* @param compute_checksums True to update/emit checksums while writing.
*/
explicit D4StreamMarshaller(std::ostream &out, bool write_data = true, bool compute_checksums = false);
D4StreamMarshaller() = delete;
D4StreamMarshaller(const D4StreamMarshaller &) = delete;
D4StreamMarshaller &operator=(const D4StreamMarshaller &) = delete;
D4StreamMarshaller(const D4StreamMarshaller &&) = delete;
D4StreamMarshaller &operator=(const D4StreamMarshaller &&) = delete;
~D4StreamMarshaller() override;
virtual void reset_checksum();
virtual string get_checksum();
virtual void checksum_update(const void *data, unsigned long len);
virtual void put_checksum();
virtual void put_count(int64_t count);
/** @copydoc Marshaller::put_byte */
void put_byte(dods_byte val) override;
/** @brief Serialize one DAP4 `Int8` value.
* @param val Value to serialize.
*/
virtual void put_int8(dods_int8 val);
/** @copydoc Marshaller::put_int16 */
void put_int16(dods_int16 val) override;
/** @copydoc Marshaller::put_int32 */
void put_int32(dods_int32 val) override;
// Added
/** @brief Serialize one DAP4 `Int64` value.
* @param val Value to serialize.
*/
virtual void put_int64(dods_int64 val);
/** @copydoc Marshaller::put_float32 */
void put_float32(dods_float32 val) override;
/** @copydoc Marshaller::put_float64 */
void put_float64(dods_float64 val) override;
/** @copydoc Marshaller::put_uint16 */
void put_uint16(dods_uint16 val) override;
/** @copydoc Marshaller::put_uint32 */
void put_uint32(dods_uint32 val) override;
// Added
/** @brief Serialize one DAP4 `UInt64` value.
* @param val Value to serialize.
*/
virtual void put_uint64(dods_uint64 val);
/** @copydoc Marshaller::put_str */
void put_str(const string &val) override;
/** @copydoc Marshaller::put_url */
void put_url(const string &val) override;
/// @brief Not supported by DAP4
void put_opaque(char *, unsigned int) override {
throw InternalErr(__FILE__, __LINE__, "Not implemented for DAP4; use put_opaque_dap4() instead.");
}
/**
* @brief Serialize DAP4 opaque bytes.
* @param val Buffer containing bytes.
* @param num_bytes Number of bytes to serialize.
*/
virtual void put_opaque_dap4(const char *val, int64_t num_bytes);
/// @brief Not supported by DAP4
void put_int(int) override { throw InternalErr(__FILE__, __LINE__, "Not Implemented; use put_length_prefix."); }
/**
* @brief Serialize a contiguous byte block.
* @param val Buffer containing serialized element bytes.
* @param num_bytes Number of bytes to write.
*/
virtual void put_vector(char *val, int64_t num_bytes);
/**
* @brief Serialize a vector with explicit element count and element width.
* @param val Buffer containing vector bytes.
* @param num_elem Number of elements.
* @param elem_size Number of bytes per element.
*/
virtual void put_vector(char *val, int64_t num_elem, int elem_size);
virtual void put_vector_float32(char *val, int64_t num_elem);
virtual void put_vector_float64(char *val, int64_t num_elem);
/// @brief Not supported by DAP4
void put_vector(char *, int, Vector &) override {
throw InternalErr(__FILE__, __LINE__, "Not Implemented; use other put_vector() versions.");
}
/// @brief Not supported by DAP4
void put_vector(char *, int, int, Vector &) override {
throw InternalErr(__FILE__, __LINE__, "Not Implemented; use other put_vector() versions.");
}
/// @brief Does nothing in DAP4
void put_vector_start(int /*num*/) override {}
void put_vector_part(char * /*val*/, unsigned int /*num*/, int /*width*/, Type /*type*/) override;
/// @brief Does nothing in DAP4
void put_vector_end() override {}
void dump(std::ostream &strm) const override;
};
} // namespace libdap
#endif // I_D4StreamMarshaller_h