Go to the documentation of this file.00001
00022 #ifndef HYPERTABLE_COMMBUF_H
00023 #define HYPERTABLE_COMMBUF_H
00024
00025 #include <string>
00026
00027 #include <boost/shared_array.hpp>
00028
00029 #include "Common/ByteString.h"
00030 #include "Common/InetAddr.h"
00031 #include "Common/Logger.h"
00032 #include "Common/ReferenceCount.h"
00033 #include "Common/Serialization.h"
00034 #include "Common/StaticBuffer.h"
00035
00036 #include "CommHeader.h"
00037
00038 namespace Hypertable {
00039
00071 class CommBuf : public ReferenceCount {
00072 public:
00073
00083 CommBuf(CommHeader &hdr, uint32_t len=0) : header(hdr), ext_ptr(0) {
00084 len += header.encoded_length();
00085 data.set(new uint8_t [len], len, true);
00086 data_ptr = data.base + header.encoded_length();
00087 header.set_total_length(len);
00088 }
00089
00102 CommBuf(CommHeader &hdr, uint32_t len, StaticBuffer &buffer)
00103 : ext(buffer), header(hdr) {
00104 len += header.encoded_length();
00105 data.set(new uint8_t [len], len, true);
00106 data_ptr = data.base + header.encoded_length();
00107 header.set_total_length(len+buffer.size);
00108 ext_ptr = ext.base;
00109 }
00110
00111
00125 CommBuf(CommHeader &hdr, uint32_t len,
00126 boost::shared_array<uint8_t> &ext_buffer, uint32_t ext_len) :
00127 header(hdr), ext_shared_array(ext_buffer) {
00128 len += header.encoded_length();
00129 data.set(new uint8_t [len], len, true);
00130 data_ptr = data.base + header.encoded_length();
00131 ext.base = ext_shared_array.get();
00132 ext.size = ext_len;
00133 ext.own = false;
00134 header.set_total_length(len+ext_len);
00135 ext_ptr = ext.base;
00136 }
00137
00145 void write_header_and_reset() {
00146 uint8_t *buf = data.base;
00147 HT_ASSERT((data_ptr - data.base) == (int)data.size);
00148 header.encode(&buf);
00149 data_ptr = data.base;
00150 ext_ptr = ext.base;
00151 }
00152
00156 void *get_data_ptr() { return data_ptr; }
00157
00161 uint8_t **get_data_ptr_address() { return &data_ptr; }
00162
00169 void *advance_data_ptr(size_t len) { data_ptr += len; return data_ptr; }
00170
00177 void append_bool(bool bval) { Serialization::encode_bool(&data_ptr, bval); }
00178
00185 void append_byte(uint8_t bval) { *data_ptr++ = bval; }
00186
00194 void append_bytes(const uint8_t *bytes, uint32_t len) {
00195 memcpy(data_ptr, bytes, len);
00196 data_ptr += len;
00197 }
00198
00207 void append_str16(const char *str) {
00208 Serialization::encode_str16(&data_ptr, str);
00209 }
00210
00219 void append_str16(const String &str) {
00220 Serialization::encode_str16(&data_ptr, str);
00221 }
00222
00229 void append_i16(uint16_t sval) {
00230 Serialization::encode_i16(&data_ptr, sval);
00231 }
00232
00239 void append_i32(uint32_t ival) {
00240 Serialization::encode_i32(&data_ptr, ival);
00241 }
00242
00249 void append_i64(uint64_t lval) {
00250 Serialization::encode_i64(&data_ptr, lval);
00251 }
00252
00261 void append_vstr(const char *str) {
00262 Serialization::encode_vstr(&data_ptr, str);
00263 }
00264
00273 void append_vstr(const String &str) {
00274 Serialization::encode_vstr(&data_ptr, str);
00275 }
00276
00286 void append_vstr(const void *str, uint32_t len) {
00287 Serialization::encode_vstr(&data_ptr, str, len);
00288 }
00289
00296 void append_inet_addr(const InetAddr &addr) {
00297 Serialization::encode_inet_addr(&data_ptr, addr);
00298 }
00299
00300 friend class IOHandlerData;
00301 friend class IOHandlerDatagram;
00302
00303 StaticBuffer data;
00304 StaticBuffer ext;
00305 CommHeader header;
00306
00307 protected:
00308 uint8_t *data_ptr;
00309 const uint8_t *ext_ptr;
00310 boost::shared_array<uint8_t> ext_shared_array;
00311 };
00312
00313 typedef intrusive_ptr<CommBuf> CommBufPtr;
00314
00315 }
00316
00317
00318 #endif // HYPERTABLE_COMMBUF_H