Documentation
¶
Overview ¶
Package controllers reads the game controllers on a player's PC through SDL3 and yields the ControllerFrames a client streams to the game's host, as modlock.wasm.controllers describes.
SDL3 is loaded at run time, so a client without it runs on and reports ErrUnavailable. SDL opens a device without exclusive access, so Steam Input and the game keep reading it, and the reader asks for no enhanced reports or lights that would change what other programs see.
Index ¶
- Constants
- Variables
- func FeedHost(ctx context.Context, host SRPCControllerFeedClient)
- func FeedLocal(ctx context.Context, hub *Hub)
- func InstallSDL(ctx context.Context, dir string) error
- func LoadSDL(path string) error
- func NewSRPCControllerFeedHandler(impl SRPCControllerFeedServer, serviceID string) srpc.Handler
- func SRPCRegisterControllerFeed(mux srpc.Mux, impl SRPCControllerFeedServer) error
- func Serve(ctx context.Context, log *slog.Logger, wants <-chan uint32, ...) error
- type Feed
- type Hub
- func (h *Hub) Follow(event *control.HostEvent)
- func (h *Hub) Forward(guest string, local *net.UDPAddr) func()
- func (h *Hub) Joined(slot int32, address string, steamID uint64)
- func (h *Hub) Left(slot int32)
- func (h *Hub) Members(members map[string]uint64)
- func (h *Hub) Open(guest string) *Feed
- func (h *Hub) ServeFeed(guest string, strm SRPCControllerFeed_FeedStream) error
- func (h *Hub) Wanted(wants *pb.ControllerWants)
- type Reader
- type SRPCControllerFeedClient
- type SRPCControllerFeedHandler
- func (SRPCControllerFeedHandler) GetMethodIDs() []string
- func (d *SRPCControllerFeedHandler) GetServiceID() string
- func (d *SRPCControllerFeedHandler) InvokeMethod(serviceID, methodID string, strm srpc.Stream) (bool, error)
- func (SRPCControllerFeedHandler) InvokeMethod_Feed(impl SRPCControllerFeedServer, strm srpc.Stream) error
- type SRPCControllerFeedServer
- type SRPCControllerFeed_FeedClient
- type SRPCControllerFeed_FeedStream
Constants ¶
const ( SDLArchive = "https://github.com/libsdl-org/SDL/releases/download/release-" + SDLVersion + "/SDL3-" + SDLVersion + "-win32-x64.zip" SDLArchiveSHA256 = "368bed8b24e44ad9ea372990814f2343fcfb9e468abb7f8f4a3bc146c7304752" )
SDLArchive is SDL's published Windows x64 archive of SDLVersion, and SDLArchiveSHA256 its pinned checksum, which every download must match.
const FeedProtocolID = protocol.ID("modlock/controllers/1")
FeedProtocolID identifies the peer streams that carry the ControllerFeed service from a player's own client to their game server.
const Heartbeat = 50 * time.Millisecond
Heartbeat is the longest a running reader goes without sending a frame, well inside the host's 200 millisecond stale deadline.
const MinInterval = time.Second / 128
MinInterval is the shortest gap between two frames: 128 a second, two per 64 Hz server tick. Presses and releases in between wait for the next one.
const SDLVersion = "3.4.20"
SDLVersion is the SDL release whose Windows x64 SDL3.dll the reader loads when none sits beside the executable.
const SRPCControllerFeedServiceID = "modlock.controllers.ControllerFeed"
const VirtualEnv = "MODLOCK_VIRTUAL_CONTROLLERS"
VirtualEnv names the environment variable that, set to 1, makes every Reader in the process stream two virtual devices beside the real ones, so a controller mod can be built and measured on a PC with no device:
- "Modlock virtual gamepad", a gamepad whose left stick's x traces the wall clock, (t mod 2 s) / 1 s - 1, so a mod decodes when the value was read as (x + 1) seconds past an even second and compares it with its own clock. Its left stick's y and its right stick circle once every 4 s, its triggers ramp up over each second, and its south button is tapped for one sample at each quarter second, a press and a release shorter than a server frame.
- "Modlock virtual radio", a joystick with eight axes, four buttons and a hat, as a radio transmitter in USB joystick mode looks. Axis k is sin(2π t / 4 s + kπ / 4), button k is held during second k of each 4 s, and the hat points up, right, down and left in turn, a second each.
Variables ¶
ErrUnavailable is returned by Open when no SDL3 library can be loaded, so a client without it runs on and streams no controllers.
Functions ¶
func FeedHost ¶
func FeedHost(ctx context.Context, host SRPCControllerFeedClient)
FeedHost streams this PC's controllers to a game server for the player whose game this peer carries, reading them only while the server's mods watch, until ctx ends. A server that serves no feed, or one that drops it, leaves the player with no controllers and the game playing on.
func FeedLocal ¶
FeedLocal streams this PC's controllers to the host's own player on the game server this program runs, reading them only while the server's mods watch, until ctx ends.
func InstallSDL ¶
InstallSDL downloads SDLArchive, checks it against SDLArchiveSHA256, and writes its SDL3.dll into dir, which it creates whole or not at all. It does nothing when dir exists. An application that ships SDL3.dll beside its executable calls it at build time; the Windows reader calls it on the PC that lacks one.
func LoadSDL ¶
LoadSDL loads the SDL3 library at path for every later Open, for an application that ships its own copy somewhere the reader does not look, such as a directory it unpacks into. It does nothing once a load has succeeded.
func NewSRPCControllerFeedHandler ¶
func NewSRPCControllerFeedHandler(impl SRPCControllerFeedServer, serviceID string) srpc.Handler
NewSRPCControllerFeedHandler constructs a new RPC handler. serviceID: if empty, uses default: modlock.controllers.ControllerFeed
func SRPCRegisterControllerFeed ¶
func SRPCRegisterControllerFeed(mux srpc.Mux, impl SRPCControllerFeedServer) error
SRPCRegisterControllerFeed registers the implementation with the mux. Uses the default serviceID: modlock.controllers.ControllerFeed
func Serve ¶
func Serve(ctx context.Context, log *slog.Logger, wants <-chan uint32, send func(*pb.ControllerFrame) error) error
Serve reads this PC's controllers while the latest want epoch is nonzero, passing each frame to send, until ctx ends, wants closes or send fails. A zero epoch closes the devices, so a player whose controllers no mod watches has none open. Where SDL3 is missing it logs once and reads nothing, which the host sees as a player with no stream.
Types ¶
type Feed ¶
type Feed struct {
// contains filtered or unexported fields
}
Feed is one controller stream: a guest's, or the host player's own when its guest is empty.
func (*Feed) Close ¶
func (f *Feed) Close()
Close ends the feed's stream, ending its player's stream at the server.
func (*Feed) Frame ¶
func (f *Feed) Frame(frame *pb.ControllerFrame)
Frame passes a frame from the feed to its slot's player while the mods watch them, and drops it otherwise.
type Hub ¶
type Hub struct {
// contains filtered or unexported fields
}
Hub binds each controller stream on a game server to the one player whose game connection it may feed, passes that player's frames to the server, and tells each stream the epoch the server's mods watch its player at.
A guest's stream feeds the slot whose client connects from one of the sockets the server bound to forward that guest's game stream, as the server reports when the player joins, or, for a game that connects to the server directly, the slot whose Steam account the server's own authority verified as the guest's. A frame names no player, so no guest can feed another's slot. The host's own player, fed by a stream with no guest, is the slot connecting from a loopback address that forwards no guest.
The program that runs the server owns the Hub: it reports joins, leaves and the watched players from the server, each forwarding socket from its game links, each guest's verified account when it has one, and opens a Feed for each controller stream it accepts.
func NewHub ¶
func NewHub(send func(*pb.ControllerFrames)) *Hub
NewHub returns a hub that passes each bound player's frames, and the end of each player's stream, to send. send runs under the hub's lock, so it must not call the hub.
func (*Hub) Follow ¶
Follow passes hub the server events that bind controller streams: a join that gives a slot's address, a leave that frees it, and the set of watched players. A join with no Steam ID is a bot, whose slot no stream may feed.
func (*Hub) Forward ¶
Forward records that a socket at local forwards guest's game stream, and returns the call that forgets it once the stream ends.
func (*Hub) Joined ¶
Joined records that the client in slot connects from address, its ip:port or the engine's "loopback", signed in to steamID, the account the engine authenticated or zero. A bot, with neither, is forgotten, so no stream may feed its slot.
func (*Hub) Members ¶
Members replaces the guests whose Steam accounts the server's own authority verified, such as its lobby's roster, with members, from guest to account. A member's stream feeds the slot signed in to its account, for a game that connects without a forwarding socket.
func (*Hub) Open ¶
Open starts a controller stream for guest, or for the host's own player when guest is empty. The caller closes it when the stream ends.
func (*Hub) ServeFeed ¶
func (h *Hub) ServeFeed(guest string, strm SRPCControllerFeed_FeedStream) error
ServeFeed takes the controller frames of guest, the peer the stream came over, for the slot its own game connection holds, and sends the mods' want for that slot as it changes, until the guest or the stream ends. An empty guest feeds the host's own player, so a server calls it only with the peer its transport authenticated.
func (*Hub) Wanted ¶
func (h *Hub) Wanted(wants *pb.ControllerWants)
Wanted replaces the watched players with wants, the server's latest list.
type Reader ¶
type Reader struct {
// contains filtered or unexported fields
}
Reader streams the controllers attached to this PC. SDL's state is process-wide, so one Run goes at a time.
func Open ¶
Open loads SDL3, or returns ErrUnavailable when it cannot be loaded; a later Open tries again. A nil log uses slog's default.
type SRPCControllerFeedClient ¶
type SRPCControllerFeedClient interface {
// SRPCClient returns the underlying SRPC client.
SRPCClient() srpc.Client
// Feed carries the client's controllers to the player whose game connection
// the same peer link carries; the frames name no player, so a client feeds
// only its own. The server sends that player's want each time it changes,
// with epoch zero while no mod watches or the client has no slot, and the
// client reads its devices only while the latest epoch is nonzero.
Feed(ctx context.Context) (SRPCControllerFeed_FeedClient, error)
}
func NewSRPCControllerFeedClient ¶
func NewSRPCControllerFeedClient(cc srpc.Client) SRPCControllerFeedClient
func NewSRPCControllerFeedClientWithServiceID ¶
func NewSRPCControllerFeedClientWithServiceID(cc srpc.Client, serviceID string) SRPCControllerFeedClient
type SRPCControllerFeedHandler ¶
type SRPCControllerFeedHandler struct {
// contains filtered or unexported fields
}
func (SRPCControllerFeedHandler) GetMethodIDs ¶
func (SRPCControllerFeedHandler) GetMethodIDs() []string
func (*SRPCControllerFeedHandler) GetServiceID ¶
func (d *SRPCControllerFeedHandler) GetServiceID() string
func (*SRPCControllerFeedHandler) InvokeMethod ¶
func (SRPCControllerFeedHandler) InvokeMethod_Feed ¶
func (SRPCControllerFeedHandler) InvokeMethod_Feed(impl SRPCControllerFeedServer, strm srpc.Stream) error
type SRPCControllerFeedServer ¶
type SRPCControllerFeedServer interface {
// Feed carries the client's controllers to the player whose game connection
// the same peer link carries; the frames name no player, so a client feeds
// only its own. The server sends that player's want each time it changes,
// with epoch zero while no mod watches or the client has no slot, and the
// client reads its devices only while the latest epoch is nonzero.
Feed(SRPCControllerFeed_FeedStream) error
}
type SRPCControllerFeed_FeedClient ¶
type SRPCControllerFeed_FeedClient interface {
srpc.Stream
Send(*controllers2.ControllerFrame) error
Recv() (*controllers2.ControllerWant, error)
RecvTo(*controllers2.ControllerWant) error
}
type SRPCControllerFeed_FeedStream ¶
type SRPCControllerFeed_FeedStream interface {
srpc.Stream
Send(*controllers2.ControllerWant) error
SendAndClose(*controllers2.ControllerWant) error
Recv() (*controllers2.ControllerFrame, error)
RecvTo(*controllers2.ControllerFrame) error
}