Skip to content

Commit

Permalink
added Control() function to give user privilege to set net.PacketConn
Browse files Browse the repository at this point in the history
  • Loading branch information
xtaci committed Aug 23, 2024
1 parent 77f5fe2 commit e2e5372
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions sess.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const (
var (
errInvalidOperation = errors.New("invalid operation")
errTimeout = errors.New("timeout")
errNotOwner = errors.New("not the owner of this connection")
)

var (
Expand Down Expand Up @@ -590,6 +591,18 @@ func (s *UDPSession) SetWriteBuffer(bytes int) error {
return errInvalidOperation
}

// Control applys a procedure to the underly socket fd.
// CAUTION: BE VERY CAREFUL TO USE THIS FUNCTION, YOU MAY BREAK THE PROTOCOL.
func (s *UDPSession) Control(f func(conn net.PacketConn) error) error {
if !s.ownConn {
return errNotOwner
}

s.mu.Lock()
defer s.mu.Unlock()
return f(s.conn)
}

// a goroutine to handle post processing of kcp and make the critical section smaller
// pipeline for outgoing packets (from ARQ to network)
//
Expand Down Expand Up @@ -1060,6 +1073,14 @@ func (l *Listener) Close() error {
return err
}

// Control applys a procedure to the underly socket fd.
// CAUTION: BE VERY CAREFUL TO USE THIS FUNCTION, YOU MAY BREAK THE PROTOCOL.
func (l *Listener) Control(f func(conn net.PacketConn) error) error {
l.sessionLock.Lock()
defer l.sessionLock.Unlock()
return f(l.conn)
}

// closeSession notify the listener that a session has closed
func (l *Listener) closeSession(remote net.Addr) (ret bool) {
l.sessionLock.Lock()
Expand Down

0 comments on commit e2e5372

Please sign in to comment.