Skip to content

Commit e2e5372

Browse files
committed
added Control() function to give user privilege to set net.PacketConn
1 parent 77f5fe2 commit e2e5372

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

sess.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const (
8383
var (
8484
errInvalidOperation = errors.New("invalid operation")
8585
errTimeout = errors.New("timeout")
86+
errNotOwner = errors.New("not the owner of this connection")
8687
)
8788

8889
var (
@@ -590,6 +591,18 @@ func (s *UDPSession) SetWriteBuffer(bytes int) error {
590591
return errInvalidOperation
591592
}
592593

594+
// Control applys a procedure to the underly socket fd.
595+
// CAUTION: BE VERY CAREFUL TO USE THIS FUNCTION, YOU MAY BREAK THE PROTOCOL.
596+
func (s *UDPSession) Control(f func(conn net.PacketConn) error) error {
597+
if !s.ownConn {
598+
return errNotOwner
599+
}
600+
601+
s.mu.Lock()
602+
defer s.mu.Unlock()
603+
return f(s.conn)
604+
}
605+
593606
// a goroutine to handle post processing of kcp and make the critical section smaller
594607
// pipeline for outgoing packets (from ARQ to network)
595608
//
@@ -1060,6 +1073,14 @@ func (l *Listener) Close() error {
10601073
return err
10611074
}
10621075

1076+
// Control applys a procedure to the underly socket fd.
1077+
// CAUTION: BE VERY CAREFUL TO USE THIS FUNCTION, YOU MAY BREAK THE PROTOCOL.
1078+
func (l *Listener) Control(f func(conn net.PacketConn) error) error {
1079+
l.sessionLock.Lock()
1080+
defer l.sessionLock.Unlock()
1081+
return f(l.conn)
1082+
}
1083+
10631084
// closeSession notify the listener that a session has closed
10641085
func (l *Listener) closeSession(remote net.Addr) (ret bool) {
10651086
l.sessionLock.Lock()

0 commit comments

Comments
 (0)