|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +// Go MySQL Driver - A MySQL-Driver for Go's database/sql package |
| 19 | +// |
| 20 | +// Copyright 2019 The Go-MySQL-Driver Authors. All rights reserved. |
| 21 | +// |
| 22 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 23 | +// License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 24 | +// You can obtain one at http://mozilla.org/MPL/2.0/. |
| 25 | + |
| 26 | +// +build linux darwin dragonfly freebsd netbsd openbsd solaris illumos |
| 27 | + |
| 28 | +package gxnet |
| 29 | + |
| 30 | +import ( |
| 31 | + "errors" |
| 32 | + "io" |
| 33 | + "net" |
| 34 | + "syscall" |
| 35 | +) |
| 36 | + |
| 37 | +var errUnexpectedRead = errors.New("unexpected read from socket") |
| 38 | + |
| 39 | +func connCheck(conn net.Conn) error { |
| 40 | + var sysErr error |
| 41 | + |
| 42 | + sysConn, ok := conn.(syscall.Conn) |
| 43 | + if !ok { |
| 44 | + return nil |
| 45 | + } |
| 46 | + rawConn, err := sysConn.SyscallConn() |
| 47 | + if err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + err = rawConn.Read(func(fd uintptr) bool { |
| 52 | + var buf [1]byte |
| 53 | + n, err := syscall.Read(int(fd), buf[:]) |
| 54 | + switch { |
| 55 | + case n == 0 && err == nil: |
| 56 | + sysErr = io.EOF |
| 57 | + case n > 0: |
| 58 | + sysErr = errUnexpectedRead |
| 59 | + case err == syscall.EAGAIN || err == syscall.EWOULDBLOCK: |
| 60 | + sysErr = nil |
| 61 | + default: |
| 62 | + sysErr = err |
| 63 | + } |
| 64 | + return true |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + return sysErr |
| 71 | +} |
| 72 | + |
0 commit comments