Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions src/server/nuclearnet/web_socket_proxy_nuclearnet_server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NUClearNetOptions } from 'nuclearnet.js'
import { NUClearNetPeer } from 'nuclearnet.js'
import { NUClearNetPacket } from 'nuclearnet.js'
import * as stream from 'stream'

import { NUClearNetClient } from '../../shared/nuclearnet/nuclearnet_client'
import { Clock } from '../../shared/time/clock'
Expand Down Expand Up @@ -40,7 +41,7 @@ class WebSocketServerClient {
private offJoin: () => void
private offLeave: () => void
private offListenMap: Map<string, () => void>
private processors: Map<NUClearNetPeer, PacketProcessor>
private processors: Map<NUClearNetPeer, PacketStream>

public constructor(private nuclearnetClient: NUClearNetClient, private socket: WebSocket) {
this.connected = false
Expand All @@ -61,7 +62,8 @@ class WebSocketServerClient {

private onJoin = (peer: NUClearNetPeer) => {
this.socket.send('nuclear_join', peer)
this.processors.set(peer, PacketProcessor.of(this.socket))
// this.processors.set(peer, PacketProcessor.of(this.socket))
this.processors.set(peer, PacketStream.of(this.socket))
}

private onLeave = (peer: NUClearNetPeer) => {
Expand Down Expand Up @@ -108,7 +110,8 @@ class WebSocketServerClient {
if (peerKey) {
const processor = this.processors.get(peerKey)
if (processor) {
processor.onPacket(event, packet)
const streamPacket: StreamPacket = { event, packet }
processor.write(streamPacket)
}
}
}
Expand All @@ -123,6 +126,32 @@ class WebSocketServerClient {
}
}

type StreamPacket = {
event: string,
packet: NUClearNetPacket
}

class PacketStream extends stream.Writable {
public constructor(private socket: WebSocket) {
super({
objectMode: true,
})
}

public static of(socket: WebSocket) {
return new PacketStream(socket)
}

public _write(streamPacket: StreamPacket) {
const { event, packet } = streamPacket
if (packet.reliable) {
this.socket.send(event, packet)
} else {
this.socket.sendVolatile(event, packet)
}
}
}

class PacketProcessor {
private eventQueueSize: Map<string, number>

Expand All @@ -149,9 +178,10 @@ class PacketProcessor {
this.sendReliablePacket(event, packet)
} else if (this.isEventBelowLimit(event)) {
this.sendUnreliablePacket(event, packet)
}/* else {
// This event is unreliable and already at the limit, simply drop the packet.
}*/
}
/* else {
// This event is unreliable and already at the limit, simply drop the packet.
}*/
}

private isEventBelowLimit(event: string) {
Expand Down
4 changes: 4 additions & 0 deletions src/server/nuclearnet/web_socket_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,8 @@ export class WebSocket {
public send(event: string, ...args: any[]) {
this.sioSocket.emit(event, ...args)
}

public sendVolatile(event: string, ...args: any[]) {
this.sioSocket.volatile.emit(event, ...args)
}
}