forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
165 lines (145 loc) · 3.94 KB
/
server.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
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package c2s
import (
"context"
"fmt"
"net"
"strconv"
"sync"
"sync/atomic"
"time"
"github.com/sxmpp/jackal/component"
streamerror "github.com/sxmpp/jackal/errors"
"github.com/sxmpp/jackal/log"
"github.com/sxmpp/jackal/module"
"github.com/sxmpp/jackal/router"
"github.com/sxmpp/jackal/storage/repository"
"github.com/sxmpp/jackal/stream"
"github.com/sxmpp/jackal/transport"
)
var listenerProvider = net.Listen
type server struct {
cfg *Config
mods *module.Modules
comps *component.Components
router router.Router
userRep repository.User
blockListRep repository.BlockList
inConnectionsMu sync.Mutex
inConnections map[string]stream.C2S
ln net.Listener
stmSeq uint64
listening uint32
}
func newC2SServer(config *Config, mods *module.Modules, comps *component.Components, router router.Router, userRep repository.User, blockListRep repository.BlockList) c2sServer {
return &server{
cfg: config,
mods: mods,
comps: comps,
router: router,
userRep: userRep,
blockListRep: blockListRep,
inConnections: make(map[string]stream.C2S),
}
}
func (s *server) start() {
bindAddr := s.cfg.Transport.BindAddress
port := s.cfg.Transport.Port
address := bindAddr + ":" + strconv.Itoa(port)
log.Infof("%s: listening at %s [transport: %v]", s.cfg.ID, address, s.cfg.Transport.Type)
var err error
switch s.cfg.Transport.Type {
case transport.Socket:
err = s.listenSocketConn(address)
}
if err != nil {
log.Fatalf("%v", err)
}
}
func (s *server) listenSocketConn(address string) error {
ln, err := listenerProvider("tcp", address)
if err != nil {
return err
}
s.ln = ln
atomic.StoreUint32(&s.listening, 1)
for atomic.LoadUint32(&s.listening) == 1 {
conn, err := ln.Accept()
if err == nil {
go s.startStream(transport.NewSocketTransport(conn), s.cfg.KeepAlive)
continue
}
}
return nil
}
func (s *server) shutdown(ctx context.Context) error {
if atomic.CompareAndSwapUint32(&s.listening, 1, 0) {
// stop listening
switch s.cfg.Transport.Type {
case transport.Socket:
if err := s.ln.Close(); err != nil {
return err
}
}
// close all connections
c, err := s.closeConnections(ctx)
if err != nil {
return err
}
log.Infof("%s: closed %d connection(s)", s.cfg.ID, c)
}
return nil
}
func (s *server) startStream(tr transport.Transport, keepAlive time.Duration) {
cfg := &streamConfig{
resourceConflict: s.cfg.ResourceConflict,
connectTimeout: s.cfg.ConnectTimeout,
keepAlive: s.cfg.KeepAlive,
timeout: s.cfg.Timeout,
maxStanzaSize: s.cfg.MaxStanzaSize,
sasl: s.cfg.SASL,
compression: s.cfg.Compression,
onDisconnect: s.unregisterStream,
}
stm := newStream(s.nextID(), cfg, tr, s.mods, s.comps, s.router, s.userRep, s.blockListRep)
s.registerStream(stm)
}
func (s *server) registerStream(stm stream.C2S) {
s.inConnectionsMu.Lock()
s.inConnections[stm.ID()] = stm
s.inConnectionsMu.Unlock()
log.Infof("registered c2s stream... (id: %s)", stm.ID())
}
func (s *server) unregisterStream(stm stream.C2S) {
s.inConnectionsMu.Lock()
delete(s.inConnections, stm.ID())
s.inConnectionsMu.Unlock()
log.Infof("unregistered c2s stream... (id: %s)", stm.ID())
}
func (s *server) nextID() string {
return fmt.Sprintf("c2s:%s:%d", s.cfg.ID, atomic.AddUint64(&s.stmSeq, 1))
}
func (s *server) closeConnections(ctx context.Context) (count int, err error) {
s.inConnectionsMu.Lock()
for _, stm := range s.inConnections {
select {
case <-closeConn(ctx, stm):
count++
case <-ctx.Done():
return 0, ctx.Err()
}
}
s.inConnectionsMu.Unlock()
return count, nil
}
func closeConn(ctx context.Context, stm stream.InStream) <-chan bool {
c := make(chan bool, 1)
go func() {
stm.Disconnect(ctx, streamerror.ErrSystemShutdown)
c <- true
}()
return c
}