-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
244 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package quicreuse | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"time" | ||
|
||
"github.com/quic-go/quic-go" | ||
) | ||
|
||
// 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 *quic.Transport | ||
ctx context.Context | ||
ctxCancel context.CancelFunc | ||
readCtx context.Context | ||
readCancel context.CancelFunc | ||
} | ||
|
||
// Close implements net.PacketConn. | ||
func (n *nonQUICPacketConn) Close() error { | ||
n.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. | ||
n.owningTransport.DecreaseCount() | ||
return nil | ||
} | ||
|
||
// LocalAddr implements net.PacketConn. | ||
func (n *nonQUICPacketConn) LocalAddr() net.Addr { | ||
return n.tr.Conn.LocalAddr() | ||
} | ||
|
||
// ReadFrom implements net.PacketConn. | ||
func (n *nonQUICPacketConn) ReadFrom(p []byte) (int, net.Addr, error) { | ||
ctx := n.readCtx | ||
if ctx == nil { | ||
ctx = n.ctx | ||
} | ||
return n.tr.ReadNonQUICPacket(ctx, p) | ||
} | ||
|
||
// SetDeadline implements net.PacketConn. | ||
func (n *nonQUICPacketConn) SetDeadline(t time.Time) error { | ||
// Only used for reads. | ||
return n.SetReadDeadline(t) | ||
} | ||
|
||
// SetReadDeadline implements net.PacketConn. | ||
func (n *nonQUICPacketConn) SetReadDeadline(t time.Time) error { | ||
if t.IsZero() && n.readCtx != nil { | ||
n.readCancel() | ||
n.readCtx = nil | ||
} | ||
n.readCtx, n.readCancel = context.WithDeadline(n.ctx, t) | ||
return nil | ||
} | ||
|
||
// SetWriteDeadline implements net.PacketConn. | ||
func (n *nonQUICPacketConn) SetWriteDeadline(t time.Time) error { | ||
// Unused. quic-go doesn't support deadlines for writes. | ||
return nil | ||
} | ||
|
||
// WriteTo implements net.PacketConn. | ||
func (n *nonQUICPacketConn) WriteTo(p []byte, addr net.Addr) (int, error) { | ||
return n.tr.WriteTo(p, addr) | ||
} | ||
|
||
var _ net.PacketConn = &nonQUICPacketConn{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters