-
Notifications
You must be signed in to change notification settings - Fork 4
/
message.go
executable file
·91 lines (84 loc) · 2.96 KB
/
message.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Title: message.go
// Author: Jon Cody
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package wsrooms
import (
"bytes"
"encoding/binary"
)
// Message protocol followed by wsrooms servers and clients.
type Message struct {
RoomLength int // room name length
Room string // room name
EventLength int // event name length
Event string // event name
DstLength int // destination id length
Dst string // destination id
SrcLength int // source id length
Src string // source id
PayloadLength int // payload length
Payload []byte // payload
}
// Message protocol used only with a room's Send channel.
type RoomMessage struct {
Sender *Conn
Data []byte
}
// Returns a Message type from bytes.
func BytesToMessage(data []byte) *Message {
buf := bytes.NewBuffer(data)
msg := &Message{}
msg.RoomLength = int(binary.BigEndian.Uint32(buf.Next(4)))
msg.Room = string(buf.Next(msg.RoomLength))
msg.EventLength = int(binary.BigEndian.Uint32(buf.Next(4)))
msg.Event = string(buf.Next(msg.EventLength))
msg.DstLength = int(binary.BigEndian.Uint32(buf.Next(4)))
msg.Dst = string(buf.Next(msg.DstLength))
msg.SrcLength = int(binary.BigEndian.Uint32(buf.Next(4)))
msg.Src = string(buf.Next(msg.SrcLength))
msg.PayloadLength = int(binary.BigEndian.Uint32(buf.Next(4)))
msg.Payload = buf.Next(msg.PayloadLength)
return msg
}
// Returns bytes from a Message type.
func (msg *Message) Bytes() []byte {
buf := bytes.NewBuffer([]byte{})
binary.Write(buf, binary.BigEndian, uint32(msg.RoomLength))
buf.Write([]byte(msg.Room))
binary.Write(buf, binary.BigEndian, uint32(msg.EventLength))
buf.Write([]byte(msg.Event))
binary.Write(buf, binary.BigEndian, uint32(msg.DstLength))
buf.Write([]byte(msg.Dst))
binary.Write(buf, binary.BigEndian, uint32(msg.SrcLength))
buf.Write([]byte(msg.Src))
binary.Write(buf, binary.BigEndian, uint32(msg.PayloadLength))
buf.Write(msg.Payload)
return buf.Bytes()
}
// Constructs and returns a new Message type.
func ConstructMessage(room, event, dst, src string, payload []byte) *Message {
return &Message{
RoomLength: len(room),
Room: room,
EventLength: len(event),
Event: event,
DstLength: len(dst),
Dst: dst,
SrcLength: len(src),
Src: src,
PayloadLength: len(payload),
Payload: payload,
}
}