-
Notifications
You must be signed in to change notification settings - Fork 1
/
message_encode_decode.go
41 lines (31 loc) · 1.14 KB
/
message_encode_decode.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
package main
import "encoding/binary"
const headerLenght = 4
func EncodeMessage(messageChannel MessageChannel, message Message) []byte {
channelLenght := len(messageChannel)
messageLenght := len(message)
totalLenght := headerLenght + channelLenght + messageLenght
data := make([]byte, totalLenght)
writeIntToByteArray(data[0:], uint32(channelLenght))
writeStringToByteArray(data[headerLenght:], string(messageChannel))
copy(data[headerLenght+channelLenght:], message)
return data
}
func DecodeMessage(data []byte) (MessageChannel, Message, error) {
channelLenght := readIntFromByteArray(data[0:])
messageChannel := readStringFromByteArray(data[headerLenght : headerLenght+channelLenght])
message := data[headerLenght+channelLenght:]
return MessageChannel(messageChannel), Message(message), nil
}
func writeIntToByteArray(array []byte, value uint32) {
binary.BigEndian.PutUint32(array, value)
}
func readIntFromByteArray(array []byte) uint32 {
return binary.BigEndian.Uint32(array)
}
func writeStringToByteArray(array []byte, value string) {
copy(array, []byte(value))
}
func readStringFromByteArray(array []byte) string {
return string(array)
}