github.com/hlts2/go@v0.0.0-20170904000733-812b34efaed8/src/net/rawconn.go (about)

     1  // Copyright 2017 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"runtime"
     9  	"syscall"
    10  )
    11  
    12  // BUG(mikio): On Windows, the Read and Write methods of
    13  // syscall.RawConn are not implemented.
    14  
    15  // BUG(mikio): On NaCl and Plan 9, the Control, Read and Write methods
    16  // of syscall.RawConn are not implemented.
    17  
    18  type rawConn struct {
    19  	fd *netFD
    20  }
    21  
    22  func (c *rawConn) ok() bool { return c != nil && c.fd != nil }
    23  
    24  func (c *rawConn) Control(f func(uintptr)) error {
    25  	if !c.ok() {
    26  		return syscall.EINVAL
    27  	}
    28  	err := c.fd.pfd.RawControl(f)
    29  	runtime.KeepAlive(c.fd)
    30  	if err != nil {
    31  		err = &OpError{Op: "raw-control", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
    32  	}
    33  	return err
    34  }
    35  
    36  func (c *rawConn) Read(f func(uintptr) bool) error {
    37  	if !c.ok() {
    38  		return syscall.EINVAL
    39  	}
    40  	err := c.fd.pfd.RawRead(f)
    41  	runtime.KeepAlive(c.fd)
    42  	if err != nil {
    43  		err = &OpError{Op: "raw-read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
    44  	}
    45  	return err
    46  }
    47  
    48  func (c *rawConn) Write(f func(uintptr) bool) error {
    49  	if !c.ok() {
    50  		return syscall.EINVAL
    51  	}
    52  	err := c.fd.pfd.RawWrite(f)
    53  	runtime.KeepAlive(c.fd)
    54  	if err != nil {
    55  		err = &OpError{Op: "raw-write", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
    56  	}
    57  	return err
    58  }
    59  
    60  func newRawConn(fd *netFD) (*rawConn, error) {
    61  	return &rawConn{fd: fd}, nil
    62  }