...
Run Format

Source file src/net/rawconn.go

Documentation: net

     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(tmm1): On Windows, the Write method of syscall.RawConn
    13  // does not integrate with the runtime's network poller. It cannot
    14  // wait for the connection to become writeable, and does not respect
    15  // deadlines. If the user-provided callback returns false, the Write
    16  // method will fail immediately.
    17  
    18  // BUG(mikio): On JS, NaCl and Plan 9, the Control, Read and Write
    19  // methods of syscall.RawConn are not implemented.
    20  
    21  type rawConn struct {
    22  	fd *netFD
    23  }
    24  
    25  func (c *rawConn) ok() bool { return c != nil && c.fd != nil }
    26  
    27  func (c *rawConn) Control(f func(uintptr)) error {
    28  	if !c.ok() {
    29  		return syscall.EINVAL
    30  	}
    31  	err := c.fd.pfd.RawControl(f)
    32  	runtime.KeepAlive(c.fd)
    33  	if err != nil {
    34  		err = &OpError{Op: "raw-control", Net: c.fd.net, Source: nil, Addr: c.fd.laddr, Err: err}
    35  	}
    36  	return err
    37  }
    38  
    39  func (c *rawConn) Read(f func(uintptr) bool) error {
    40  	if !c.ok() {
    41  		return syscall.EINVAL
    42  	}
    43  	err := c.fd.pfd.RawRead(f)
    44  	runtime.KeepAlive(c.fd)
    45  	if err != nil {
    46  		err = &OpError{Op: "raw-read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
    47  	}
    48  	return err
    49  }
    50  
    51  func (c *rawConn) Write(f func(uintptr) bool) error {
    52  	if !c.ok() {
    53  		return syscall.EINVAL
    54  	}
    55  	err := c.fd.pfd.RawWrite(f)
    56  	runtime.KeepAlive(c.fd)
    57  	if err != nil {
    58  		err = &OpError{Op: "raw-write", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
    59  	}
    60  	return err
    61  }
    62  
    63  func newRawConn(fd *netFD) (*rawConn, error) {
    64  	return &rawConn{fd: fd}, nil
    65  }
    66  
    67  type rawListener struct {
    68  	rawConn
    69  }
    70  
    71  func (l *rawListener) Read(func(uintptr) bool) error {
    72  	return syscall.EINVAL
    73  }
    74  
    75  func (l *rawListener) Write(func(uintptr) bool) error {
    76  	return syscall.EINVAL
    77  }
    78  
    79  func newRawListener(fd *netFD) (*rawListener, error) {
    80  	return &rawListener{rawConn{fd: fd}}, nil
    81  }
    82  

View as plain text