forked from thinkgos/go-iecp5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
executable file
·166 lines (146 loc) · 3.95 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
166
// Copyright 2020 thinkgos (thinkgo@aliyun.com). All rights reserved.
// Use of this source code is governed by a version 3 of the GNU General
// Public License, license that can be found in the LICENSE file.
package cs104
import (
"context"
"crypto/tls"
"net"
"sync"
"time"
"github.com/jiguorui/go-iecp5/asdu"
"github.com/jiguorui/go-iecp5/clog"
)
// timeoutResolution is seconds according to companion standard 104,
// subclass 6.9, caption "Definition of time outs". However, then
// of a second make this system much more responsive i.c.w. S-frames.
const timeoutResolution = 100 * time.Millisecond
// Server the common server
type Server struct {
config Config
params asdu.Params
handler ServerHandlerInterface
TLSConfig *tls.Config
mux sync.Mutex
sessions map[*SrvSession]struct{}
listen net.Listener
onConnection func(asdu.Connect)
connectionLost func(asdu.Connect)
clog.Clog
wg sync.WaitGroup
}
// NewServer new a server, default config and default asdu.ParamsWide params
func NewServer(handler ServerHandlerInterface) *Server {
return &Server{
config: DefaultConfig(),
params: *asdu.ParamsWide,
handler: handler,
sessions: make(map[*SrvSession]struct{}),
Clog: clog.NewLogger("cs104 server => "),
}
}
// SetConfig set config if config is valid it will use DefaultConfig()
func (sf *Server) SetConfig(cfg Config) *Server {
if err := cfg.Valid(); err != nil {
sf.config = DefaultConfig()
} else {
sf.config = cfg
}
return sf
}
// SetParams set asdu params if params is valid it will use asdu.ParamsWide
func (sf *Server) SetParams(p *asdu.Params) *Server {
if err := p.Valid(); err != nil {
sf.params = *asdu.ParamsWide
} else {
sf.params = *p
}
return sf
}
// ListenAndServer run the server
func (sf *Server) ListenAndServer(addr string) {
listen, err := net.Listen("tcp", addr)
if err != nil {
sf.Error("server run failed, %v", err)
return
}
sf.mux.Lock()
sf.listen = listen
sf.mux.Unlock()
ctx, cancel := context.WithCancel(context.Background())
defer func() {
cancel()
_ = sf.Close()
sf.Debug("server stop")
}()
sf.Debug("server run")
for {
conn, err := listen.Accept()
if err != nil {
sf.Error("server run failed, %v", err)
return
}
sf.wg.Add(1)
go func() {
sess := &SrvSession{
config: &sf.config,
params: &sf.params,
handler: sf.handler,
conn: conn,
rcvASDU: make(chan []byte, sf.config.RecvUnAckLimitW<<4),
sendASDU: make(chan []byte, sf.config.SendUnAckLimitK<<4),
rcvRaw: make(chan []byte, sf.config.RecvUnAckLimitW<<5),
sendRaw: make(chan []byte, sf.config.SendUnAckLimitK<<5), // may not block!
onConnection: sf.onConnection,
connectionLost: sf.connectionLost,
Clog: sf.Clog,
}
////fmt.Println(len())
sf.mux.Lock()
sf.sessions[sess] = struct{}{}
sf.mux.Unlock()
sess.run(ctx)
sf.mux.Lock()
delete(sf.sessions, sess)
sf.mux.Unlock()
sf.wg.Done()
}()
}
}
// Close close the server
func (sf *Server) Close() error {
var err error
sf.mux.Lock()
if sf.listen != nil {
err = sf.listen.Close()
sf.listen = nil
}
sf.mux.Unlock()
sf.wg.Wait()
return err
}
// Send imp interface Connect
func (sf *Server) Send(a *asdu.ASDU) error {
sf.mux.Lock()
for k := range sf.sessions {
_ = k.Send(a.Clone())
}
sf.mux.Unlock()
return nil
}
// Params imp interface Connect
func (sf *Server) Params() *asdu.Params { return &sf.params }
// UnderlyingConn imp interface Connect
func (sf *Server) UnderlyingConn() net.Conn { return nil }
// SetInfoObjTimeZone set info object time zone
func (sf *Server) SetInfoObjTimeZone(zone *time.Location) {
sf.params.InfoObjTimeZone = zone
}
// SetOnConnectionHandler set on connect handler
func (sf *Server) SetOnConnectionHandler(f func(asdu.Connect)) {
sf.onConnection = f
}
// SetConnectionLostHandler set connect lost handler
func (sf *Server) SetConnectionLostHandler(f func(asdu.Connect)) {
sf.connectionLost = f
}