/
server.go
58 lines (48 loc) · 1.12 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
package watch_xixun
import (
"github.com/giskook/gotcp"
"log"
"net"
"time"
)
type ServerConfig struct {
Listener *net.TCPListener
AcceptTimeout time.Duration
}
type Server struct {
config *ServerConfig
srv *gotcp.Server
checkconnsticker *time.Ticker
Dbsrv *DbServer
}
var Gserver *Server
func SetServer(server *Server) {
Gserver = server
}
func GetServer() *Server {
return Gserver
}
func NewServer(srv *gotcp.Server, config *ServerConfig, dbsrv *DbServer) *Server {
serverstatistics := GetConfiguration().GetServerStatistics()
return &Server{
config: config,
srv: srv,
checkconnsticker: time.NewTicker(time.Duration(serverstatistics) * time.Second),
Dbsrv: dbsrv,
}
}
func (s *Server) Start() {
go s.checkStatistics()
go s.Dbsrv.Do()
s.srv.Start(s.config.Listener, s.config.AcceptTimeout)
}
func (s *Server) Stop() {
s.srv.Stop()
s.checkconnsticker.Stop()
}
func (s *Server) checkStatistics() {
for {
<-s.checkconnsticker.C
log.Printf("---------------------Totol Connections : %d---------------------\n", NewConns().GetCount())
}
}