github.com/pion/webrtc/v3@v3.2.24/internal/mux/endpoint.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  package mux
     5  
     6  import (
     7  	"errors"
     8  	"io"
     9  	"net"
    10  	"time"
    11  
    12  	"github.com/pion/ice/v2"
    13  	"github.com/pion/transport/v2/packetio"
    14  )
    15  
    16  // Endpoint implements net.Conn. It is used to read muxed packets.
    17  type Endpoint struct {
    18  	mux    *Mux
    19  	buffer *packetio.Buffer
    20  }
    21  
    22  // Close unregisters the endpoint from the Mux
    23  func (e *Endpoint) Close() (err error) {
    24  	err = e.close()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	e.mux.RemoveEndpoint(e)
    30  	return nil
    31  }
    32  
    33  func (e *Endpoint) close() error {
    34  	return e.buffer.Close()
    35  }
    36  
    37  // Read reads a packet of len(p) bytes from the underlying conn
    38  // that are matched by the associated MuxFunc
    39  func (e *Endpoint) Read(p []byte) (int, error) {
    40  	return e.buffer.Read(p)
    41  }
    42  
    43  // Write writes len(p) bytes to the underlying conn
    44  func (e *Endpoint) Write(p []byte) (int, error) {
    45  	n, err := e.mux.nextConn.Write(p)
    46  	if errors.Is(err, ice.ErrNoCandidatePairs) {
    47  		return 0, nil
    48  	} else if errors.Is(err, ice.ErrClosed) {
    49  		return 0, io.ErrClosedPipe
    50  	}
    51  
    52  	return n, err
    53  }
    54  
    55  // LocalAddr is a stub
    56  func (e *Endpoint) LocalAddr() net.Addr {
    57  	return e.mux.nextConn.LocalAddr()
    58  }
    59  
    60  // RemoteAddr is a stub
    61  func (e *Endpoint) RemoteAddr() net.Addr {
    62  	return e.mux.nextConn.RemoteAddr()
    63  }
    64  
    65  // SetDeadline is a stub
    66  func (e *Endpoint) SetDeadline(time.Time) error {
    67  	return nil
    68  }
    69  
    70  // SetReadDeadline is a stub
    71  func (e *Endpoint) SetReadDeadline(time.Time) error {
    72  	return nil
    73  }
    74  
    75  // SetWriteDeadline is a stub
    76  func (e *Endpoint) SetWriteDeadline(time.Time) error {
    77  	return nil
    78  }