mirror of
https://github.com/nestriness/nestri.git
synced 2025-12-12 16:55:37 +02:00
## 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>
98 lines
1.9 KiB
Go
98 lines
1.9 KiB
Go
package common
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
// SafeMap is a generic thread-safe map with its own mutex
|
|
type SafeMap[K comparable, V any] struct {
|
|
mu sync.RWMutex
|
|
m map[K]V
|
|
}
|
|
|
|
// NewSafeMap creates a new SafeMap instance
|
|
func NewSafeMap[K comparable, V any]() *SafeMap[K, V] {
|
|
return &SafeMap[K, V]{
|
|
m: make(map[K]V),
|
|
}
|
|
}
|
|
|
|
// Get retrieves a value from the map
|
|
func (sm *SafeMap[K, V]) Get(key K) (V, bool) {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
v, ok := sm.m[key]
|
|
return v, ok
|
|
}
|
|
|
|
// Has checks if a key exists in the map
|
|
func (sm *SafeMap[K, V]) Has(key K) bool {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
_, ok := sm.m[key]
|
|
return ok
|
|
}
|
|
|
|
// Set adds or updates a value in the map
|
|
func (sm *SafeMap[K, V]) Set(key K, value V) {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
sm.m[key] = value
|
|
}
|
|
|
|
// Delete removes a key from the map
|
|
func (sm *SafeMap[K, V]) Delete(key K) {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
delete(sm.m, key)
|
|
}
|
|
|
|
// Len returns the number of items in the map
|
|
func (sm *SafeMap[K, V]) Len() int {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
return len(sm.m)
|
|
}
|
|
|
|
// Copy creates a shallow copy of the map and returns it
|
|
func (sm *SafeMap[K, V]) Copy() map[K]V {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
copied := make(map[K]V, len(sm.m))
|
|
for k, v := range sm.m {
|
|
copied[k] = v
|
|
}
|
|
return copied
|
|
}
|
|
|
|
// Range iterates over the map and applies a function to each key-value pair
|
|
func (sm *SafeMap[K, V]) Range(f func(K, V) bool) {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
for k, v := range sm.m {
|
|
if !f(k, v) {
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
func (sm *SafeMap[K, V]) MarshalJSON() ([]byte, error) {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
return json.Marshal(sm.m)
|
|
}
|
|
|
|
func (sm *SafeMap[K, V]) UnmarshalJSON(data []byte) error {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
return json.Unmarshal(data, &sm.m)
|
|
}
|
|
|
|
func (sm *SafeMap[K, V]) String() string {
|
|
sm.mu.RLock()
|
|
defer sm.mu.RUnlock()
|
|
return fmt.Sprintf("%+v", sm.m)
|
|
}
|