use tinypool

This commit is contained in:
SikongJueluo 2025-03-26 21:22:44 +08:00
parent 67439780ed
commit 27b69c5d0d
No known key found for this signature in database
2 changed files with 27 additions and 20 deletions

View File

@ -2,5 +2,10 @@ import { resolve } from "path";
import Tinypool from "tinypool";
const udpClientsPool = new Tinypool({
filename: resolve(__dirname, "./udpClient.ts")
filename: resolve(__dirname, "./udpClient.ts"),
workerData: {}
})
export function send() {
udpClientsPool.run({}, { name: "send" })
}

View File

@ -1,33 +1,35 @@
import { parentPort } from "worker_threads"
import { MsgProtocol } from "./msgProtocol"
import _ from "lodash"
import { type udp } from "bun"
declare var self: Worker
export type UDPServerMsgType = "port"
interface BinaryTypeList {
arraybuffer: ArrayBuffer;
buffer: Buffer;
uint8array: Uint8Array;
// TODO: DataView
// dataview: DataView;
}
type BinaryType = keyof BinaryTypeList;
const udpServer = await Bun.udpSocket({
port: 33000,
socket: {
data(_socket, _buf, _port, _addr) {
data(
_socket: udp.Socket<BinaryType>,
data: BinaryTypeList[BinaryType],
port: number,
address: string,
) {
// todo : Handle Recieved Data
}
}
})
parentPort?.on("message", (msg: MsgProtocol.MessageQuery) => {
if (MsgProtocol.isMessageQuery(msg)) {
switch (msg.command) {
case "port": {
postMessage(MsgProtocol.genMessageResult(udpServer.port, msg))
break
}
default: {
break
}
}
} else {
return
}
})
postMessage("UDP Server Start Successfully!")
export function udpServerPort(): number {
return udpServer.port
}