| 1 |
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly> |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
// Package main implements a simple ping-pong example |
| 5 |
package main |
| 6 |
|
| 7 |
import ( |
| 8 |
"net" |
| 9 |
"sync" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
// Reference: https://github.com/pion/sctp/blob/master/association_test.go |
| 14 |
// Since UDP is connectionless, as a server, it doesn't know how to reply |
| 15 |
// simply using the `Write` method. So, to make it work, `disconnectedPacketConn` |
| 16 |
// will infer the last packet that it reads as the reply address for `Write` |
| 17 |
|
| 18 |
type disconnectedPacketConn struct { // nolint: unused |
| 19 |
mu sync.RWMutex |
| 20 |
rAddr net.Addr |
| 21 |
pConn net.PacketConn |
| 22 |
} |
| 23 |
|
| 24 |
// Read. |
| 25 |
func (c *disconnectedPacketConn) Read(p []byte) (int, error) { //nolint:unused |
| 26 |
i, rAddr, err := c.pConn.ReadFrom(p) |
| 27 |
if err != nil { |
| 28 |
return 0, err |
| 29 |
} |
| 30 |
|
| 31 |
c.mu.Lock() |
| 32 |
c.rAddr = rAddr |
| 33 |
c.mu.Unlock() |
| 34 |
|
| 35 |
return i, err |
| 36 |
} |
| 37 |
|
| 38 |
// Write writes len(p) bytes from p to the DTLS connection. |
| 39 |
func (c *disconnectedPacketConn) Write(p []byte) (n int, err error) { //nolint:unused |
| 40 |
return c.pConn.WriteTo(p, c.RemoteAddr()) |
| 41 |
} |
| 42 |
|
| 43 |
// Close closes the conn and releases any Read calls. |
| 44 |
func (c *disconnectedPacketConn) Close() error { //nolint:unused |
| 45 |
return c.pConn.Close() |
| 46 |
} |
| 47 |
|
| 48 |
// LocalAddr is a stub. |
| 49 |
func (c *disconnectedPacketConn) LocalAddr() net.Addr { //nolint:unused |
| 50 |
if c.pConn != nil { |
| 51 |
return c.pConn.LocalAddr() |
| 52 |
} |
| 53 |
|
| 54 |
return nil |
| 55 |
} |
| 56 |
|
| 57 |
// RemoteAddr is a stub. |
| 58 |
func (c *disconnectedPacketConn) RemoteAddr() net.Addr { //nolint:unused |
| 59 |
c.mu.RLock() |
| 60 |
defer c.mu.RUnlock() |
| 61 |
|
| 62 |
return c.rAddr |
| 63 |
} |
| 64 |
|
| 65 |
// SetDeadline is a stub. |
| 66 |
func (c *disconnectedPacketConn) SetDeadline(time.Time) error { //nolint:unused |
| 67 |
return nil |
| 68 |
} |
| 69 |
|
| 70 |
// SetReadDeadline is a stub. |
| 71 |
func (c *disconnectedPacketConn) SetReadDeadline(time.Time) error { //nolint:unused |
| 72 |
return nil |
| 73 |
} |
| 74 |
|
| 75 |
// SetWriteDeadline is a stub. |
| 76 |
func (c *disconnectedPacketConn) SetWriteDeadline(time.Time) error { //nolint:unused |
| 77 |
return nil |
| 78 |
} |