endpoint.go 1.5 KB

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