-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
181 lines (164 loc) · 3.59 KB
/
client.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package hoom
import (
"context"
"errors"
"fmt"
"io"
"net"
"time"
"github.com/kmcsr/go-pio/encoding"
"github.com/kmcsr/go-pio"
)
type joinedRoom struct{
*Room
token *RoomToken
onunload func()
}
func newJoinedRoom(room *Room, token *RoomToken)(r *joinedRoom, err error){
r = &joinedRoom{
Room: room,
token: token,
}
return
}
type Client struct{
mem *AuthedMember
conn *pio.Conn
server *net.TCPAddr
rooms map[uint32]*joinedRoom
}
func (m *AuthedMember)Dial(server *net.TCPAddr)(c *Client, err error){
var conn *net.TCPConn
conn, err = net.DialTCP("tcp", nil, server)
if err != nil {
return
}
c = &Client{
mem: m,
conn: pio.NewConn(conn, conn),
server: server,
rooms: make(map[uint32]*joinedRoom),
}
c.jnitPacket()
c.conn.OnPktNotFound = func(id uint32, body encoding.Reader){
loger.Warn("hoom.Client: Unexpected packet id:", id)
}
go c.conn.Serve()
var res pio.PacketBase
if res, err = c.conn.Ask(&CbindPkt{
Mem: m.GetMem(),
}); err != nil {
c.conn.Close()
return nil, err
}
switch rs := res.(type) {
case pio.Ok:
case *SerrorPkt:
return nil, fmt.Errorf("Cannot bind member: %s", rs.Error)
default:
panic("Unexpected result")
}
return
}
func (c *Client)Ping()(ping time.Duration, err error){
return c.conn.Ping()
}
func (c *Client)Close()(err error){
return c.conn.Close()
}
func (c *Client)Context()(context.Context){
return c.conn.Context()
}
func (c *Client)Rooms()(rooms []*Room){
rooms = make([]*Room, 0, len(c.rooms))
for _, r := range c.rooms {
rooms = append(rooms, r.Room)
}
return
}
func (c *Client)GetRoom(id uint32)(r *Room){
if r, ok := c.rooms[id]; ok {
return r.Room
}
return
}
func (c *Client)jnitPacket(){
c.conn.AddPacket(func()(pio.PacketBase){ return &SjoinPkt {c: c} })
c.conn.AddPacket(func()(pio.PacketBase){ return &SjoinBPkt {c: c} })
c.conn.AddPacket(func()(pio.PacketBase){ return &SleavePkt {c: c} })
c.conn.AddPacket(func()(pio.PacketBase){ return &SleaveBPkt{c: c} })
c.conn.AddPacket(func()(pio.PacketBase){ return &SerrorPkt {} })
}
func (c *Client)Join(id uint32)(rm *Room, err error){
var res pio.PacketBase
if res, err = c.conn.Ask(&CjoinPkt{
RoomId: id,
}); err != nil {
return
}
var room *joinedRoom
switch rs := res.(type) {
case *SjoinPkt:
if room, err = newJoinedRoom(rs.Room, rs.Token); err != nil {
return
}
case *SerrorPkt:
return nil, fmt.Errorf("Join error: %s", rs.Error)
default:
panic("Unexpected result")
}
c.rooms[id] = room
rm = room.Room
return
}
func (c *Client)dial()(conn *pio.Conn, err error){
var con *net.TCPConn
con, err = net.DialTCP("tcp", nil, c.server)
if err != nil {
return
}
conn = pio.NewConn(con, con)
conn.AddPacket(func()(pio.PacketBase){ return &SerrorPkt {} })
conn.OnPktNotFound = func(id uint32, body encoding.Reader){
loger.Warn("Unexpected packet id:", id)
}
go conn.Serve()
return
}
func (c *Client)Dial(id uint32)(conn io.ReadWriteCloser, err error){
room, ok := c.rooms[id]
if !ok {
err = fmt.Errorf("You didn't join the room(%d) yet", id)
return
}
var con *pio.Conn
if con, err = c.dial(); err != nil {
return
}
defer func(){ if con != nil {
con.Close()
}}()
var res pio.PacketBase
if res, err = con.Ask(&CdialPkt{
MemId: c.mem.Id(),
Token: room.token,
}); err != nil && !errors.Is(err, context.Canceled) {
return
}
if err == nil {
switch rs := res.(type) {
case pio.Ok:
case *SerrorPkt:
err = fmt.Errorf("Dial error: %s", rs.Error)
return
default:
panic("Unexpected result")
}
}
loger.Trace("hoom.Client: pio.Conn streaming")
if conn, err = con.AsStream(); err != nil {
return
}
con = nil
return
}