Files
netris-nestri/packages/relay/internal/core/room.go
Kristian Ollikainen d87a0b35dd feat: Fully use protobuf, fix controller issues and cleanup (#305)
## Description
### First commit
Restructured protobuf schemas to make them easier to use across
languages, switched to using them in-place of JSON for signaling as
well, so there's no 2 different message formats flying about. Few new
message types to deal with clients and nestri-servers better (not final
format, may see changes still).

General cleanup of dead/unused code along some bug squashing and package
updates.

TODO for future commits:
- [x] Fix additional controllers not doing inputs (possibly needs
vimputti changes)
- [x] ~~Restructure relay protocols code a bit, to reduce bloatiness of
the currently single file for them, more code re-use.~~
- Gonna keep this PR somewhat manageable without poking more at relay..
- [x] ~~Try to fix issue where with multiple clients, static stream
content causes video to freeze until there's some movement.~~
- Was caused by server tuned profile being `throughput-performance`,
causing CPU latency to be too high.
- [x] Ponder the orb


### Second + third commit
Redid the controller polling handling and fixed multi-controller
handling in vimputti and nestri code sides. Remove some dead relay code
as well to clean up the protocol source file, we'll revisit the meshing
functionality later.

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

* **New Features**
* Added software rendering option and MangoHud runtime config;
controller sessions now support reconnection and batched state updates
with persistent session IDs.

* **Bug Fixes**
* Restored previously-filtered NES-like gamepads so they connect
correctly.

* **Chores**
* Modernized dependencies and protobuf tooling, migrated to
protobuf-based messaging and streaming, and removed obsolete CUDA build
steps.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com>
2025-11-08 13:10:28 +02:00

109 lines
2.8 KiB
Go

package core
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"relay/internal/shared"
"github.com/libp2p/go-libp2p/core/network"
"github.com/oklog/ulid/v2"
)
// --- Room Management ---
// GetRoomByID retrieves a local Room struct by its ULID
func (r *Relay) GetRoomByID(id ulid.ULID) *shared.Room {
if room, ok := r.LocalRooms.Get(id); ok {
return room
}
return nil
}
// GetRoomByName retrieves a local Room struct by its name
func (r *Relay) GetRoomByName(name string) *shared.Room {
for _, room := range r.LocalRooms.Copy() {
if room.Name == name {
return room
}
}
return nil
}
// CreateRoom creates a new local Room struct with the given name
func (r *Relay) CreateRoom(name string) *shared.Room {
roomID := ulid.Make()
room := shared.NewRoom(name, roomID, r.ID)
r.LocalRooms.Set(room.ID, room)
slog.Debug("Created new local room", "room", name, "id", room.ID)
return room
}
// DeleteRoomIfEmpty checks if a local room struct is inactive and can be removed
func (r *Relay) DeleteRoomIfEmpty(room *shared.Room) {
if room == nil {
return
}
if len(room.Participants) <= 0 && r.LocalRooms.Has(room.ID) {
slog.Debug("Deleting empty room without participants", "room", room.Name)
r.LocalRooms.Delete(room.ID)
err := room.PeerConnection.Close()
if err != nil {
slog.Error("Failed to close Room PeerConnection", "room", room.Name, "err", err)
}
}
}
// GetRemoteRoomByName returns room from mesh by name
func (r *Relay) GetRemoteRoomByName(roomName string) *shared.RoomInfo {
for _, room := range r.Rooms.Copy() {
if room.Name == roomName && room.OwnerID != r.ID {
// Make sure connection is alive
if r.Host.Network().Connectedness(room.OwnerID) == network.Connected {
return &room
}
slog.Debug("Removing stale peer, owns a room without connection", "room", roomName, "peer", room.OwnerID)
r.onPeerDisconnected(room.OwnerID)
}
}
return nil
}
// --- State Publishing ---
// publishRoomStates publishes the state of all rooms currently owned by *this* relay
func (r *Relay) publishRoomStates(ctx context.Context) error {
if r.pubTopicState == nil {
slog.Warn("Cannot publish room states: topic is nil")
return nil
}
var statesToPublish []shared.RoomInfo
r.LocalRooms.Range(func(id ulid.ULID, room *shared.Room) bool {
// Only publish state for rooms owned by this relay
if room.OwnerID == r.ID {
statesToPublish = append(statesToPublish, shared.RoomInfo{
ID: room.ID,
Name: room.Name,
OwnerID: r.ID,
})
}
return true // Continue iteration
})
if len(statesToPublish) == 0 {
return nil
}
data, err := json.Marshal(statesToPublish)
if err != nil {
return fmt.Errorf("failed to marshal local room states: %w", err)
}
if pubErr := r.pubTopicState.Publish(ctx, data); pubErr != nil {
slog.Error("Failed to publish room states message", "err", pubErr)
}
return nil
}