Files
netris-nestri/packages/relay/internal/core/peer.go
Kristian Ollikainen c62a22b552 feat: Controller support, performance enchancements, multi-stage images, fixes (#304)
## Description
Oops.. another massive PR 🥲 

This PR contains multiple improvements and changes.

Firstly, thanks gst-wayland-display's PR
[here](https://github.com/games-on-whales/gst-wayland-display/pull/20).
NVIDIA path is now way more efficient than before.

Secondly, adding controller support was a massive hurdle, requiring me
to start another project
[vimputti](https://github.com/DatCaptainHorse/vimputti) - which allows
simple virtual controller inputs in isolated containers. Well, it's not
simple, it includes LD_PRELOAD shims and other craziness, but the
library API is simple to use..

Thirdly, split runner image into 3 separate stages, base + build +
runtime, should help keep things in check in future, also added GitHub
Actions CI builds for v2 to v4 builds (hopefully they pass..).

Fourth, replaced the runner's runtime Steam patching with better and
simpler bubblewrap patch, massive thanks to `games-on-whales` to
figuring it out better!

Fifth, relay for once needed some changes, the new changes are still
mostly WIP, but I'll deal with them next time I have energy.. I'm spent
now. Needed to include these changes as relay needed a minor change to
allow rumble events to flow back to client peer.

Sixth.. tons of package updates, minor code improvements and the usual. 

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **New Features**
* End-to-end gamepad/controller support (attach/detach, buttons, sticks,
triggers, rumble) with client/server integration and virtual controller
plumbing.
  * Optional Prometheus metrics endpoint and WebTransport support.
  * Background vimputti manager process added for controller handling.

* **Improvements**
  * Multi-variant container image builds and streamlined runtime images.
  * Zero-copy video pipeline and encoder improvements for lower latency.
  * Updated Steam compat mapping and dependency/toolchain refreshes.

* **Bug Fixes**
* More robust GPU detection, input/fullscreen lifecycle,
startup/entrypoint, and container runtime fixes.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com>
2025-10-20 11:20:05 +03:00

78 lines
2.3 KiB
Go

package core
import (
"errors"
"log/slog"
"os"
"relay/internal/common"
"relay/internal/shared"
"time"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
)
// PeerInfo contains information of a peer, in light transmit-friendly format
type PeerInfo struct {
ID peer.ID
Addrs []multiaddr.Multiaddr // Addresses of this peer
Peers *common.SafeMap[peer.ID, *PeerInfo] // Peers connected to this peer
Latencies *common.SafeMap[peer.ID, time.Duration] // Latencies to other peers from this peer
Rooms *common.SafeMap[string, shared.RoomInfo] // Rooms this peer is part of or owner of
}
func NewPeerInfo(id peer.ID, addrs []multiaddr.Multiaddr) *PeerInfo {
return &PeerInfo{
ID: id,
Addrs: addrs,
Peers: common.NewSafeMap[peer.ID, *PeerInfo](),
Latencies: common.NewSafeMap[peer.ID, time.Duration](),
Rooms: common.NewSafeMap[string, shared.RoomInfo](),
}
}
// SaveToFile saves the peer store to a JSON file in persistent path
func (pi *PeerInfo) SaveToFile(filePath string) error {
if len(filePath) <= 0 {
return errors.New("filepath is not set")
}
// Marshal the peer store to JSON array (we don't need to store IDs..)
data, err := pi.Peers.MarshalJSON()
if err != nil {
return errors.New("failed to marshal peer store data: " + err.Error())
}
// Save the data to a file
if err = os.WriteFile(filePath, data, 0644); err != nil {
return errors.New("failed to save peer store to file: " + err.Error())
}
slog.Info("PeerStore saved to file", "path", filePath)
return nil
}
// LoadFromFile loads the peer store from a JSON file in persistent path
func (pi *PeerInfo) LoadFromFile(filePath string) error {
if len(filePath) <= 0 {
return errors.New("filepath is not set")
}
data, err := os.ReadFile(filePath)
if err != nil {
if os.IsNotExist(err) {
slog.Info("PeerStore file does not exist, starting with empty store")
return nil // No peers to load
}
return errors.New("failed to read peer store file: " + err.Error())
}
// Unmarshal the JSON data into the peer store
if err = pi.Peers.UnmarshalJSON(data); err != nil {
return errors.New("failed to unmarshal peer store data: " + err.Error())
}
slog.Info("PeerStore loaded from file", "path", filePath)
return nil
}