-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlistener-tcp.go
49 lines (39 loc) · 875 Bytes
/
listener-tcp.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
package syslog5424 // import "github.com/nathanaelle/syslog5424/v2"
import (
"net"
)
type (
tcpReceiver struct {
listener *net.TCPListener
}
)
// TCPListener create a TCP Listener
func TCPListener(network, address string) (Listener, error) {
var err error
if network != "tcp" && network != "tcp4" && network != "tcp6" {
return nil, ErrInvalidNetwork
}
laddr, err := net.ResolveTCPAddr(network, address)
if err != nil {
return nil, err
}
r := new(tcpReceiver)
r.listener, err = net.ListenTCP(network, laddr)
if err != nil {
return nil, err
}
return r, nil
}
func (r *tcpReceiver) Accept() (DataReader, error) {
conn, err := r.listener.AcceptTCP()
if err != nil {
return nil, err
}
conn.SetLinger(-1)
conn.CloseWrite()
conn.SetReadBuffer(readBuffer)
return conn, nil
}
func (r *tcpReceiver) Close() error {
return r.listener.Close()
}