forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listener_windows.go
135 lines (123 loc) · 3.35 KB
/
listener_windows.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
134
135
// Copyright (c) 2023 The Gnet Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gnet
import (
"context"
"errors"
"net"
"os"
"sync"
"syscall"
"golang.org/x/sys/windows"
errorx "github.com/panjf2000/gnet/v2/pkg/errors"
"github.com/panjf2000/gnet/v2/pkg/logging"
)
type listener struct {
network string
address string
once sync.Once
ln net.Listener
pc net.PacketConn
addr net.Addr
}
func (l *listener) dup() (int, string, error) {
if l.ln == nil && l.pc == nil {
return -1, "dup", errorx.ErrUnsupportedOp
}
var (
sc syscall.Conn
ok bool
)
if l.ln != nil {
sc, ok = l.ln.(syscall.Conn)
} else {
sc, ok = l.pc.(syscall.Conn)
}
if !ok {
return -1, "dup", errors.New("failed to convert net.Conn to syscall.Conn")
}
rc, err := sc.SyscallConn()
if err != nil {
return -1, "dup", errors.New("failed to get syscall.RawConn from net.Conn")
}
var dupHandle windows.Handle
e := rc.Control(func(fd uintptr) {
process := windows.CurrentProcess()
err = windows.DuplicateHandle(
process,
windows.Handle(fd),
process,
&dupHandle,
0,
true,
windows.DUPLICATE_SAME_ACCESS,
)
})
if err != nil {
return -1, "dup", err
}
if e != nil {
return -1, "dup", e
}
return int(dupHandle), "dup", nil
}
func (l *listener) close() {
l.once.Do(func() {
if l.pc != nil {
logging.Error(os.NewSyscallError("close", l.pc.Close()))
return
}
logging.Error(os.NewSyscallError("close", l.ln.Close()))
})
}
func initListener(network, addr string, options *Options) (l *listener, err error) {
lc := net.ListenConfig{
Control: func(network, address string, c syscall.RawConn) error {
return c.Control(func(fd uintptr) {
if network != "unix" && (options.ReuseAddr || options.ReusePort) {
_ = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_REUSEADDR, 1)
}
if options.TCPNoDelay == TCPNoDelay {
_ = windows.SetsockoptInt(windows.Handle(fd), windows.IPPROTO_TCP, windows.TCP_NODELAY, 1)
}
if options.SocketRecvBuffer > 0 {
_ = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_RCVBUF, options.SocketRecvBuffer)
}
if options.SocketSendBuffer > 0 {
_ = windows.SetsockoptInt(windows.Handle(fd), windows.SOL_SOCKET, windows.SO_SNDBUF, options.SocketSendBuffer)
}
})
},
KeepAlive: options.TCPKeepAlive,
}
l = &listener{network: network, address: addr}
switch network {
case "udp", "udp4", "udp6":
if l.pc, err = lc.ListenPacket(context.Background(), network, addr); err != nil {
return nil, err
}
l.addr = l.pc.LocalAddr()
case "unix":
_ = os.Remove(addr)
fallthrough
case "tcp", "tcp4", "tcp6":
if l.ln, err = lc.Listen(context.Background(), network, addr); err != nil {
return nil, err
}
l.addr = l.ln.Addr()
default:
err = errorx.ErrUnsupportedProtocol
}
return
}