@@ -6,7 +6,6 @@ package m3ua
66
77import (
88 "fmt"
9- "log"
109 "math/rand"
1110 "net"
1211 "sync"
@@ -105,7 +104,7 @@ func (c *Conn) ReadPD() (pd *params.ProtocolDataPayload, err error) {
105104
106105// Write writes data to the connection.
107106func (c * Conn ) Write (b []byte ) (n int , err error ) {
108- stream := RandomUint16 ( c . maxMessageStreamID ) // choose a random stream number from 1 to a certain maximum
107+ stream := c . chooseStreamID ()
109108
110109 return c .WriteToStream (b , stream )
111110}
@@ -131,8 +130,7 @@ func (c *Conn) WriteToStream(b []byte, streamID uint16) (n int, err error) {
131130 info .Stream = streamID
132131 n , err = c .sctpConn .SCTPWrite (d , & info )
133132 if err != nil {
134- log .Printf ("go-m3ua: error writing on sctp connection, stream id: %v, max negotiated stream id: %v, error : %v" , streamID , c .maxMessageStreamID , err )
135- return 0 , err
133+ return 0 , fmt .Errorf ("go-m3ua: error writing on sctp connection, stream id: %v, max negotiated stream id: %v, error : %w" , streamID , c .maxMessageStreamID , err )
136134 }
137135
138136 n += len (d )
@@ -141,7 +139,7 @@ func (c *Conn) WriteToStream(b []byte, streamID uint16) (n int, err error) {
141139
142140// WritePD writes data with a specific mtp3 protocol data to the connection.
143141func (c * Conn ) WritePD (protocolData * params.Param ) (n int , err error ) {
144- stream := RandomUint16 ( c . maxMessageStreamID ) // choose a random stream number from 1 to a certain maximum
142+ stream := c . chooseStreamID ()
145143
146144 return c .WritePDToStream (protocolData , stream )
147145}
@@ -255,15 +253,15 @@ func (c *Conn) MaxMessageStreamID() uint16 {
255253 return c .maxMessageStreamID
256254}
257255
258- // RandomUint16 generates a random uint16 from 1 to max (inclusive)
259- func RandomUint16 ( max uint16 ) uint16 {
256+ // chooseStreamID generates a random uint16 from 1 to max (inclusive)
257+ func ( c * Conn ) chooseStreamID ( ) uint16 {
260258 r := rand .New (rand .NewSource (time .Now ().UnixNano ()))
261259
262- if max == 1 {
260+ if c . maxMessageStreamID == 1 {
263261 return 1
264262 }
265263
266- randomNum := uint16 (r .Intn (int (max )))
264+ randomNum := uint16 (r .Intn (int (c . maxMessageStreamID )))
267265
268266 return randomNum + 1
269267}
0 commit comments