|
| 1 | +package sctp |
| 2 | + |
| 3 | +import ( |
| 4 | + "net" |
| 5 | + |
| 6 | + "github.com/pion/udp" |
| 7 | +) |
| 8 | + |
| 9 | +// ListenAssociation creates a SCTP association listener |
| 10 | +func ListenAssociation(network string, laddr *net.UDPAddr, config Config) (*AssociationListener, error) { |
| 11 | + lc := udp.ListenConfig{} |
| 12 | + parent, err := lc.Listen(network, laddr) |
| 13 | + if err != nil { |
| 14 | + return nil, err |
| 15 | + } |
| 16 | + return &AssociationListener{ |
| 17 | + config: config, |
| 18 | + parent: parent, |
| 19 | + }, nil |
| 20 | +} |
| 21 | + |
| 22 | +// NewAssociationListener creates a SCTP association listener |
| 23 | +// which accepts connections from an inner Listener. |
| 24 | +// The net.Conn in the config is ignored. |
| 25 | +func NewAssociationListener(inner net.Listener, config Config) (*AssociationListener, error) { |
| 26 | + return &AssociationListener{ |
| 27 | + config: config, |
| 28 | + parent: inner, |
| 29 | + }, nil |
| 30 | +} |
| 31 | + |
| 32 | +// AssociationListener represents a SCTP association listener |
| 33 | +type AssociationListener struct { |
| 34 | + config Config |
| 35 | + parent net.Listener |
| 36 | +} |
| 37 | + |
| 38 | +// Accept waits for and returns the next association to the listener. |
| 39 | +// You have to either close or read on all connection that are created. |
| 40 | +func (l *AssociationListener) Accept() (*Association, error) { |
| 41 | + c, err := l.parent.Accept() |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + l.config.NetConn = c |
| 46 | + return Server(l.config) |
| 47 | +} |
| 48 | + |
| 49 | +// Close closes the listener. |
| 50 | +// Any blocked Accept operations will be unblocked and return errors. |
| 51 | +// Already Accepted connections are not closed. |
| 52 | +func (l *AssociationListener) Close() error { |
| 53 | + return l.parent.Close() |
| 54 | +} |
| 55 | + |
| 56 | +// Addr returns the listener's network address. |
| 57 | +func (l *AssociationListener) Addr() net.Addr { |
| 58 | + return l.parent.Addr() |
| 59 | +} |
0 commit comments