-
Notifications
You must be signed in to change notification settings - Fork 5
/
tcplistener.go
133 lines (119 loc) · 3.01 KB
/
tcplistener.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package tnet
import (
"fmt"
"net"
"golang.org/x/sys/unix"
"trpc.group/trpc-go/tnet/internal/cache/systype"
"trpc.group/trpc-go/tnet/internal/iovec"
"trpc.group/trpc-go/tnet/internal/netutil"
"trpc.group/trpc-go/tnet/metrics"
)
type tcpListener struct {
nfd netFD
}
type netError struct {
error
isTimeout bool
}
// Timeout implements net.Error interface.
func (e netError) Timeout() bool {
return e.isTimeout
}
// Temporary implements net.Error interface.
func (e netError) Temporary() bool {
switch e.error {
case unix.EAGAIN, unix.ECONNRESET, unix.ECONNABORTED:
return true
default:
return false
}
}
// Accept implements tcp listener's accept method.
func (t *tcpListener) Accept() (net.Conn, error) {
// TODO: how to support blocking mode
return t.accept(nil)
}
func (t *tcpListener) accept(handle OnTCPOpened) (net.Conn, error) {
fd, sa, err := netutil.Accept(t.FD())
if err != nil {
return nil, netError{error: err}
}
conn := &tcpconn{
nfd: netFD{
fd: fd,
fdtype: fdTCP,
network: t.nfd.network,
laddr: t.nfd.laddr,
raddr: netutil.SockaddrToTCPOrUnixAddr(sa),
},
readTrigger: make(chan struct{}, 1),
}
if !MassiveConnections {
conn.writevData = iovec.NewIOData(iovec.WithLength(systype.MaxLen))
}
conn.inBuffer.Initialize()
conn.outBuffer.Initialize()
if handle != nil {
if err := handle(conn); err != nil {
conn.Close()
return nil, fmt.Errorf("on tcp opened error: %w", err)
}
}
if err := conn.nfd.SetNoDelay(true); err != nil {
return nil, fmt.Errorf("set tcp no delay error: %w", err)
}
if err := conn.nfd.Schedule(tcpOnRead, tcpOnWrite, tcpOnHup, conn); err != nil {
conn.Close()
return nil, fmt.Errorf("connection netfd schedule error: %w", err)
}
metrics.Add(metrics.TCPConnsCreate, 1)
return conn, nil
}
// Close closes the tcp listener.
func (t *tcpListener) Close() error {
t.nfd.close()
return nil
}
// FD returns the tcp listener's file descriptor.
func (t *tcpListener) FD() (fd int) {
return t.nfd.fd
}
// Addr returns the tcp listener's local address.
func (t *tcpListener) Addr() net.Addr {
return t.nfd.laddr
}
func listenTCP(network string, address string) (*tcpListener, error) {
ln, err := net.Listen(network, address)
if err != nil {
return nil, err
}
return newListener(ln)
}
func newListener(listener net.Listener) (*tcpListener, error) {
fd, err := netutil.GetFD(listener)
if err != nil {
return nil, fmt.Errorf("new listener get fd error: %w", err)
}
ln := &tcpListener{
nfd: netFD{
fd: fd,
fdtype: fdListen,
sock: listener,
network: listener.Addr().Network(),
laddr: listener.Addr(),
},
}
return ln, nil
}