0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CommHeader.cc
Go to the documentation of this file.
1 /*
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 #include "Common/Compat.h"
30 #include "Common/Checksum.h"
31 #include "Common/Error.h"
32 #include "Common/Serialization.h"
33 #include "Common/Logger.h"
34 
35 #include "CommHeader.h"
36 
37 using namespace Hypertable;
38 
39 void CommHeader::encode(uint8_t **bufp) {
40  uint8_t *base = *bufp;
46  Serialization::encode_i32(bufp, id);
52  // compute and serialize header checksum
53  header_checksum = fletcher32(base, (*bufp)-base);
54  base += 6;
56 }
57 
58 void CommHeader::decode(const uint8_t **bufp, size_t *remainp) {
59  const uint8_t *base = *bufp;
60  if (*remainp < FIXED_LENGTH)
62  "Header size %d is less than the minumum fixed length %d",
63  (int)*remainp, (int)FIXED_LENGTH);
64  HT_TRY("decoding comm header",
65  version = Serialization::decode_i8(bufp, remainp);
66  header_len = Serialization::decode_i8(bufp, remainp);
67  alignment = Serialization::decode_i16(bufp, remainp);
68  flags = Serialization::decode_i16(bufp, remainp);
70  id = Serialization::decode_i32(bufp, remainp);
71  gid = Serialization::decode_i32(bufp, remainp);
72  total_len = Serialization::decode_i32(bufp, remainp);
73  timeout_ms = Serialization::decode_i32(bufp, remainp);
75  command = Serialization::decode_i64(bufp, remainp));
76  memset((void *)(base+6), 0, 4);
77  uint32_t checksum = fletcher32(base, *bufp-base);
78  if (checksum != header_checksum)
79  HT_THROWF(Error::COMM_HEADER_CHECKSUM_MISMATCH, "%u != %u", checksum,
81 }
uint32_t header_checksum
Header checksum (computed with this member 0)
Definition: CommHeader.h:140
uint16_t flags
Flags.
Definition: CommHeader.h:139
uint32_t timeout_ms
Request timeout.
Definition: CommHeader.h:144
uint16_t alignment
Align payload to this byte offset.
Definition: CommHeader.h:138
static const size_t FIXED_LENGTH
Definition: CommHeader.h:46
uint32_t decode_i32(const uint8_t **bufp, size_t *remainp)
Decode a 32-bit integer in little-endian order.
uint32_t gid
Group ID (see ApplicationQueue)
Definition: CommHeader.h:142
uint32_t fletcher32(const void *data8, size_t len8)
Compute fletcher32 checksum for arbitary data.
Definition: Checksum.cc:42
uint8_t decode_i8(const uint8_t **bufp, size_t *remainp)
Decode a 8-bit integer (a byte/character)
Definition: Serialization.h:60
uint64_t decode_i64(const uint8_t **bufp, size_t *remainp)
Decode a 64-bit integer in little-endian order.
void decode(const uint8_t **bufp, size_t *remainp)
Decode serialized header at *bufp The bufp pointer is advanced to the address immediately following t...
Definition: CommHeader.cc:58
uint16_t decode_i16(const uint8_t **bufp, size_t *remainp)
Decode a 16-bit integer in little-endian order.
void encode(uint8_t **bufp)
Encode header to memory pointed to by *bufp.
Definition: CommHeader.cc:39
uint32_t payload_checksum
Payload checksum (currently unused)
Definition: CommHeader.h:145
Logging routines and macros.
void encode_i32(uint8_t **bufp, uint32_t val)
Encode a 32-bit integer in little-endian order.
Compatibility Macros for C/C++.
void encode_i16(uint8_t **bufp, uint16_t val)
Encode a 16-bit integer in little-endian order.
void encode_i64(uint8_t **bufp, uint64_t val)
Encode a 64-bit integer in little-endian order.
Functions to serialize/deserialize primitives to/from a memory buffer.
uint32_t total_len
Total length of message including header.
Definition: CommHeader.h:143
Hypertable definitions
Implementation of checksum routines.
#define HT_THROWF(_code_, _fmt_,...)
Definition: Error.h:490
uint8_t header_len
Length of header.
Definition: CommHeader.h:137
#define HT_TRY(_s_, _code_)
Definition: Error.h:517
Declarations for CommHeader.
Error codes, Exception handling, error logging.
uint8_t version
Protocol version.
Definition: CommHeader.h:136
uint64_t command
Request command number.
Definition: CommHeader.h:146
void encode_i8(uint8_t **bufp, uint8_t val)
Encodes a byte into the given buffer.
Definition: Serialization.h:49