github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/buffconn/buffconn.go (about)

     1  package buffconn
     2  
     3  import (
     4  	"bufio"
     5  	"net"
     6  	"time"
     7  )
     8  
     9  // BuffConn is a net.Conn that implements read buffering to save on syscalls.
    10  type BuffConn struct {
    11  	wire      net.Conn
    12  	bufReader *bufio.Reader
    13  }
    14  
    15  // New creates a new BuffConn.
    16  func New(wire net.Conn) *BuffConn {
    17  	return &BuffConn{
    18  		wire:      wire,
    19  		bufReader: bufio.NewReaderSize(wire, 65536),
    20  	}
    21  }
    22  
    23  func (bc *BuffConn) Read(p []byte) (int, error) {
    24  	return bc.bufReader.Read(p)
    25  }
    26  
    27  func (bc *BuffConn) Write(p []byte) (int, error) {
    28  	return bc.wire.Write(p)
    29  }
    30  
    31  func (bc *BuffConn) Close() error {
    32  	return bc.wire.Close()
    33  }
    34  
    35  func (bc *BuffConn) SetDeadline(t time.Time) error {
    36  	return bc.wire.SetDeadline(t)
    37  }
    38  
    39  func (bc *BuffConn) SetReadDeadline(t time.Time) error {
    40  	return bc.wire.SetReadDeadline(t)
    41  }
    42  
    43  func (bc *BuffConn) SetWriteDeadline(t time.Time) error {
    44  	return bc.wire.SetWriteDeadline(t)
    45  }
    46  
    47  func (bc *BuffConn) LocalAddr() net.Addr {
    48  	return bc.wire.LocalAddr()
    49  }
    50  
    51  func (bc *BuffConn) RemoteAddr() net.Addr {
    52  	return bc.wire.RemoteAddr()
    53  }