0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CommBuf.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
29 #ifndef AsyncComm_CommBuf_h
30 #define AsyncComm_CommBuf_h
31 
32 #include "CommHeader.h"
33 
34 #include <Common/ByteString.h>
35 #include <Common/InetAddr.h>
36 #include <Common/Logger.h>
37 #include <Common/Serialization.h>
38 #include <Common/StaticBuffer.h>
39 
40 #include <boost/shared_array.hpp>
41 
42 #include <memory>
43 #include <string>
44 
45 namespace Hypertable {
46 
79  class CommBuf {
80  public:
81 
89  CommBuf(CommHeader &hdr, uint32_t len=0) : header(hdr), ext_ptr(0) {
90  len += header.encoded_length();
91  data.set(new uint8_t [len], len, true);
94  }
95 
106  CommBuf(CommHeader &hdr, uint32_t len, StaticBuffer &buffer)
107  : ext(buffer), header(hdr) {
108  len += header.encoded_length();
109  data.set(new uint8_t [len], len, true);
111  header.set_total_length(len+buffer.size);
112  ext_ptr = ext.base;
113  }
114 
115 
127  CommBuf(CommHeader &hdr, uint32_t len,
128  boost::shared_array<uint8_t> &ext_buffer, uint32_t ext_len) :
129  header(hdr), ext_shared_array(ext_buffer) {
130  len += header.encoded_length();
131  data.set(new uint8_t [len], len, true);
133  ext.base = ext_shared_array.get();
134  ext.size = ext_len;
135  ext.own = false;
136  header.set_total_length(len+ext_len);
137  ext_ptr = ext.base;
138  }
139 
147  uint8_t *buf = data.base;
148  HT_ASSERT((data_ptr-data.base) == (int)data.size || data_ptr == data.base);
149  header.encode(&buf);
150  data_ptr = data.base;
151  ext_ptr = ext.base;
152  }
153 
156  void *get_data_ptr() { return data_ptr; }
157 
160  uint8_t **get_data_ptr_address() { return &data_ptr; }
161 
167  void *advance_data_ptr(size_t len) { data_ptr += len; return data_ptr; }
168 
173  void append_bool(bool bval) { Serialization::encode_bool(&data_ptr, bval); }
174 
179  void append_byte(uint8_t bval) { *data_ptr++ = bval; }
180 
187  void append_bytes(const uint8_t *bytes, uint32_t len) {
188  memcpy(data_ptr, bytes, len);
189  data_ptr += len;
190  }
191 
199  void append_str16(const char *str) {
201  }
202 
211  void append_str16(const String &str) {
213  }
214 
221  void append_i16(uint16_t sval) {
223  }
224 
230  void append_i32(uint32_t ival) {
232  }
233 
239  void append_i64(uint64_t lval) {
241  }
242 
250  void append_vstr(const char *str) {
252  }
253 
260  void append_vstr(const String &str) {
262  }
263 
272  void append_vstr(const void *str, uint32_t len) {
274  }
275 
281  void append_inet_addr(const InetAddr &addr) {
283  }
284 
285  friend class IOHandlerData;
286  friend class IOHandlerDatagram;
287 
291 
292  protected:
293 
295  uint8_t *data_ptr;
296 
298  const uint8_t *ext_ptr;
299 
301  boost::shared_array<uint8_t> ext_shared_array;
302  };
303 
305  typedef std::shared_ptr<CommBuf> CommBufPtr;
307 } // namespace Hypertable
308 
309 
310 #endif // AsyncComm_CommBuf_h
A memory buffer of static size.
Definition: StaticBuffer.h:45
void set(uint8_t *data, uint32_t len, bool take_ownership=true)
Sets data pointer; the existing buffer is discarded and deleted.
Definition: StaticBuffer.h:175
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
I/O handler for datagram (UDP) sockets.
void set_total_length(uint32_t len)
Set total length of message (header + payload).
Definition: CommHeader.h:121
boost::shared_array< uint8_t > ext_shared_array
Smart pointer to extended buffer memory.
Definition: CommBuf.h:301
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
uint8_t ** get_data_ptr_address()
Returns address of the primary buffer internal data pointer.
Definition: CommBuf.h:160
void append_str16(const String &str)
Appends a String to the primary buffer.
Definition: CommBuf.h:211
void write_header_and_reset()
Encodes the header at the beginning of the primary buffer.
Definition: CommBuf.h:146
void encode_inet_addr(uint8_t **bufp, const InetAddr &addr)
Encode an InetAddr structure.
void append_vstr(const char *str)
Appends a c-style string to the primary buffer.
Definition: CommBuf.h:250
#define HT_ASSERT(_e_)
Definition: Logger.h:396
CommBuf(CommHeader &hdr, uint32_t len, StaticBuffer &buffer)
Constructor.
Definition: CommBuf.h:106
CommHeader header
Comm header.
Definition: CommBuf.h:290
Encapsulate an internet address.
Definition: InetAddr.h:66
void encode(uint8_t **bufp)
Encode header to memory pointed to by *bufp.
Definition: CommHeader.cc:39
StaticBuffer data
Primary data buffer.
Definition: CommBuf.h:288
std::shared_ptr< CommBuf > CommBufPtr
Smart pointer to CommBuf.
Definition: CommBuf.h:305
Logging routines and macros.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
void append_i16(uint16_t sval)
Appends a 16-bit integer to the primary buffer.
Definition: CommBuf.h:221
const uint8_t * ext_ptr
Write pointer into ext buffer.
Definition: CommBuf.h:298
void encode_i16(uint8_t **bufp, uint16_t val)
Encode a 16-bit integer in little-endian order.
void append_vstr(const String &str)
Appends a String to the primary buffer.
Definition: CommBuf.h:260
uint8_t * data_ptr
Write pointer into data buffer.
Definition: CommBuf.h:295
I/O handler for TCP sockets.
Definition: IOHandlerData.h:51
void encode_i64(uint8_t **bufp, uint64_t val)
Encode a 64-bit integer in little-endian order.
void * advance_data_ptr(size_t len)
Advance the primary buffer internal data pointer by len bytes.
Definition: CommBuf.h:167
void append_i32(uint32_t ival)
Appends a 32-bit integer to the primary buffer.
Definition: CommBuf.h:230
Functions to serialize/deserialize primitives to/from a memory buffer.
A memory buffer of static size.
CommBuf(CommHeader &hdr, uint32_t len, boost::shared_array< uint8_t > &ext_buffer, uint32_t ext_len)
Constructor.
Definition: CommBuf.h:127
StaticBuffer ext
Extended buffer.
Definition: CommBuf.h:289
void append_str16(const char *str)
Appends a c-style string to the primary buffer.
Definition: CommBuf.h:199
CommBuf(CommHeader &hdr, uint32_t len=0)
Constructor.
Definition: CommBuf.h:89
void append_i64(uint64_t lval)
Appends a 64-bit integer to the primary buffer.
Definition: CommBuf.h:239
void encode_vstr(uint8_t **bufp, const void *buf, size_t len)
Encode a buffer as variable length string (vint64, data, null)
Hypertable definitions
void encode_bool(uint8_t **bufp, bool bval)
Encodes a boolean into the given buffer.
Definition: Serialization.h:84
Header for messages transmitted via AsyncComm.
Definition: CommHeader.h:40
size_t encoded_length() const
Returns encoded length of header.
Definition: CommHeader.h:95
void append_bytes(const uint8_t *bytes, uint32_t len)
Appends a sequence of bytes to the primary buffer.
Definition: CommBuf.h:187
Internet address wrapper classes and utility functions.
Message buffer for holding data to be transmitted over a network.
Definition: CommBuf.h:79
void append_vstr(const void *str, uint32_t len)
Appends a variable sized string to the primary buffer.
Definition: CommBuf.h:272
A serializable ByteString.
void append_byte(uint8_t bval)
Appends a byte of data to the primary buffer.
Definition: CommBuf.h:179
void encode_str16(uint8_t **bufp, const void *str, uint16_t len)
Encodes a string buffer into the given buffer.
Declarations for CommHeader.
void * get_data_ptr()
Returns the primary buffer internal data pointer.
Definition: CommBuf.h:156
void append_inet_addr(const InetAddr &addr)
Appends an InetAddr structure to the primary buffer.
Definition: CommBuf.h:281
void append_bool(bool bval)
Appends a boolean value to the primary buffer.
Definition: CommBuf.h:173