1 /*
2     Copyright (c) 2007-2009 iMatix Corporation
3     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
4 
5     This file is part of Crossroads I/O project.
6 
7     Crossroads I/O is free software; you can redistribute it and/or modify it
8     under the terms of the GNU Lesser General Public License as published by
9     the Free Software Foundation; either version 3 of the License, or
10     (at your option) any later version.
11 
12     Crossroads is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU Lesser General Public License for more details.
16 
17     You should have received a copy of the GNU Lesser General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __XS_WIRE_HPP_INCLUDED__
22 #define __XS_WIRE_HPP_INCLUDED__
23 
24 #include "stdint.hpp"
25 
26 //  Protocol-related constants.
27 #define XS_CMD_SUBSCRIBE 1
28 #define XS_CMD_UNSUBSCRIBE 2
29 
30 namespace xs
31 {
32 
33     //  Helper functions to convert different integer types to/from network
34     //  byte order.
35 
put_uint8(unsigned char * buffer_,uint8_t value)36     inline void put_uint8 (unsigned char *buffer_, uint8_t value)
37     {
38         *buffer_ = value;
39     }
40 
get_uint8(unsigned char * buffer_)41     inline uint8_t get_uint8 (unsigned char *buffer_)
42     {
43         return *buffer_;
44     }
45 
put_uint16(unsigned char * buffer_,uint16_t value)46     inline void put_uint16 (unsigned char *buffer_, uint16_t value)
47     {
48         buffer_ [0] = (unsigned char) (((value) >> 8) & 0xff);
49         buffer_ [1] = (unsigned char) (value & 0xff);
50     }
51 
get_uint16(unsigned char * buffer_)52     inline uint16_t get_uint16 (unsigned char *buffer_)
53     {
54         return
55             (((uint16_t) buffer_ [0]) << 8) |
56             ((uint16_t) buffer_ [1]);
57     }
58 
put_uint32(unsigned char * buffer_,uint32_t value)59     inline void put_uint32 (unsigned char *buffer_, uint32_t value)
60     {
61         buffer_ [0] = (unsigned char) (((value) >> 24) & 0xff);
62         buffer_ [1] = (unsigned char) (((value) >> 16) & 0xff);
63         buffer_ [2] = (unsigned char) (((value) >> 8) & 0xff);
64         buffer_ [3] = (unsigned char) (value & 0xff);
65     }
66 
get_uint32(unsigned char * buffer_)67     inline uint32_t get_uint32 (unsigned char *buffer_)
68     {
69         return
70             (((uint32_t) buffer_ [0]) << 24) |
71             (((uint32_t) buffer_ [1]) << 16) |
72             (((uint32_t) buffer_ [2]) << 8) |
73             ((uint32_t) buffer_ [3]);
74     }
75 
put_uint64(unsigned char * buffer_,uint64_t value)76     inline void put_uint64 (unsigned char *buffer_, uint64_t value)
77     {
78         buffer_ [0] = (unsigned char) (((value) >> 56) & 0xff);
79         buffer_ [1] = (unsigned char) (((value) >> 48) & 0xff);
80         buffer_ [2] = (unsigned char) (((value) >> 40) & 0xff);
81         buffer_ [3] = (unsigned char) (((value) >> 32) & 0xff);
82         buffer_ [4] = (unsigned char) (((value) >> 24) & 0xff);
83         buffer_ [5] = (unsigned char) (((value) >> 16) & 0xff);
84         buffer_ [6] = (unsigned char) (((value) >> 8) & 0xff);
85         buffer_ [7] = (unsigned char) (value & 0xff);
86     }
87 
get_uint64(unsigned char * buffer_)88     inline uint64_t get_uint64 (unsigned char *buffer_)
89     {
90         return
91             (((uint64_t) buffer_ [0]) << 56) |
92             (((uint64_t) buffer_ [1]) << 48) |
93             (((uint64_t) buffer_ [2]) << 40) |
94             (((uint64_t) buffer_ [3]) << 32) |
95             (((uint64_t) buffer_ [4]) << 24) |
96             (((uint64_t) buffer_ [5]) << 16) |
97             (((uint64_t) buffer_ [6]) << 8) |
98             ((uint64_t) buffer_ [7]);
99     }
100 
101 }
102 
103 #endif
104