package quicreuse

import (
	
	
	
)

// nonQUICPacketConn is a net.PacketConn that can be used to read and write
// non-QUIC packets on a quic.Transport. This lets us reuse this UDP port for
// other transports like WebRTC.
type nonQUICPacketConn struct {
	owningTransport RefCountedQUICTransport
	tr              QUICTransport
	ctx             context.Context
	ctxCancel       context.CancelFunc
	readCtx         context.Context
	readCancel      context.CancelFunc
}

// Close implements net.PacketConn.
func ( *nonQUICPacketConn) () error {
	.ctxCancel()

	// Don't actually close the underlying transport since someone else might be using it.
	// reuse has it's own gc to close unused transports.
	.owningTransport.DecreaseCount()
	return nil
}

// LocalAddr implements net.PacketConn.
func ( *nonQUICPacketConn) () net.Addr {
	return .owningTransport.LocalAddr()
}

// ReadFrom implements net.PacketConn.
func ( *nonQUICPacketConn) ( []byte) (int, net.Addr, error) {
	 := .readCtx
	if  == nil {
		 = .ctx
	}
	return .tr.ReadNonQUICPacket(, )
}

// SetDeadline implements net.PacketConn.
func ( *nonQUICPacketConn) ( time.Time) error {
	// Only used for reads.
	return .SetReadDeadline()
}

// SetReadDeadline implements net.PacketConn.
func ( *nonQUICPacketConn) ( time.Time) error {
	if .IsZero() && .readCtx != nil {
		.readCancel()
		.readCtx = nil
	}
	.readCtx, .readCancel = context.WithDeadline(.ctx, )
	return nil
}

// SetWriteDeadline implements net.PacketConn.
func ( *nonQUICPacketConn) ( time.Time) error {
	// Unused. quic-go doesn't support deadlines for writes.
	return nil
}

// WriteTo implements net.PacketConn.
func ( *nonQUICPacketConn) ( []byte,  net.Addr) (int, error) {
	return .tr.WriteTo(, )
}

var _ net.PacketConn = &nonQUICPacketConn{}