forked from andeya/tp-micro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
147 lines (128 loc) · 5.41 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
// Copyright 2018 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ant
import (
"net"
"time"
"github.com/henrylee2cn/cfgo"
tp "github.com/henrylee2cn/teleport"
"github.com/henrylee2cn/teleport/socket"
binder "github.com/henrylee2cn/tp-ext/plugin-binder"
heartbeat "github.com/henrylee2cn/tp-ext/plugin-heartbeat"
)
// SrvConfig server config
// Note:
// yaml tag is used for github.com/henrylee2cn/cfgo
// ini tag is used for github.com/henrylee2cn/ini
type SrvConfig struct {
TlsCertFile string `yaml:"tls_cert_file" ini:"tls_cert_file" comment:"TLS certificate file path"`
TlsKeyFile string `yaml:"tls_key_file" ini:"tls_key_file" comment:"TLS key file path"`
DefaultSessionAge time.Duration `yaml:"default_session_age" ini:"default_session_age" comment:"Default session max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
DefaultContextAge time.Duration `yaml:"default_context_age" ini:"default_context_age" comment:"Default PULL or PUSH context max age, if less than or equal to 0, no time limit; ns,µs,ms,s,m,h"`
SlowCometDuration time.Duration `yaml:"slow_comet_duration" ini:"slow_comet_duration" comment:"Slow operation alarm threshold; ns,µs,ms,s ..."`
DefaultBodyCodec string `yaml:"default_body_codec" ini:"default_body_codec" comment:"Default body codec type id"`
PrintBody bool `yaml:"print_body" ini:"print_body" comment:"Is print body or not"`
CountTime bool `yaml:"count_time" ini:"count_time" comment:"Is count cost time or not"`
Network string `yaml:"network" ini:"network" comment:"Network; tcp, tcp4, tcp6, unix or unixpacket"`
ListenAddress string `yaml:"listen_address" ini:"listen_address" comment:"Listen address; for server role"`
EnableHeartbeat bool `yaml:"enable_heartbeat" ini:"enable_heartbeat" comment:"enable heartbeat"`
}
// Reload Bi-directionally synchronizes config between YAML file and memory.
func (s *SrvConfig) Reload(bind cfgo.BindFunc) error {
return bind()
}
func (s *SrvConfig) peerConfig() tp.PeerConfig {
return tp.PeerConfig{
DefaultSessionAge: s.DefaultSessionAge,
DefaultContextAge: s.DefaultContextAge,
SlowCometDuration: s.SlowCometDuration,
DefaultBodyCodec: s.DefaultBodyCodec,
PrintBody: s.PrintBody,
CountTime: s.CountTime,
Network: s.Network,
ListenAddress: s.ListenAddress,
}
}
// Server server peer
type Server struct {
peer tp.Peer
}
// NewServer creates a server peer.
func NewServer(cfg SrvConfig, plugin ...tp.Plugin) *Server {
plugin = append(
[]tp.Plugin{
binder.NewStructArgsBinder(RerrCodeBind, "invalid parameter"),
},
plugin...,
)
if cfg.EnableHeartbeat {
plugin = append(plugin, heartbeat.NewPong())
}
peer := tp.NewPeer(cfg.peerConfig(), plugin...)
if len(cfg.TlsCertFile) > 0 && len(cfg.TlsKeyFile) > 0 {
err := peer.SetTlsConfigFromFile(cfg.TlsCertFile, cfg.TlsKeyFile)
if err != nil {
tp.Fatalf("%v", err)
}
}
return &Server{
peer: peer,
}
}
// SubRoute adds handler group.
func (s *Server) SubRoute(pathPrefix string, plugin ...tp.Plugin) *tp.Router {
return s.peer.SubRoute(pathPrefix, plugin...)
}
// RoutePull registers PULL handler.
func (s *Server) RoutePull(ctrlStruct interface{}, plugin ...tp.Plugin) {
s.peer.RoutePull(ctrlStruct, plugin...)
}
// RoutePush registers PUSH handler.
func (s *Server) RoutePush(ctrlStruct interface{}, plugin ...tp.Plugin) {
s.peer.RoutePush(ctrlStruct, plugin...)
}
// SetUnknownPull sets the default handler,
// which is called when no handler for PULL is found.
func (s *Server) SetUnknownPull(fn func(tp.UnknownPullCtx) (interface{}, *tp.Rerror), plugin ...tp.Plugin) {
s.peer.SetUnknownPull(fn, plugin...)
}
// SetUnknownPush sets the default handler,
// which is called when no handler for PUSH is found.
func (s *Server) SetUnknownPush(fn func(tp.UnknownPushCtx) *tp.Rerror, plugin ...tp.Plugin) {
s.peer.SetUnknownPush(fn, plugin...)
}
// Close closes server.
func (s *Server) Close() error {
return s.peer.Close()
}
// CountSession returns the number of sessions.
func (s *Server) CountSession() int {
return s.peer.CountSession()
}
// GetSession gets the session by id.
func (s *Server) GetSession(sessionId string) (tp.Session, bool) {
return s.peer.GetSession(sessionId)
}
// Listen turns on the listening service.
func (s *Server) Listen(protoFunc ...socket.ProtoFunc) error {
return s.peer.Listen(protoFunc...)
}
// RangeSession ranges all sessions. If fn returns false, stop traversing.
func (s *Server) RangeSession(fn func(sess tp.Session) bool) {
s.peer.RangeSession(fn)
}
// ServeConn serves the connection and returns a session.
func (s *Server) ServeConn(conn net.Conn, protoFunc ...socket.ProtoFunc) (tp.Session, error) {
return s.peer.ServeConn(conn, protoFunc...)
}