2023-11-03 11:00:40 +08:00
|
|
|
package net
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
2025-04-10 01:16:54 +08:00
|
|
|
|
|
|
|
"github.com/metacubex/mihomo/common/contextutils"
|
2023-11-03 11:00:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetupContextForConn is a helper function that starts connection I/O interrupter goroutine.
|
|
|
|
func SetupContextForConn(ctx context.Context, conn net.Conn) (done func(*error)) {
|
2025-04-10 01:16:54 +08:00
|
|
|
stopc := make(chan struct{})
|
|
|
|
stop := contextutils.AfterFunc(ctx, func() {
|
|
|
|
// Close the connection, discarding the error
|
|
|
|
_ = conn.Close()
|
|
|
|
close(stopc)
|
|
|
|
})
|
2023-11-03 11:00:40 +08:00
|
|
|
return func(inputErr *error) {
|
2025-04-10 01:16:54 +08:00
|
|
|
if !stop() {
|
|
|
|
// The AfterFunc was started, wait for it to complete.
|
|
|
|
<-stopc
|
|
|
|
if ctxErr := ctx.Err(); ctxErr != nil && inputErr != nil {
|
|
|
|
// Return context error to user.
|
|
|
|
inputErr = &ctxErr
|
|
|
|
}
|
2023-11-03 11:00:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|