cubefs
60 строк · 1.8 Кб
1// +build !appengine
2
3/*
4*
5* Copyright 2018 gRPC authors.
6*
7* Licensed under the Apache License, Version 2.0 (the "License");
8* you may not use this file except in compliance with the License.
9* You may obtain a copy of the License at
10*
11* http://www.apache.org/licenses/LICENSE-2.0
12*
13* Unless required by applicable law or agreed to in writing, software
14* distributed under the License is distributed on an "AS IS" BASIS,
15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16* See the License for the specific language governing permissions and
17* limitations under the License.
18*
19*/
20
21package credentials
22
23import (
24"net"
25"syscall"
26)
27
28type sysConn = syscall.Conn
29
30// syscallConn keeps reference of rawConn to support syscall.Conn for channelz.
31// SyscallConn() (the method in interface syscall.Conn) is explicitly
32// implemented on this type,
33//
34// Interface syscall.Conn is implemented by most net.Conn implementations (e.g.
35// TCPConn, UnixConn), but is not part of net.Conn interface. So wrapper conns
36// that embed net.Conn don't implement syscall.Conn. (Side note: tls.Conn
37// doesn't embed net.Conn, so even if syscall.Conn is part of net.Conn, it won't
38// help here).
39type syscallConn struct {
40net.Conn
41// sysConn is a type alias of syscall.Conn. It's necessary because the name
42// `Conn` collides with `net.Conn`.
43sysConn
44}
45
46// WrapSyscallConn tries to wrap rawConn and newConn into a net.Conn that
47// implements syscall.Conn. rawConn will be used to support syscall, and newConn
48// will be used for read/write.
49//
50// This function returns newConn if rawConn doesn't implement syscall.Conn.
51func WrapSyscallConn(rawConn, newConn net.Conn) net.Conn {
52sysConn, ok := rawConn.(syscall.Conn)
53if !ok {
54return newConn
55}
56return &syscallConn{
57Conn: newConn,
58sysConn: sysConn,
59}
60}
61