/
tcp_service.go
106 lines (97 loc) · 3.36 KB
/
tcp_service.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
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* rpc/tcp_service.go *
* *
* hprose tcp service for Go. *
* *
* LastModified: Apr 13, 2018 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package rpc
import (
"crypto/tls"
"net"
"time"
)
// TCPService is the hprose tcp service
type TCPService struct {
SocketService
Linger int
NoDelay bool
KeepAlive bool
KeepAlivePeriod time.Duration
}
// NewTCPService is the constructor of TCPService
func NewTCPService() (service *TCPService) {
service = new(TCPService)
service.initTCPService()
return service
}
func (service *TCPService) initTCPService() {
service.initSocketService()
service.Linger = -1
service.NoDelay = true
service.KeepAlive = true
service.KeepAlivePeriod = 0
}
// ServeTCPConn runs on a single tcp connection. ServeTCPConn blocks, serving
// the connection until the client hangs up. The caller typically invokes
// ServeTCPConn in a go statement.
func (service *TCPService) ServeTCPConn(conn *net.TCPConn) {
conn.SetLinger(service.Linger)
conn.SetNoDelay(service.NoDelay)
conn.SetKeepAlive(service.KeepAlive)
if service.KeepAlivePeriod > 0 {
conn.SetKeepAlivePeriod(service.KeepAlivePeriod)
}
if service.TLSConfig != nil {
tlsConn := tls.Server(conn, service.TLSConfig)
tlsConn.Handshake()
service.serveConn(tlsConn)
} else {
service.serveConn(conn)
}
}
// ServeConn runs on a single net connection. ServeConn blocks, serving the
// connection until the client hangs up. The caller typically invokes
// ServeConn in a go statement.
func (service *TCPService) ServeConn(conn net.Conn) {
service.ServeTCPConn(conn.(*net.TCPConn))
}
// ServeTCP runs on the TCPListener. ServeTCP blocks, serving the listener
// until the server is stop. The caller typically invokes ServeTCP in a go
// statement.
func (service *TCPService) ServeTCP(listener *net.TCPListener) {
service.workerPool.Start();
defer service.workerPool.Stop();
var tempDelay time.Duration // how long to sleep on accept failure
for {
conn, err := listener.AcceptTCP()
if err != nil {
tempDelay = nextTempDelay(err, service.Event, tempDelay)
if tempDelay > 0 {
continue
}
return
}
tempDelay = 0
service.workerPool.Go(func() {
service.ServeTCPConn(conn)
});
}
}
// Serve runs on the Listener. Serve blocks, serving the listener
// until the server is stop. The caller typically invokes Serve in a go
// statement.
func (service *TCPService) Serve(listener net.Listener) {
service.ServeTCP(listener.(*net.TCPListener))
}