/
client.go
153 lines (132 loc) · 3.92 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
package cli
import (
"context"
"errors"
"fmt"
"git.golaxy.org/core/util/uid"
"git.golaxy.org/framework/net/gtp"
"git.golaxy.org/framework/net/gtp/transport"
"git.golaxy.org/framework/util/binaryutil"
"git.golaxy.org/framework/util/concurrent"
"go.uber.org/zap"
"net"
"sync"
)
var (
ErrReconnectFailed = errors.New("reconnect failed")
ErrInactiveTimeout = errors.New("inactive timeout")
)
// IWatcher 监听器
type IWatcher interface {
context.Context
Terminate() <-chan struct{}
}
// Client 客户端
type Client struct {
context.Context
terminate context.CancelCauseFunc
terminatedChan chan struct{}
wg sync.WaitGroup
mutex sync.Mutex
options ClientOptions
sessionId uid.Id
endpoint string
transceiver transport.Transceiver
eventDispatcher transport.EventDispatcher
trans transport.TransProtocol
ctrl transport.CtrlProtocol
reconnectChan chan struct{}
renewChan chan struct{}
futures concurrent.Futures
dataWatchers concurrent.LockedSlice[*_DataWatcher]
eventWatchers concurrent.LockedSlice[*_EventWatcher]
logger *zap.SugaredLogger
}
// String implements fmt.Stringer
func (c *Client) String() string {
return fmt.Sprintf(`{"session_id":%q, "token":%q, "end_point":%q}`, c.GetSessionId(), c.GetToken(), c.GetEndpoint())
}
// GetSessionId 获取会话Id
func (c *Client) GetSessionId() uid.Id {
return c.sessionId
}
// GetToken 获取token
func (c *Client) GetToken() string {
return c.options.AuthToken
}
// GetEndpoint 获取服务器地址
func (c *Client) GetEndpoint() string {
return c.endpoint
}
// GetLocalAddr 获取本地地址
func (c *Client) GetLocalAddr() net.Addr {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.transceiver.Conn.LocalAddr()
}
// GetRemoteAddr 获取对端地址
func (c *Client) GetRemoteAddr() net.Addr {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.transceiver.Conn.RemoteAddr()
}
// GetFutures 获取异步模型Future控制器
func (c *Client) GetFutures() concurrent.IFutures {
return &c.futures
}
// GetLogger 获取logger
func (c *Client) GetLogger() *zap.SugaredLogger {
return c.logger
}
// SendData 发送数据
func (c *Client) SendData(data []byte) error {
return c.trans.SendData(data)
}
// WatchData 监听数据
func (c *Client) WatchData(ctx context.Context, handler RecvDataHandler) IWatcher {
return c.newDataWatcher(ctx, handler)
}
// SendEvent 发送自定义事件
func (c *Client) SendEvent(event transport.Event[gtp.MsgReader]) error {
return transport.Retry{
Transceiver: &c.transceiver,
Times: c.options.IORetryTimes,
}.Send(c.transceiver.Send(event.Pack()))
}
// WatchEvent 监听自定义事件
func (c *Client) WatchEvent(ctx context.Context, handler RecvEventHandler) IWatcher {
return c.newEventWatcher(ctx, handler)
}
// SendDataChan 发送数据的channel
func (c *Client) SendDataChan() chan<- binaryutil.RecycleBytes {
if c.options.SendDataChan == nil {
c.logger.Panic("send data channel size less equal 0, can't be used")
}
return c.options.SendDataChan
}
// RecvDataChan 接收数据的channel
func (c *Client) RecvDataChan() <-chan binaryutil.RecycleBytes {
if c.options.RecvDataChan == nil {
c.logger.Panic("receive data channel size less equal 0, can't be used")
}
return c.options.RecvDataChan
}
// SendEventChan 发送自定义事件的channel
func (c *Client) SendEventChan() chan<- transport.Event[gtp.MsgReader] {
if c.options.SendEventChan == nil {
c.logger.Panic("send event channel size less equal 0, can't be used")
}
return c.options.SendEventChan
}
// RecvEventChan 接收自定义事件的channel
func (c *Client) RecvEventChan() <-chan transport.Event[gtp.Msg] {
if c.options.RecvEventChan == nil {
c.logger.Panic("receive event channel size less equal 0, can't be used")
}
return c.options.RecvEventChan
}
// Close 关闭
func (c *Client) Close(err error) <-chan struct{} {
c.terminate(err)
return c.terminatedChan
}