-
Notifications
You must be signed in to change notification settings - Fork 204
/
unix_service.go
88 lines (80 loc) · 2.94 KB
/
unix_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
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* rpc/unix_service.go *
* *
* hprose unix service for Go. *
* *
* LastModified: Apr 13, 2018 *
* Author: Ma Bingyao <andot@hprose.com> *
* *
\**********************************************************/
package rpc
import (
"crypto/tls"
"net"
"time"
)
// UnixService is the hprose unix service
type UnixService struct {
SocketService
}
// NewUnixService is the constructor of UnixService
func NewUnixService() (service *UnixService) {
service = new(UnixService)
service.initSocketService()
return service
}
// ServeUnixConn runs on a single tcp connection. ServeUnixConn blocks, serving
// the connection until the client hangs up. The caller typically invokes
// ServeUnixConn in a go statement.
func (service *UnixService) ServeUnixConn(conn *net.UnixConn) {
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 *UnixService) ServeConn(conn net.Conn) {
service.ServeUnixConn(conn.(*net.UnixConn))
}
// ServeUnix runs on the UnixListener. ServeUnix blocks, serving the listener
// until the server is stop. The caller typically invokes ServeUnix in a go
// statement.
func (service *UnixService) ServeUnix(listener *net.UnixListener) {
service.workerPool.Start();
defer service.workerPool.Stop();
var tempDelay time.Duration // how long to sleep on accept failure
for {
conn, err := listener.AcceptUnix()
if err != nil {
tempDelay = nextTempDelay(err, service.Event, tempDelay)
if tempDelay > 0 {
continue
}
return
}
tempDelay = 0
service.workerPool.Go(func() {
service.ServeUnixConn(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 *UnixService) Serve(listener net.Listener) {
service.ServeUnix(listener.(*net.UnixListener))
}