Skip to content
This repository was archived by the owner on Jul 3, 2023. It is now read-only.
Merged
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
9 changes: 9 additions & 0 deletions src/client/network/network.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NUClearNetSend } from 'nuclearnet.js'

import { NUsightNetwork } from './nusight_network'
import { MessageType } from './nusight_network'
import { MessageCallback } from './nusight_network'
Expand Down Expand Up @@ -44,4 +46,11 @@ export class Network {
}
this.offNUClearMessages.clear()
}

/**
* Send the given message on the network
*/
send(opts: NUClearNetSend): void {
this.nusightNetwork.send(opts)
}
}
5 changes: 5 additions & 0 deletions src/client/network/nusight_network.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NUClearNetPacket } from 'nuclearnet.js'
import { NUClearNetOptions } from 'nuclearnet.js'
import { NUClearNetPeer } from 'nuclearnet.js'
import { NUClearNetSend } from 'nuclearnet.js'

import { NUClearNetClient } from '../../shared/nuclearnet/nuclearnet_client'
import { AppModel } from '../components/app/model'
Expand Down Expand Up @@ -30,6 +31,10 @@ export class NUsightNetwork {
return this.nuclearnetClient.connect(opts)
}

send(opts: NUClearNetSend) {
this.nuclearnetClient.send(opts)
}

onNUClearMessage<T>(messageType: MessageType<T>, cb: MessageCallback<T>) {
const messageTypeName = this.messageTypePath.getPath(messageType)
return this.nuclearnetClient.on(messageTypeName, (packet: NUClearNetPacket) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ describe('WebSocketProxyNUClearNetClient', () => {
payload: Buffer.alloc(8),
}
client.send(opts)
expect(mockWebSocket.send).toHaveBeenCalledWith('foo', opts)
expect(mockWebSocket.send).toHaveBeenCalledWith('packet', opts)
})
})
})
4 changes: 1 addition & 3 deletions src/client/nuclearnet/web_socket_proxy_nuclearnet_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ export class WebSocketProxyNUClearNetClient implements NUClearNetClient {
}

send(options: NUClearNetSend): void {
if (typeof options.type === 'string') {
this.socket.send(options.type, options)
}
this.socket.send('packet', options)
}

private onReconnect = (options: NUClearNetOptions) => {
Expand Down
10 changes: 8 additions & 2 deletions src/server/nuclearnet/web_socket_proxy_nuclearnet_server.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NUClearNetSend } from 'nuclearnet.js'
import { NUClearNetOptions } from 'nuclearnet.js'
import { NUClearNetPeer } from 'nuclearnet.js'
import { NUClearNetPacket } from 'nuclearnet.js'
Expand Down Expand Up @@ -52,6 +53,7 @@ class WebSocketServerClient {
this.offLeave = this.nuclearnetClient.onLeave(this.onLeave)
this.offListenMap = new Map()

this.socket.on('packet', this.onClientPacket)
this.socket.on('listen', this.onListen)
this.socket.on('unlisten', this.onUnlisten)
this.socket.on('nuclear_connect', this.onConnect)
Expand Down Expand Up @@ -85,7 +87,7 @@ class WebSocketServerClient {
}

private onListen = (event: string, requestToken: string) => {
const off = this.nuclearnetClient.on(event, this.onPacket.bind(this, event))
const off = this.nuclearnetClient.on(event, this.onServerPacket.bind(this, event))
this.offListenMap.set(requestToken, off)
}

Expand All @@ -101,9 +103,13 @@ class WebSocketServerClient {
this.offLeave()
}

private onPacket = (event: string, packet: NUClearNetPacket) => {
private onServerPacket = (event: string, packet: NUClearNetPacket) => {
this.processor.onPacket(event, packet)
}

private onClientPacket = (options: NUClearNetSend) => {
this.nuclearnetClient.send(options)
}
}

class PacketProcessor {
Expand Down
29 changes: 24 additions & 5 deletions src/shared/nuclearnet/nuclearnet_proxy_parser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Emitter from 'component-emitter'
import { NUClearNetPacket } from 'nuclearnet.js'
import { NUClearNetPeer } from 'nuclearnet.js'
import { NUClearNetSend } from 'nuclearnet.js'

import { Packet } from './nuclearnet_proxy_parser_socketio'
import { TYPES } from './nuclearnet_proxy_parser_socketio'
Expand Down Expand Up @@ -35,8 +36,16 @@ export class Encoder {
case 'unlisten':
return callback([JSON.stringify(packet)])

case 'packet': {
const { id, data: [key, { target, type, payload, reliable }] } = packet
return callback([
JSON.stringify({ id, nsp, key, header: { target, type, reliable } }),
Copy link
Member

@TrentHouliston TrentHouliston Feb 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're sending type as a string here, and adding a hashType below that isn't a part of a NUClearNetSend packet.
The decoding logic below will take that and add a .hash element to it afterwards that'll be ignored, so the hashType(type) here is doing nothing.
However, NUClearNetSend does accept a buffer or a string here, so maybe instead don't send the type in json and in the parser below add it as the type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the decoder to not add the unused hash.

payload,
])
}

// For NUClearNet packets, we send the payload separately to avoid array slicing later
default:
default: {
const { id, data: [key, { peer, hash, payload, reliable }] } = packet

// Send the header as a JSON and then the payload as binary
Expand All @@ -45,6 +54,7 @@ export class Encoder {
hash,
payload,
])
}
}
default:
return callback([JSON.stringify(packet)])
Expand All @@ -58,7 +68,7 @@ export class Decoder extends Emitter {
private nuclearPacket?: {
nsp: string,
type: TYPES.EVENT,
data: [string, Partial<NUClearNetPacket>],
data: [string, Partial<NUClearNetPacket> | Partial<NUClearNetSend>],
id: number
}

Expand All @@ -85,10 +95,19 @@ export class Decoder extends Emitter {
}
} else {
switch (this.state) {
// State 1 means we are getting a hash
// State 1 means we are getting a payload or hash
case 1:
this.nuclearPacket!.data[1].hash = obj
this.state = 2
if (this.nuclearPacket!.data[0] === 'packet') {
// 'packet' messages are sent using NUClearNetSend which doesn't have a hash,
// so here we get the payload and emit
this.nuclearPacket!.data[1].payload = obj
this.emit('decoded', this.nuclearPacket)
this.state = 0
} else {
// For NUClearNetPackets we get a hash in state 1 and move to state 2 for the payload
(this.nuclearPacket!.data[1] as NUClearNetPacket).hash = obj
this.state = 2
}
break

// State 2 means we are getting a packet
Expand Down