-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduces a new net package, which defines networking interfaces used throughout the pion/dtls code base, as well as utilities to help consumers convert standard library net types into pion/dtls compatible ones. Signed-off-by: Daniel Mangum <georgedanielmangum@gmail.com>
- Loading branch information
Showing
2 changed files
with
108 additions
and
57 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,108 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package net defines packet-oriented primitives that are compatible with net | ||
// in the standard library. | ||
package net | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
// A PacketListener is the same as net.Listener but returns a net.PacketConn on | ||
// Accept() rather than a net.Conn. | ||
// | ||
// Multiple goroutines may invoke methods on a PacketListener simultaneously. | ||
type PacketListener interface { | ||
// Accept waits for and returns the next connection to the listener. | ||
Accept() (net.PacketConn, net.Addr, error) | ||
|
||
// Close closes the listener. | ||
// Any blocked Accept operations will be unblocked and return errors. | ||
Close() error | ||
|
||
// Addr returns the listener's network address. | ||
Addr() net.Addr | ||
} | ||
|
||
// PacketListenerFromListener converts a net.Listener into a | ||
// dtlsnet.PacketListener. | ||
func PacketListenerFromListener(l net.Listener) PacketListener { | ||
return &packetListenerWrapper{ | ||
l: l, | ||
} | ||
} | ||
|
||
// packetListenerWrapper wraps a net.Listener and implements | ||
// dtlsnet.PacketListener. | ||
type packetListenerWrapper struct { | ||
l net.Listener | ||
} | ||
|
||
// Accept calls Accept on the underlying net.Listener and converts the returned | ||
// net.Conn into a net.PacketConn. | ||
func (p *packetListenerWrapper) Accept() (net.PacketConn, net.Addr, error) { | ||
c, err := p.l.Accept() | ||
if err != nil { | ||
return PacketConnFromConn(c), nil, err | ||
} | ||
return PacketConnFromConn(c), c.RemoteAddr(), nil | ||
} | ||
|
||
// Close closes the underlying net.Listener. | ||
func (p *packetListenerWrapper) Close() error { | ||
return p.l.Close() | ||
} | ||
|
||
// Addr returns the address of the underlying net.Listener. | ||
func (p *packetListenerWrapper) Addr() net.Addr { | ||
return p.l.Addr() | ||
} | ||
|
||
// PacketConnFromConn converts a net.Conn into a net.PacketConn. | ||
func PacketConnFromConn(conn net.Conn) net.PacketConn { | ||
return &packetConnWrapper{conn} | ||
} | ||
|
||
// packetConnWrapper wraps a net.Conn and implements net.PacketConn. | ||
type packetConnWrapper struct { | ||
conn net.Conn | ||
} | ||
|
||
// ReadFrom reads from the underlying net.Conn and returns its remote address. | ||
func (p *packetConnWrapper) ReadFrom(b []byte) (int, net.Addr, error) { | ||
n, err := p.conn.Read(b) | ||
return n, p.conn.RemoteAddr(), err | ||
} | ||
|
||
// WriteTo writes to the underlying net.Conn. | ||
func (p *packetConnWrapper) WriteTo(b []byte, _ net.Addr) (int, error) { | ||
n, err := p.conn.Write(b) | ||
return n, err | ||
} | ||
|
||
// Close closes the underlying net.Conn. | ||
func (p *packetConnWrapper) Close() error { | ||
return p.conn.Close() | ||
} | ||
|
||
// LocalAddr returns the local address of the underlying net.Conn. | ||
func (p *packetConnWrapper) LocalAddr() net.Addr { | ||
return p.conn.LocalAddr() | ||
} | ||
|
||
// SetDeadline sets the deadline on the underlying net.Conn. | ||
func (p *packetConnWrapper) SetDeadline(t time.Time) error { | ||
return p.conn.SetDeadline(t) | ||
} | ||
|
||
// SetReadDeadline sets the read deadline on the underlying net.Conn. | ||
func (p *packetConnWrapper) SetReadDeadline(t time.Time) error { | ||
return p.conn.SetReadDeadline(t) | ||
} | ||
|
||
// SetWriteDeadline sets the write deadline on the underlying net.Conn. | ||
func (p *packetConnWrapper) SetWriteDeadline(t time.Time) error { | ||
return p.conn.SetWriteDeadline(t) | ||
} |