-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom.go
134 lines (118 loc) · 2.24 KB
/
room.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package hoom
import (
"io"
"net"
"github.com/google/uuid"
"github.com/kmcsr/go-pio/encoding"
hdata "github.com/kmcsr/go-hoom/data"
)
type Desc = hdata.Desc
type Room = hdata.Room
var NewRoom = hdata.NewRoom
func WriteRoom(w encoding.Writer, r *Room)(err error){
if err = w.WriteUint32(r.Id()); err != nil {
return
}
if err = WriteMember(w, r.Owner()); err != nil {
return
}
tid := r.TypeId()
if _, err = w.Write(tid[:]); err != nil {
return
}
if err = w.WriteString(r.Name()); err != nil {
return
}
mems := r.Members()
if err = w.WriteUint32((uint32)(len(mems))); err != nil {
return
}
for _, m := range mems {
if err = WriteMember(w, m); err != nil {
return
}
}
return
}
func ParseRoom(r encoding.Reader)(rm *Room, err error){
var (
id uint32
name string
mem *Member
tid uuid.UUID
)
if id, err = r.ReadUint32(); err != nil {
return
}
if mem, err = ParseMember(r); err != nil {
return
}
if _, err = io.ReadFull(r, tid[:]); err != nil {
return
}
if name, err = r.ReadString(); err != nil {
return
}
rm = NewRoom(id, mem, tid, name)
var n uint32
if n, err = r.ReadUint32(); err != nil {
return
}
for i := (uint32)(0); i < n; i++ {
if mem, err = ParseMember(r); err != nil {
return
}
rm.Put(mem)
}
return
}
type ServerRoom struct{
*Room
server *Server
target *net.TCPAddr
}
func (r *ServerRoom)Server()(*Server){
return r.server
}
func (r *ServerRoom)Target()(*net.TCPAddr){
return r.target
}
func (r *ServerRoom)Kick(id uint32, reason string)(err error){
return r.server.Kick(r.Id(), id, reason)
}
type RoomToken struct{
RoomId uint32
MemId uint32
Token uint64
Sign []byte
}
func (t *RoomToken)WriteTo(w encoding.Writer)(err error){
if err = w.WriteUint32(t.RoomId); err != nil {
return
}
if err = w.WriteUint32(t.MemId); err != nil {
return
}
if err = w.WriteUint64(t.Token); err != nil {
return
}
if err = w.WriteBytes(t.Sign); err != nil {
return
}
return
}
func (t *RoomToken)ParseFrom(r encoding.Reader)(err error){
if t.RoomId, err = r.ReadUint32(); err != nil {
return
}
if t.MemId, err = r.ReadUint32(); err != nil {
return
}
if t.Token, err = r.ReadUint64(); err != nil {
return
}
if t.Sign, err = r.ReadBytes(); err != nil {
return
}
return
}