Files
sing-box/service/usbip/host.go
T
世界 b9cf48a827 usbip: drop ctx ceremony from leaf APIs, move ExportHost lifecycle ctx into constructor
Export.{Snapshot,LeaseCheck,DeviceInfo}, ExportHost.{Reconcile,FinishImport},
ImportHost.Start, and all 11 exportLedger methods carried ctx params that
implementations never consumed (linux FinishImport now reaches into h.runCtx
internally). UrbTransaction.{Wait,Cancel} did consume ctx, but every call site
passed context.Background(), forcing the reverse pattern in endpoint_darwin
where e.ctx was already cancelled. Cancel becomes synchronous, Wait reads
under the close(t.done) happens-before. ExportHost's runCtx now derives in
newPlatformExportHost so Start() is just precondition-check (linux
ensureKernelPath / darwin no-op), and the three pre-Start nil defences in
Close/Events fall out.
2026-06-09 10:42:34 +08:00

69 lines
2.0 KiB
Go

//go:build linux || (darwin && cgo)
package usbip
import (
"context"
"net"
)
// ExportHost lifecycle: Start → Reconcile* → Close. Callers must apply
// the Reconcile snapshot and released list even when err != nil.
type ExportHost interface {
Start() error
Close() error
Reconcile(isReserved func(busid string) bool) (snapshot map[string]Export, released []string, err error)
FinishImport(busid string) (released bool, err error)
// Events MUST be called after Start succeeds. The channel closes when
// Close() runs.
Events() (<-chan struct{}, error)
}
type ImportHost interface {
Start() error
Close() error
// Attach takes ownership of conn for the lifetime of the import.
Attach(ctx context.Context, info DeviceInfoTruncated, conn net.Conn) (AttachedSession, error)
}
type ExportLeaseIdentity string
// Export pointers handed back from Reconcile are immutable from the
// ledger's perspective: the ledger calls the methods below outside any
// lock. Hosts that need to mutate must clone, mutate the clone, then
// swap it into their committed map under the host's own lock.
type Export interface {
BusID() string
Snapshot(busy bool) ExportSnapshot
LeaseIdentity() ExportLeaseIdentity
LeaseCheck() (ok bool, reason string)
DeviceInfo() (DeviceInfoTruncated, error)
NewServerDataSession(ctx context.Context, conn net.Conn) (DataSession, error)
}
// ExportSnapshot entries with an empty BusID are filtered out by the
// ledger; hosts may keep stale snapshots so state transitions broadcast.
type ExportSnapshot struct {
Entry DeviceEntry
Backend string
StableID string
State string
StatusReason string
RawStatus int
}
// DataSession implementations MUST close Done when the session
// terminates. Err is only valid after Done. Start and Close are
// idempotent; Close before Start releases all resources.
type DataSession interface {
Done() <-chan struct{}
Err() error
Start() error
Close() error
}
type AttachedSession interface {
DataSession
Description() string
}