Files
netris-nestri/packages/relay/internal/core/room.go
Kristian Ollikainen 6e82eff9e2 feat: Migrate from WebSocket to libp2p for peer-to-peer connectivity (#286)
## Description
Whew, some stuff is still not re-implemented, but it's working!

Rabbit's gonna explode with the amount of changes I reckon 😅



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

- **New Features**
- Introduced a peer-to-peer relay system using libp2p with enhanced
stream forwarding, room state synchronization, and mDNS peer discovery.
- Added decentralized room and participant management, metrics
publishing, and safe, size-limited, concurrent message streaming with
robust framing and callback dispatching.
- Implemented asynchronous, callback-driven message handling over custom
libp2p streams replacing WebSocket signaling.
- **Improvements**
- Migrated signaling and stream protocols from WebSocket to libp2p,
improving reliability and scalability.
- Simplified configuration and environment variables, removing
deprecated flags and adding persistent data support.
- Enhanced logging, error handling, and connection management for better
observability and robustness.
- Refined RTP header extension registration and NAT IP handling for
improved WebRTC performance.
- **Bug Fixes**
- Improved ICE candidate buffering and SDP negotiation in WebRTC
connections.
  - Fixed NAT IP and UDP port range configuration issues.
- **Refactor**
- Modularized codebase, reorganized relay and server logic, and removed
deprecated WebSocket-based components.
- Streamlined message structures, removed obsolete enums and message
types, and simplified SafeMap concurrency.
- Replaced WebSocket signaling with libp2p stream protocols in server
and relay components.
- **Chores**
- Updated and cleaned dependencies across Go, Rust, and JavaScript
packages.
  - Added `.gitignore` for persistent data directory in relay package.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

---------

Co-authored-by: DatCaptainHorse <DatCaptainHorse@users.noreply.github.com>
Co-authored-by: Philipp Neumann <3daquawolf@gmail.com>
2025-06-06 16:48:49 +03: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 room.Participants.Len() == 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.MeshRooms.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
} else {
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
}