Skip to content

Commit 2312bfa

Browse files
committed
enh: enhances code
1 parent fbe688e commit 2312bfa

3 files changed

Lines changed: 15 additions & 25 deletions

File tree

client.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,14 @@ func Dial(ctx context.Context, net string, laddr, raddr *sctp.SCTPAddr, cfg *Con
3939
conn.sctpConn, err = sctp.DialSCTP(n, laddr, raddr)
4040
if err != nil {
4141
if conn.sctpConn != nil {
42-
logf("go-m3ua: issue dialing connection. closing error: %v\n", conn.sctpConn.Close())
42+
return nil, fmt.Errorf("go-m3ua: issue dialing connection. closing error: %w", conn.sctpConn.Close())
4343
}
4444
return nil, err
4545
}
4646

4747
r, err := conn.sctpConn.GetStatus()
4848
if err != nil {
49-
logf("go-m3ua: failed to retrive sctpConnection status for Dial: %v\n", err)
50-
return nil, err
49+
return nil, fmt.Errorf("go-m3ua: failed to retrive sctpConnection status for Dial: %w", err)
5150
}
5251
conn.maxMessageStreamID = r.Ostreams - 1 // removing 1 for management messages of stream ID 0
5352

@@ -59,12 +58,10 @@ func Dial(ctx context.Context, net string, laddr, raddr *sctp.SCTPAddr, cfg *Con
5958
select {
6059
case _, ok := <-conn.established:
6160
if !ok {
62-
logf("go-m3ua: issue having established client connection. closing error: %v\n", conn.sctpConn.Close())
63-
return nil, ErrFailedToEstablish
61+
return nil, fmt.Errorf("go-m3ua: issue having established client connection. error: %w, closing error: %w", ErrFailedToEstablish, conn.sctpConn.Close())
6462
}
6563
return conn, nil
6664
case <-time.After(10 * time.Second):
67-
logf("go-m3ua: issue client connection timeout. closing error: %v\n", conn.sctpConn.Close())
68-
return nil, ErrTimeout
65+
return nil, fmt.Errorf("go-m3ua: issue client connection timeout. error: %w, closing error: %w", ErrTimeout, conn.sctpConn.Close())
6966
}
7067
}

conn.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package m3ua
66

77
import (
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.
107106
func (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.
143141
func (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
}

server.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package m3ua
66

77
import (
88
"context"
9-
"errors"
109
"fmt"
1110
"net"
1211
"sync"
@@ -63,14 +62,12 @@ func (l *Listener) Accept(ctx context.Context) (*Conn, error) {
6362
var ok bool
6463
conn.sctpConn, ok = c.(*sctp.SCTPConn)
6564
if !ok {
66-
logf("go-m3ua: issue asserting server connection. closing error: %v\n", c.Close())
67-
return nil, errors.New("failed to assert conn")
65+
return nil, fmt.Errorf("go-m3ua: issue asserting server connection. error: %w, closing error: %w", fmt.Errorf("failed to assert conn"), c.Close())
6866
}
6967

7068
r, err := conn.sctpConn.GetStatus()
7169
if err != nil {
72-
logf("go-m3ua: failed to retrive sctpConnection status for Accept: %v\n", err)
73-
return nil, err
70+
return nil, fmt.Errorf("go-m3ua: failed to retrive sctpConnection status for Accept: %w", err)
7471
}
7572
conn.maxMessageStreamID = r.Ostreams - 1 // removing 1 for management messages of stream ID 0
7673

@@ -86,17 +83,15 @@ func (l *Listener) Accept(ctx context.Context) (*Conn, error) {
8683
if conn.sctpConn != nil {
8784
closeErr = conn.sctpConn.Close()
8885
}
89-
logf("go-m3ua: issue having established server connection. closing error: %v\n", closeErr)
90-
return nil, ErrFailedToEstablish
86+
return nil, fmt.Errorf("go-m3ua: issue having established server connection. error: %w, closing error: %w", ErrFailedToEstablish, closeErr)
9187
}
9288
return conn, nil
9389
case <-time.After(10 * time.Second):
9490
var closeErr error
9591
if conn.sctpConn != nil {
9692
closeErr = conn.sctpConn.Close()
9793
}
98-
logf("go-m3ua: issue server connection timeout. closing error: %v\n", closeErr)
99-
return nil, ErrTimeout
94+
return nil, fmt.Errorf("go-m3ua: issue server connection timeout. error: %w, closing error: %w", ErrTimeout, closeErr)
10095
}
10196
}
10297

0 commit comments

Comments
 (0)