github.com/Psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/quic/gquic-go/conn.go (about)

     1  package gquic
     2  
     3  import (
     4  	"net"
     5  	"sync"
     6  )
     7  
     8  type connection interface {
     9  	Write([]byte) error
    10  	Read([]byte) (int, net.Addr, error)
    11  	Close() error
    12  	LocalAddr() net.Addr
    13  	RemoteAddr() net.Addr
    14  	SetCurrentRemoteAddr(net.Addr)
    15  }
    16  
    17  type conn struct {
    18  	mutex sync.RWMutex
    19  
    20  	pconn       net.PacketConn
    21  	currentAddr net.Addr
    22  }
    23  
    24  var _ connection = &conn{}
    25  
    26  func (c *conn) Write(p []byte) error {
    27  	_, err := c.pconn.WriteTo(p, c.currentAddr)
    28  	return err
    29  }
    30  
    31  func (c *conn) Read(p []byte) (int, net.Addr, error) {
    32  	return c.pconn.ReadFrom(p)
    33  }
    34  
    35  func (c *conn) SetCurrentRemoteAddr(addr net.Addr) {
    36  	c.mutex.Lock()
    37  	c.currentAddr = addr
    38  	c.mutex.Unlock()
    39  }
    40  
    41  func (c *conn) LocalAddr() net.Addr {
    42  	return c.pconn.LocalAddr()
    43  }
    44  
    45  func (c *conn) RemoteAddr() net.Addr {
    46  	c.mutex.RLock()
    47  	addr := c.currentAddr
    48  	c.mutex.RUnlock()
    49  	return addr
    50  }
    51  
    52  func (c *conn) Close() error {
    53  	return c.pconn.Close()
    54  }