-
Notifications
You must be signed in to change notification settings - Fork 1
/
listener-unix.go
63 lines (50 loc) · 1.13 KB
/
listener-unix.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
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"
import (
"net"
"os"
)
type (
unixReceiver struct {
listener *net.UnixListener
}
)
// UnixListener create a UNIX Listener
// careful : a previous unused socket may be removed and recreated
func UnixListener(address string) (Listener, error) {
var err error
r := new(unixReceiver)
uAddr, err := net.ResolveUnixAddr("unix", address)
if err != nil {
return nil, err
}
r.listener, err = net.ListenUnix("unix", uAddr)
for err != nil {
switch err.(type) {
case *net.OpError:
if err.(*net.OpError).Err.Error() != "bind: address already in use" {
return nil, err
}
default:
return nil, err
}
if _, osErr := os.Stat(address); osErr != nil {
return nil, err
}
os.Remove(address)
r.listener, err = net.ListenUnix("unix", uAddr)
}
return r, nil
}
func (r *unixReceiver) Accept() (DataReader, error) {
conn, err := r.listener.AcceptUnix()
if err != nil {
return nil, err
}
conn.SetWriteBuffer(0)
conn.CloseWrite()
conn.SetReadBuffer(readBuffer)
return conn, nil
}
func (r *unixReceiver) Close() error {
return r.listener.Close()
}