-
Notifications
You must be signed in to change notification settings - Fork 1
/
connector-local.go
54 lines (42 loc) · 1.13 KB
/
connector-local.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
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"
import (
"net"
)
type (
localConn struct {
network, address string
}
unixgram struct {
addr *Addr
c *net.UnixConn
}
)
// LocalConnector is a dialer that forward to a local RFC5424 syslog receiver
func LocalConnector(network, address string) Connector {
return &localConn{network, address}
}
// Connect is part of implementation of (Connector interface)[#Connector]
func (c *localConn) Connect() (WriteCloser, error) {
if c.address != "" && c.network != "" {
return c.localWriteCloser(net.DialUnix(c.network, nil, &net.UnixAddr{Name: c.address, Net: c.network}))
}
return c.localWriteCloser(c.osGuessConnnector())
}
func (c *localConn) localWriteCloser(conn *net.UnixConn, err error) (WriteCloser, error) {
if err != nil {
return nil, err
}
conn.SetReadBuffer(0)
conn.CloseRead()
if c.network == "unixgram" {
// return conn, nil
return unixgram{&Addr{c.network, c.address}, conn}, nil
}
return conn, nil
}
func (c unixgram) Close() error {
return c.c.Close()
}
func (c unixgram) Write(d []byte) (n int, err error) {
return c.c.Write(d)
}