30 #include <arpa/inet.h> 41 #include "chunked_stream.h" 42 #include "chunked_ostream.h" 53 std::streambuf::int_type
56 DBG(cerr <<
"In chunked_outbuf::data_chunk" << endl);
58 int32_t num = pptr() - pbase();
76 uint32_t header = num;
79 if (!d_big_endian) header |= CHUNK_LITTLE_ENDIAN;
82 header = htonl(header);
84 d_os.write((
const char *)&header,
sizeof(int32_t));
88 d_os.write(d_buffer, num);
89 if (d_os.eof() || d_os.bad())
90 return traits_type::eof();
107 std::streambuf::int_type
110 DBG(cerr <<
"In chunked_outbuf::end_chunk" << endl);
112 int32_t num = pptr() - pbase();
118 uint32_t header = (uint32_t)num | CHUNK_END;
122 if (!d_big_endian) header |= CHUNK_LITTLE_ENDIAN;
125 header = htonl(header);
130 d_os.write((
const char *)&header,
sizeof(uint32_t));
134 d_os.write(d_buffer, num);
135 if (d_os.eof() || d_os.bad())
136 return traits_type::eof();
149 std::streambuf::int_type
152 DBG(cerr <<
"In chunked_outbuf::err_chunk" << endl);
157 int32_t num = pptr() - pbase();
162 if (msg.length() > 0x00FFFFFF)
163 msg =
"Error message too long";
165 uint32_t header = (uint32_t)msg.length() | CHUNK_ERR;
168 if (!d_big_endian) header |= CHUNK_LITTLE_ENDIAN;
171 header = htonl(header);
177 d_os.write((
const char *)&header,
sizeof(uint32_t));
181 d_os.write(msg.data(), msg.length());
182 if (d_os.eof() || d_os.bad())
183 return traits_type::eof();
204 std::streambuf::int_type
207 DBG(cerr <<
"In chunked_outbuf::overflow" << endl);
212 if (!traits_type::eq_int_type(c, traits_type::eof())) {
213 *pptr() = traits_type::not_eof(c);
219 return traits_type::eof();
222 return traits_type::not_eof(c);
249 DBG(cerr <<
"In chunked_outbuf::xsputn: num: " << num << endl);
259 int32_t bytes_in_buffer = pptr() - pbase();
264 if (bytes_in_buffer + num < d_buf_size) {
265 DBG2(cerr <<
":xsputn: buffering num: " << num << endl);
266 memcpy(pptr(), s, num);
268 return traits_type::not_eof(num);
273 uint32_t header = d_buf_size;
276 if (!d_big_endian) header |= CHUNK_LITTLE_ENDIAN;
279 header = htonl(header);
281 d_os.write((
const char *)&header,
sizeof(int32_t));
286 setp(d_buffer, d_buffer + (d_buf_size - 1));
288 d_os.write(d_buffer, bytes_in_buffer);
289 if (d_os.eof() || d_os.bad())
290 return traits_type::not_eof(0);
292 int bytes_to_fill_out_buffer = d_buf_size - bytes_in_buffer;
293 d_os.write(s, bytes_to_fill_out_buffer);
294 if (d_os.eof() || d_os.bad())
295 return traits_type::not_eof(0);
296 s += bytes_to_fill_out_buffer;
297 uint32_t bytes_still_to_send = num - bytes_to_fill_out_buffer;
301 while (bytes_still_to_send >= d_buf_size) {
303 d_os.write((
const char *) &header,
sizeof(int32_t));
304 d_os.write(s, d_buf_size);
305 if (d_os.eof() || d_os.bad())
return traits_type::not_eof(0);
307 bytes_still_to_send -= d_buf_size;
310 if (bytes_still_to_send > 0) {
314 memcpy(d_buffer, s, bytes_still_to_send);
315 pbump(bytes_still_to_send);
320 return traits_type::not_eof(num);
328 std::streambuf::int_type
331 DBG(cerr <<
"In chunked_outbuf::sync" << endl);
335 return traits_type::not_eof(-1);
337 return traits_type::not_eof(0);
virtual int_type overflow(int c)
Virtual method called when the internal buffer would overflow. When the internal buffer fills...
top level DAP object to house generic methods
virtual std::streamsize xsputn(const char *s, std::streamsize num)
Write bytes to the chunked stream Write the bytes in s to the chunked stream.
int_type end_chunk()
Send an end chunk.
int_type err_chunk(const std::string &msg)
Send an error chunk While building up the next chunk, send an error chunk, ignoring the data currentl...
virtual int_type sync()
Synchronize the stream with its data sink.
int_type data_chunk()
Write out the contents of the buffer as a chunk.