use tinypool
This commit is contained in:
		@@ -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" })
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user