Skip to content

Commit

Permalink
Document all public netctx methods
Browse files Browse the repository at this point in the history
Adds documentation for all public netctx methods.

Signed-off-by: Daniel Mangum <[email protected]>
  • Loading branch information
hasheddan committed Jul 11, 2023
1 parent 9511282 commit 742099f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions netctx/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func NewConn(netConn net.Conn) Conn {
return c
}

// ReadContext reads data from the connection.
// Unlike net.Conn.Read(), the provided context is used to control timeout.
func (c *conn) ReadContext(ctx context.Context, b []byte) (int, error) {
c.readMu.Lock()
defer c.readMu.Unlock()
Expand Down Expand Up @@ -106,6 +108,8 @@ func (c *conn) ReadContext(ctx context.Context, b []byte) (int, error) {
return n, err
}

// WriteContext writes data to the connection.
// Unlike net.Conn.Write(), the provided context is used to control timeout.
func (c *conn) WriteContext(ctx context.Context, b []byte) (int, error) {
c.writeMu.Lock()
defer c.writeMu.Unlock()
Expand Down Expand Up @@ -150,6 +154,9 @@ func (c *conn) WriteContext(ctx context.Context, b []byte) (int, error) {
return n, err
}

// Close closes the connection.
// Any blocked ReadContext or WriteContext operations will be unblocked and
// return errors.
func (c *conn) Close() error {
err := c.nextConn.Close()
c.closeOnce.Do(func() {
Expand All @@ -162,14 +169,17 @@ func (c *conn) Close() error {
return err
}

// LocalAddr returns the local network address, if known.
func (c *conn) LocalAddr() net.Addr {
return c.nextConn.LocalAddr()
}

// LocalAddr returns the local network address, if known.
func (c *conn) RemoteAddr() net.Addr {
return c.nextConn.RemoteAddr()
}

// Conn returns the underlying net.Conn.
func (c *conn) Conn() net.Conn {
return c.nextConn
}
18 changes: 18 additions & 0 deletions netctx/packetconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ func NewPacketConn(pconn net.PacketConn) PacketConn {
return p
}

// ReadFromContext reads a packet from the connection,
// copying the payload into p. It returns the number of
// bytes copied into p and the return address that
// was on the packet.
// It returns the number of bytes read (0 <= n <= len(p))
// and any error encountered. Callers should always process
// the n > 0 bytes returned before considering the error err.
// Unlike net.PacketConn.ReadFrom(), the provided context is
// used to control timeout.
func (p *packetConn) ReadFromContext(ctx context.Context, b []byte) (int, net.Addr, error) {
p.readMu.Lock()
defer p.readMu.Unlock()
Expand Down Expand Up @@ -92,6 +101,10 @@ func (p *packetConn) ReadFromContext(ctx context.Context, b []byte) (int, net.Ad
return n, raddr, err
}

// WriteToContext writes a packet with payload p to addr.
// Unlike net.PacketConn.WriteTo(), the provided context
// is used to control timeout.
// On packet-oriented connections, write timeouts are rare.
func (p *packetConn) WriteToContext(ctx context.Context, b []byte, raddr net.Addr) (int, error) {
p.writeMu.Lock()
defer p.writeMu.Unlock()
Expand Down Expand Up @@ -136,6 +149,9 @@ func (p *packetConn) WriteToContext(ctx context.Context, b []byte, raddr net.Add
return n, err
}

// Close closes the connection.
// Any blocked ReadFromContext or WriteToContext operations will be unblocked
// and return errors.
func (p *packetConn) Close() error {
err := p.nextConn.Close()
p.closeOnce.Do(func() {
Expand All @@ -148,10 +164,12 @@ func (p *packetConn) Close() error {
return err
}

// LocalAddr returns the local network address, if known.
func (p *packetConn) LocalAddr() net.Addr {
return p.nextConn.LocalAddr()
}

// Conn returns the underlying net.PacketConn.
func (p *packetConn) Conn() net.PacketConn {
return p.nextConn
}

0 comments on commit 742099f

Please sign in to comment.