forked from zhangpeihao/gortmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
136 lines (124 loc) · 3.5 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
// Copyright 2013, zhangpeihao All rights reserved.
package gortmp
import (
"bufio"
"log"
"net"
"time"
"expvar"
)
type ServerHandler interface {
NewConnection(conn ServerConn, connectReq *Command, server *Server) bool
}
type Server struct {
listener net.Listener
network string
bindAddress string
exit bool
handler ServerHandler
}
// Create a new server.
func NewServer(network string, bindAddress string, handler ServerHandler) (*Server, error) {
server := &Server{
network: network,
bindAddress: bindAddress,
exit: false,
handler: handler,
}
var err error
server.listener, err = net.Listen(server.network, server.bindAddress)
if err != nil {
return nil, err
}
serverConnEstablishedChan := make(chan ServerConn, 50)
serverConnLostChan := make(chan ServerConn, 50)
go func() {
// collect and report all connections
pVar := expvar.NewInt("client.connections.count")
allCons := make([]ServerConn, 0)
for {
select {
case newServerConn := <-serverConnEstablishedChan:
if (newServerConn == nil) {
return
}
allCons = append(allCons, newServerConn)
pVar.Set(int64(len(allCons)))
case serverConnLost := <-serverConnLostChan:
newCons := make([]ServerConn, 0)
for _, conn := range allCons {
if (conn == serverConnLost) {
log.Println("removed 1 lost conn")
continue
}
newCons = append(newCons, conn)
}
allCons = newCons
pVar.Set(int64(len(allCons)))
case <-time.After(time.Duration(1*time.Minute)):
log.Printf("current connection count: %d", len(allCons))
}
}
}()
log.Printf("Start listening on %q...",server.listener.Addr().String())
go server.acceptConnectionLoop(serverConnEstablishedChan, serverConnLostChan)
return server, nil
}
// Close listener.
func (server *Server) Close() {
log.Println("Stop server")
server.exit = true
server.listener.Close()
}
func (server *Server) acceptConnectionLoop(serverConnEstablishedChan chan<- ServerConn, serverConnLostChan chan<- ServerConn) {
for !server.exit {
c, err := server.listener.Accept()
if err != nil {
if server.exit {
break
}
log.Println("SocketServer listener error:", err)
server.rebind()
}
if c != nil {
go server.Handshake(c, serverConnEstablishedChan, serverConnLostChan)
}
}
}
func (server *Server) rebind() {
listener, err := net.Listen(server.network, server.bindAddress)
if err == nil {
server.listener = listener
} else {
time.Sleep(time.Second)
}
}
func (server *Server) Handshake(c net.Conn, serverConnEstablishedChan chan<- ServerConn, serverConnLostChan chan<- ServerConn) {
defer func() {
if r := recover(); r != nil {
err := r.(error)
log.Println("Server::Handshake panic error:", err)
}
}()
log.Printf("[%s]SHandshake beginning with client", c.RemoteAddr().String())
br := bufio.NewReader(c)
bw := bufio.NewWriter(c)
timeout := time.Duration(10) * time.Second
if err := SHandshake(c, br, bw, timeout); err != nil {
log.Printf("[%s]SHandshake error: %s", c.RemoteAddr().String(), err.Error())
c.Close()
return
}
// New client connects to server
serverConn, err := NewServerConn(c, br, bw, server, 100, serverConnLostChan)
if err != nil {
log.Printf("[%s]NewServerConn error: %s", c.RemoteAddr().String(), err.Error())
c.Close()
return
}
serverConnEstablishedChan <-serverConn
}
// On received connect request
func (server *Server) OnConnectAuth(conn ServerConn, connectReq *Command) bool {
return server.handler.NewConnection(conn, connectReq, server)
}