-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathservice.go
187 lines (153 loc) · 3.56 KB
/
service.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
182
183
184
185
186
187
package zero
import (
"context"
"errors"
"net"
"sync"
"time"
)
// SocketService struct
type SocketService struct {
onMessage func(*Session, *Message)
onConnect func(*Session)
onDisconnect func(*Session, error)
sessions *sync.Map
hbInterval time.Duration
hbTimeout time.Duration
laddr string
status int
listener net.Listener
stopCh chan error
}
// NewSocketService create a new socket service
func NewSocketService(laddr string) (*SocketService, error) {
l, err := net.Listen("tcp", laddr)
if err != nil {
return nil, err
}
s := &SocketService{
sessions: &sync.Map{},
stopCh: make(chan error),
hbInterval: 0 * time.Second,
hbTimeout: 0 * time.Second,
laddr: laddr,
status: STInited,
listener: l,
}
return s, nil
}
// RegMessageHandler register message handler
func (s *SocketService) RegMessageHandler(handler func(*Session, *Message)) {
s.onMessage = handler
}
// RegConnectHandler register connect handler
func (s *SocketService) RegConnectHandler(handler func(*Session)) {
s.onConnect = handler
}
// RegDisconnectHandler register disconnect handler
func (s *SocketService) RegDisconnectHandler(handler func(*Session, error)) {
s.onDisconnect = handler
}
// Serv Start socket service
func (s *SocketService) Serv() {
s.status = STRunning
ctx, cancel := context.WithCancel(context.Background())
defer func() {
s.status = STStop
cancel()
s.listener.Close()
}()
go s.acceptHandler(ctx)
for {
select {
case <-s.stopCh:
return
}
}
}
func (s *SocketService) acceptHandler(ctx context.Context) {
for {
c, err := s.listener.Accept()
if err != nil {
s.stopCh <- err
return
}
go s.connectHandler(ctx, c)
}
}
func (s *SocketService) connectHandler(ctx context.Context, c net.Conn) {
conn := NewConn(c, s.hbInterval, s.hbTimeout)
session := NewSession(conn)
s.sessions.Store(session.GetSessionID(), session)
connctx, cancel := context.WithCancel(ctx)
defer func() {
cancel()
conn.Close()
s.sessions.Delete(session.GetSessionID())
}()
go conn.readCoroutine(connctx)
go conn.writeCoroutine(connctx)
if s.onConnect != nil {
s.onConnect(session)
}
for {
select {
case err := <-conn.done:
if s.onDisconnect != nil {
s.onDisconnect(session, err)
}
return
case msg := <-conn.messageCh:
if s.onMessage != nil {
s.onMessage(session, msg)
}
}
}
}
// GetStatus get socket service status
func (s *SocketService) GetStatus() int {
return s.status
}
// Stop stop socket service with reason
func (s *SocketService) Stop(reason string) {
s.stopCh <- errors.New(reason)
}
// SetHeartBeat set heart beat
func (s *SocketService) SetHeartBeat(hbInterval time.Duration, hbTimeout time.Duration) error {
if s.status == STRunning {
return errors.New("Can't set heart beat on service running")
}
s.hbInterval = hbInterval
s.hbTimeout = hbTimeout
return nil
}
// GetConnsCount get connect count
func (s *SocketService) GetConnsCount() int {
var count int
s.sessions.Range(func(k, v interface{}) bool {
count++
return true
})
return count
}
// Unicast Unicast with session ID
func (s *SocketService) Unicast(sid string, msg *Message) {
v, ok := s.sessions.Load(sid)
if ok {
session := v.(*Session)
err := session.GetConn().SendMessage(msg)
if err != nil {
return
}
}
}
// Broadcast Broadcast to all connections
func (s *SocketService) Broadcast(msg *Message) {
s.sessions.Range(func(k, v interface{}) bool {
s := v.(*Session)
if err := s.GetConn().SendMessage(msg); err != nil {
// log.Println(err)
}
return true
})
}