2024-10-25 19:03:13 -06:00
|
|
|
import { Socket } from "socket.io"
|
|
|
|
|
|
|
|
class Counter {
|
2024-11-16 21:35:06 -05:00
|
|
|
private count: number = 0
|
2024-10-25 19:03:13 -06:00
|
|
|
|
2024-11-16 21:35:06 -05:00
|
|
|
increment() {
|
|
|
|
this.count++
|
|
|
|
}
|
2024-10-25 19:03:13 -06:00
|
|
|
|
2024-11-16 21:35:06 -05:00
|
|
|
decrement() {
|
|
|
|
this.count = Math.max(0, this.count - 1)
|
|
|
|
}
|
2024-10-25 19:03:13 -06:00
|
|
|
|
2024-11-16 21:35:06 -05:00
|
|
|
getValue(): number {
|
|
|
|
return this.count
|
|
|
|
}
|
2024-10-25 19:03:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Owner Connection Management
|
|
|
|
export class ConnectionManager {
|
2024-11-16 21:35:06 -05:00
|
|
|
// Counts how many times the owner is connected to a sandbox
|
|
|
|
private ownerConnections: Record<string, Counter> = {}
|
|
|
|
// Stores all sockets connected to a given sandbox
|
|
|
|
private sockets: Record<string, Set<Socket>> = {}
|
|
|
|
|
|
|
|
// Checks if the owner of a sandbox is connected
|
|
|
|
ownerIsConnected(sandboxId: string): boolean {
|
|
|
|
return this.ownerConnections[sandboxId]?.getValue() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adds a connection for a sandbox
|
|
|
|
addConnectionForSandbox(socket: Socket, sandboxId: string, isOwner: boolean) {
|
|
|
|
this.sockets[sandboxId] ??= new Set()
|
|
|
|
this.sockets[sandboxId].add(socket)
|
|
|
|
|
|
|
|
// If the connection is for the owner, increments the owner connection counter
|
|
|
|
if (isOwner) {
|
|
|
|
this.ownerConnections[sandboxId] ??= new Counter()
|
|
|
|
this.ownerConnections[sandboxId].increment()
|
2024-10-25 19:03:13 -06:00
|
|
|
}
|
2024-11-16 21:35:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Removes a connection for a sandbox
|
|
|
|
removeConnectionForSandbox(
|
|
|
|
socket: Socket,
|
|
|
|
sandboxId: string,
|
|
|
|
isOwner: boolean
|
|
|
|
) {
|
|
|
|
this.sockets[sandboxId]?.delete(socket)
|
|
|
|
|
|
|
|
// If the connection being removed is for the owner, decrements the owner connection counter
|
|
|
|
if (isOwner) {
|
|
|
|
this.ownerConnections[sandboxId]?.decrement()
|
2024-10-25 19:03:13 -06:00
|
|
|
}
|
2024-11-16 21:35:06 -05:00
|
|
|
}
|
2024-10-25 19:03:13 -06:00
|
|
|
|
2024-11-16 21:35:06 -05:00
|
|
|
// Returns the set of sockets connected to a given sandbox
|
|
|
|
connectionsForSandbox(sandboxId: string): Set<Socket> {
|
|
|
|
return this.sockets[sandboxId] ?? new Set()
|
|
|
|
}
|
|
|
|
}
|