fix: break packet bridge cycle (fix for https://github.com/microsoft/jacdac/issues/935)
This commit is contained in:
Родитель
819eacf49e
Коммит
e65d2c74d9
|
@ -14,6 +14,7 @@
|
||||||
"build": "microbundle --no-compress",
|
"build": "microbundle --no-compress",
|
||||||
"watch": "microbundle watch",
|
"watch": "microbundle watch",
|
||||||
"devtools": "yarn build && node ./dist/cli.js devtools",
|
"devtools": "yarn build && node ./dist/cli.js devtools",
|
||||||
|
"devtools:serial": "yarn build && node ./dist/cli.js devtools --serial",
|
||||||
"devtools:usb": "yarn build && node ./dist/cli.js devtools --usb",
|
"devtools:usb": "yarn build && node ./dist/cli.js devtools --usb",
|
||||||
"test:usb": "yarn build && node ./dist/cli.js stream --usb --packets --sensors",
|
"test:usb": "yarn build && node ./dist/cli.js stream --usb --packets --sensors",
|
||||||
"publish:pi": "yarn build && scp -r ../jacdac-ts/dist/* pi@192.168.0.131:/home/pi/gh/jacdac-cli/node_modules/jacdac-ts/dist && scp -r ./dist/* pi@192.168.0.131:/home/pi/gh/jacdac-cli/dist"
|
"publish:pi": "yarn build && scp -r ../jacdac-ts/dist/* pi@192.168.0.131:/home/pi/gh/jacdac-cli/node_modules/jacdac-ts/dist && scp -r ./dist/* pi@192.168.0.131:/home/pi/gh/jacdac-cli/dist"
|
||||||
|
@ -38,7 +39,7 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"commander": "^9.0.0",
|
"commander": "^9.0.0",
|
||||||
"faye-websocket": "^0.11.4",
|
"faye-websocket": "^0.11.4",
|
||||||
"jacdac-ts": "^1.24.16"
|
"jacdac-ts": "^1.24.19"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@semantic-release/exec": "^6.0.3",
|
"@semantic-release/exec": "^6.0.3",
|
||||||
|
|
|
@ -7,9 +7,11 @@ import {
|
||||||
printPacket,
|
printPacket,
|
||||||
serializeToTrace,
|
serializeToTrace,
|
||||||
createProxyBridge,
|
createProxyBridge,
|
||||||
|
randomDeviceId,
|
||||||
} from "jacdac-ts"
|
} from "jacdac-ts"
|
||||||
import { createTransports, TransportsOptions } from "./transports"
|
import { createTransports, TransportsOptions } from "./transports"
|
||||||
|
|
||||||
|
const SENDER_FIELD = "__jacdac_sender"
|
||||||
/* eslint-disable @typescript-eslint/no-var-requires */
|
/* eslint-disable @typescript-eslint/no-var-requires */
|
||||||
const WebSocket = require("faye-websocket")
|
const WebSocket = require("faye-websocket")
|
||||||
const http = require("http")
|
const http = require("http")
|
||||||
|
@ -77,9 +79,12 @@ export async function devToolsCommand(
|
||||||
})
|
})
|
||||||
|
|
||||||
// passive bus to sniff packets
|
// passive bus to sniff packets
|
||||||
const bridge = createProxyBridge(data =>
|
const bridge = createProxyBridge((data, sender) => {
|
||||||
clients.forEach(c => c.send(Buffer.from(data)))
|
//console.log(sender)
|
||||||
)
|
clients
|
||||||
|
.filter(c => c[SENDER_FIELD] !== sender)
|
||||||
|
.forEach(c => c.send(Buffer.from(data)))
|
||||||
|
})
|
||||||
const transports = createTransports(options)
|
const transports = createTransports(options)
|
||||||
const bus = new JDBus(transports, {
|
const bus = new JDBus(transports, {
|
||||||
client: false,
|
client: false,
|
||||||
|
@ -103,9 +108,11 @@ export async function devToolsCommand(
|
||||||
// is this a socket?
|
// is this a socket?
|
||||||
if (WebSocket.isWebSocket(request)) {
|
if (WebSocket.isWebSocket(request)) {
|
||||||
const client = new WebSocket(request, socket, body)
|
const client = new WebSocket(request, socket, body)
|
||||||
const sender = Math.random() + ""
|
const sender = "ws" + randomDeviceId()
|
||||||
|
// store sender id to deduped packet
|
||||||
|
client[SENDER_FIELD] = sender
|
||||||
clients.push(client)
|
clients.push(client)
|
||||||
log(`client: connected (${clients.length} clients)`)
|
log(`client: connected (${sender}, ${clients.length} clients)`)
|
||||||
client.on("message", event => {
|
client.on("message", event => {
|
||||||
const { data } = event
|
const { data } = event
|
||||||
clients.filter(c => c !== client).forEach(c => c.send(data))
|
clients.filter(c => c !== client).forEach(c => c.send(data))
|
||||||
|
@ -117,7 +124,8 @@ export async function devToolsCommand(
|
||||||
})
|
})
|
||||||
|
|
||||||
const tcpServer = net.createServer(client => {
|
const tcpServer = net.createServer(client => {
|
||||||
const sender = Math.random() + ""
|
const sender = "tcp" + randomDeviceId()
|
||||||
|
client[SENDER_FIELD] = sender
|
||||||
client.send = (pkt0: Buffer) => {
|
client.send = (pkt0: Buffer) => {
|
||||||
const pkt = new Uint8Array(pkt0)
|
const pkt = new Uint8Array(pkt0)
|
||||||
const b = new Uint8Array(1 + pkt.length)
|
const b = new Uint8Array(1 + pkt.length)
|
||||||
|
@ -132,7 +140,7 @@ export async function devToolsCommand(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
clients.push(client)
|
clients.push(client)
|
||||||
log(`client: connected (${clients.length} clients)`)
|
log(`client: connected (${sender} ${clients.length} clients)`)
|
||||||
let acc: Uint8Array
|
let acc: Uint8Array
|
||||||
client.on("data", (buf: Uint8Array) => {
|
client.on("data", (buf: Uint8Array) => {
|
||||||
if (acc) {
|
if (acc) {
|
||||||
|
|
|
@ -4024,10 +4024,10 @@ issue-parser@^6.0.0:
|
||||||
lodash.isstring "^4.0.1"
|
lodash.isstring "^4.0.1"
|
||||||
lodash.uniqby "^4.7.0"
|
lodash.uniqby "^4.7.0"
|
||||||
|
|
||||||
jacdac-ts@^1.24.16:
|
jacdac-ts@^1.24.19:
|
||||||
version "1.24.16"
|
version "1.24.19"
|
||||||
resolved "https://registry.yarnpkg.com/jacdac-ts/-/jacdac-ts-1.24.16.tgz#e0fe156b7f84ca1dc9e384b6e457ee8c5d0454a7"
|
resolved "https://registry.yarnpkg.com/jacdac-ts/-/jacdac-ts-1.24.19.tgz#c55883aa4af0d73db2a7ef3a4035bc4fa4a1fc15"
|
||||||
integrity sha512-h6flgQf5hCV+uzwARv9CxMBZ4xosvOqfSkR6Q0cPcjcO209idk23YIRBrgDU4SlZG9EX6FgCHzEZ/KvlCHZzRA==
|
integrity sha512-wr9jzuPILKTIP2tV0CuNxCehHd4s89mohC4cmp4FXyesp4HpJ4XY8zCRZvwz8pG2Cd4GlvfDTj4PECUlKAWBrA==
|
||||||
dependencies:
|
dependencies:
|
||||||
"@types/node" "^17.0.17"
|
"@types/node" "^17.0.17"
|
||||||
"@types/w3c-web-serial" "^1.0.2"
|
"@types/w3c-web-serial" "^1.0.2"
|
||||||
|
|
Загрузка…
Ссылка в новой задаче