1// flags:
2//           +---------------+---------------+
3//       byte|       0       |       1       |
4//           +---------------+---------------+
5//       bits|0 1 2 3 4 5 6 7|0 1 2 3 4 5 6 7|
6//           +-------+-------+---------------+  COMP. - Compression
7//          0| COMP. |  ENC. |  checksum   |X|  ENC.  - Encoding
8//           +-------+-------+---------------+  X     - Reserved for future
9
10package transport
11
12const ( // types of encoding over the wire.
13	// EncodingNone place holder.
14	EncodingNone byte = 0x00
15	// EncodingProtobuf uses protobuf as coding format.
16	EncodingProtobuf byte = 0x10
17)
18
19const ( // types of compression over the wire.
20	// CompressionNone does not apply compression on the payload.
21	CompressionNone byte = 0
22	// CompressionSnappy apply snappy compression on payload.
23	CompressionSnappy = 1
24	// CompressionGzip apply gzip compression on payload.
25	CompressionGzip = 2
26	// CompressionBzip2 apply bzip2 compression on the payload.
27	CompressionBzip2 = 3
28)
29
30// TransportFlag tell packet encoding and compression formats.
31type TransportFlag uint16
32
33// GetCompression returns the compression bits from flags
34func (flags TransportFlag) GetCompression() byte {
35	return byte(flags & TransportFlag(0x000F))
36}
37
38// SetSnappy will set packet compression to snappy
39func (flags TransportFlag) SetSnappy() TransportFlag {
40	return (flags & TransportFlag(0xFFF0)) | TransportFlag(CompressionSnappy)
41}
42
43// SetGzip will set packet compression to Gzip
44func (flags TransportFlag) SetGzip() TransportFlag {
45	return (flags & TransportFlag(0xFFF0)) | TransportFlag(CompressionGzip)
46}
47
48// SetBzip2 will set packet compression to bzip2
49func (flags TransportFlag) SetBzip2() TransportFlag {
50	return (flags & TransportFlag(0xFFF0)) | TransportFlag(CompressionBzip2)
51}
52
53// GetEncoding will get the encoding bits from flags
54func (flags TransportFlag) GetEncoding() byte {
55	return byte(flags & TransportFlag(0x00F0))
56}
57
58// SetProtobuf will set packet encoding to protobuf
59func (flags TransportFlag) SetProtobuf() TransportFlag {
60	return (flags & TransportFlag(0xFF0F)) | TransportFlag(EncodingProtobuf)
61}
62
63// GetChecksum will get the checksum from flags
64func (flags TransportFlag) GetChecksum() byte {
65	return byte((flags & TransportFlag(0x7F00)) >> 8)
66}
67
68// SetChecksum will set the checksum in flags
69func (flags TransportFlag) SetChecksum(c byte) TransportFlag {
70	return (flags & TransportFlag(0x80FF)) | (TransportFlag(c) << 8)
71}
72
73func (flags TransportFlag) IsValidEncoding() bool {
74
75	enc := flags.GetEncoding()
76	if enc == EncodingProtobuf {
77		return true
78	}
79	return false
80}
81