forked from chobie/momonga
-
Notifications
You must be signed in to change notification settings - Fork 0
/
will.go
52 lines (40 loc) · 1.12 KB
/
will.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014, Shuhei Tanuma. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package mqtt
import (
"encoding/binary"
"encoding/json"
"io"
)
type WillMessage struct {
Qos uint8 `json:"qos"`
Topic string `json:"topic"`
Message string `json:"message"`
Retain bool `json:retain`
}
func (self *WillMessage) WriteTo(w io.Writer) (int64, error) {
var topic_length uint16
var size int = 0
topic_length = uint16(len(self.Topic))
err := binary.Write(w, binary.BigEndian, topic_length)
w.Write([]byte(self.Topic))
size += 2 + int(topic_length)
message_length := uint16(len(self.Message))
err = binary.Write(w, binary.BigEndian, message_length)
w.Write([]byte(self.Message))
size += 2 + int(message_length)
return int64(size), err
}
func (self *WillMessage) Size() int {
var size int = 0
topic_length := uint16(len(self.Topic))
size += 2 + int(topic_length)
message_length := uint16(len(self.Message))
size += 2 + int(message_length)
return size
}
func (self *WillMessage) String() string {
b, _ := json.Marshal(self)
return string(b)
}