// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT

package mux

import (
	
	
	
	

	
	
)

// Endpoint implements net.Conn. It is used to read muxed packets.
type Endpoint struct {
	mux     *Mux
	buffer  *packetio.Buffer
	onClose func()
}

// Close unregisters the endpoint from the Mux.
func ( *Endpoint) () ( error) {
	if .onClose != nil {
		.onClose()
	}

	if  = .close();  != nil {
		return 
	}

	.mux.RemoveEndpoint()

	return nil
}

func ( *Endpoint) () error {
	return .buffer.Close()
}

// Read reads a packet of len(p) bytes from the underlying conn
// that are matched by the associated MuxFunc.
func ( *Endpoint) ( []byte) (int, error) {
	return .buffer.Read()
}

// ReadFrom reads a packet of len(p) bytes from the underlying conn
// that are matched by the associated MuxFunc.
func ( *Endpoint) ( []byte) (int, net.Addr, error) {
	,  := .Read()

	return , nil, 
}

// Write writes len(p) bytes to the underlying conn.
func ( *Endpoint) ( []byte) (int, error) {
	,  := .mux.nextConn.Write()
	if errors.Is(, ice.ErrNoCandidatePairs) {
		return 0, nil
	} else if errors.Is(, ice.ErrClosed) {
		return 0, io.ErrClosedPipe
	}

	return , 
}

// WriteTo writes len(p) bytes to the underlying conn.
func ( *Endpoint) ( []byte,  net.Addr) (int, error) {
	return .Write()
}

// LocalAddr is a stub.
func ( *Endpoint) () net.Addr {
	return .mux.nextConn.LocalAddr()
}

// RemoteAddr is a stub.
func ( *Endpoint) () net.Addr {
	return .mux.nextConn.RemoteAddr()
}

// SetDeadline is a stub.
func ( *Endpoint) (time.Time) error {
	return nil
}

// SetReadDeadline is a stub.
func ( *Endpoint) (time.Time) error {
	return nil
}

// SetWriteDeadline is a stub.
func ( *Endpoint) (time.Time) error {
	return nil
}

// SetOnClose is a user set callback that
// will be executed when `Close` is called.
func ( *Endpoint) ( func()) {
	.onClose = 
}