More appropiate terminology

client-side, server-side (highly ambiguous and context-dependant) to upstream and downstream (industry-standard)
This commit is contained in:
Dinhero21 2024-08-30 21:13:01 -03:00
parent cc9c274325
commit c0d1876548
3 changed files with 48 additions and 73 deletions

View file

@ -5,8 +5,8 @@ import { type RawPacket } from '../../util/packet.js';
import { type Message } from './shared.js';
export default async function (instance: Instance): Promise<void> {
const clientQueue: RawPacket[] = [];
const serverQueue: RawPacket[] = [];
const downstreamQueue: RawPacket[] = [];
const upstreamQueue: RawPacket[] = [];
const channel = instance.createChannel<Message>('proxy');
@ -39,10 +39,8 @@ export default async function (instance: Instance): Promise<void> {
});
client.on('packet', (data, meta) => {
// if (meta.state !== states.PLAY) return;
channel.write({
direction: 'downstream',
direction: 'upstream',
packet: {
...meta,
data,
@ -51,10 +49,8 @@ export default async function (instance: Instance): Promise<void> {
});
server.on('packet', (data, meta) => {
// if (meta.state !== states.PLAY) return;
channel.write({
direction: 'upstream',
direction: 'downstream',
packet: {
...meta,
data,
@ -67,40 +63,36 @@ export default async function (instance: Instance): Promise<void> {
client.on('state', (state) => {
if (state !== states.PLAY) return;
const queue = clientQueue;
for (const packet of downstreamQueue) writeDownstreamPacket(packet);
for (const packet of queue) client.write(packet.name, packet.data);
queue.length = 0;
downstreamQueue.length = 0;
});
server.on('state', (state) => {
if (state !== states.PLAY) return;
const queue = serverQueue;
for (const packet of upstreamQueue) writeUpstreamPacket(packet);
for (const packet of queue) server.write(packet.name, packet.data);
queue.length = 0;
upstreamQueue.length = 0;
});
channel.subscribe(({ direction: side, packet }) => {
switch (side) {
channel.subscribe(({ direction: direction, packet }) => {
switch (direction) {
case 'downstream':
writeClientPacket(packet);
writeDownstreamPacket(packet);
break;
case 'upstream':
writeServerPacket(packet);
writeUpstreamPacket(packet);
break;
default:
throw new Error(`Invalid side: ${side as any}`);
throw new Error(`Unknown direction: ${direction as any}`);
}
});
function writeClientPacket(packet: RawPacket): void {
// wait until play state
function writeDownstreamPacket(packet: RawPacket): void {
// queue until play state
if (client.state !== states.PLAY) {
clientQueue.push(packet);
downstreamQueue.push(packet);
return;
}
@ -110,10 +102,10 @@ export default async function (instance: Instance): Promise<void> {
client.write(packet.name, packet.data);
}
function writeServerPacket(packet: RawPacket): void {
// wait until play state
function writeUpstreamPacket(packet: RawPacket): void {
// queue until play state
if (server.state !== states.PLAY) {
serverQueue.push(packet);
upstreamQueue.push(packet);
return;
}

View file

@ -1,6 +1,4 @@
import type { States } from 'minecraft-protocol';
import { states } from 'minecraft-protocol';
import type { EventMap } from '../../util/events.js';
import { PublicEventHandler } from '../../util/events.js';
import { Packet, type RawPacket } from '../../util/packet.js';
import { type AsyncVoid } from '../../util/types.js';
@ -19,51 +17,39 @@ function write(direction: Direction, packet: RawPacket): void {
});
}
export const proxy = {
client: new PublicEventHandler<PacketEventMap>(),
server: new PublicEventHandler<PacketEventMap>(),
class ConnectionSide<
TEventMap extends EventMap<TEventMap>,
> extends PublicEventHandler<TEventMap> {
constructor(protected readonly direction: Direction) {
super();
}
writeDownstream(
name: string,
data: unknown,
state: States = states.PLAY,
): void {
write('downstream', { name, data, state });
},
writeUpstream(
name: string,
data: unknown,
state: States = states.PLAY,
): void {
write('upstream', { name, data, state });
},
public write(packet: Packet): void {
write(this.direction, packet);
}
}
// ngl I feel like I need rx soo bad
// then I could just do
// channel.filter(({ direction }) => direction === 'downstream')
// and it would just work
export const proxy = {
upstream: new ConnectionSide<PacketEventMap>('upstream'),
downstream: new ConnectionSide<PacketEventMap>('downstream'),
} as const;
channel.subscribe(({ direction, packet: raw }: Message) => {
void (async () => {
const sourceHandler = {
downstream: proxy.server,
upstream: proxy.client,
}[direction];
const connection = proxy[direction];
const packet = new Packet(raw.name, raw.data);
const packet = new Packet(raw.name, raw.data, raw.state);
await sourceHandler.emit('packet', packet);
await sourceHandler.emit(packet.name, packet);
await connection.emit('packet', packet);
await connection.emit(packet.name, packet);
if (packet.canceled) return;
switch (direction) {
case 'downstream':
direction = 'upstream';
break;
case 'upstream':
direction = 'downstream';
break;
default:
throw new Error(`Invalid direction: ${direction as any}`);
}
// Forward packet
channel.write({
direction,

View file

@ -6,14 +6,11 @@ export interface RawPacket extends PacketMeta {
}
export class Packet<Data = unknown> {
public name;
public state: States = states.PLAY;
public data;
public canceled: boolean = false;
constructor(name: string, data: Data) {
this.name = name;
this.data = data;
}
constructor(
public name: string,
public data: Data,
public state: States = states.PLAY,
) {}
}