From 5f872e8287f8e35c774e56d5cafafe0fddab5bf3 Mon Sep 17 00:00:00 2001 From: SikongJueluo Date: Fri, 28 Mar 2025 21:20:03 +0800 Subject: [PATCH] add test file for udp but not work --- server/udp.test.ts | 22 +++++++++++++ server/udp.ts | 74 +++++++++++++++++++++++++++++++++---------- server/udpClient.ts | 13 ++++++-- server/udpProtocol.ts | 17 ++++++++++ server/udpServer.ts | 16 ++++------ 5 files changed, 114 insertions(+), 28 deletions(-) create mode 100644 server/udp.test.ts diff --git a/server/udp.test.ts b/server/udp.test.ts new file mode 100644 index 0000000..690b7d0 --- /dev/null +++ b/server/udp.test.ts @@ -0,0 +1,22 @@ +import { expect, test } from "bun:test" +import { udpServer, udpClient } from "./udp" + +const udpServerAddr = "127.0.0.1" +const udpServerPort = await udpServer.port() + +console.info("Ready to Test UDP") + +test("Test Send String", async () => { + const str = "Hello My Server" + const retSend = await udpClient.sendString(str, udpServerPort, udpServerAddr) + expect(retSend).toBe(true) + + const retReceive = await udpServer.lastestData(udpServerAddr) + if (retReceive.isSome()) { + const data = retReceive.value + expect(data.body).toBe(str) + expect(data.address).toBe(udpServerAddr) + } else { + expect().fail("Not Receive Anything") + } +}) diff --git a/server/udp.ts b/server/udp.ts index 33c67a4..7c1fc81 100644 --- a/server/udp.ts +++ b/server/udp.ts @@ -1,40 +1,80 @@ import { resolve } from "path"; import Tinypool from "tinypool"; import type { UDPReceiveProcotol, UDPSendProtocol } from "./udpProtocol"; +import type { udp } from "bun"; +import { Option } from "ts-results-es"; -type UDPDataType = { +const SERVER_PATH = resolve(__dirname, "./udpServer.ts") +const CLIENT_PATH = resolve(__dirname, "./udpClient.ts") + +export type UDPBodyType = ( + udp.Data | + UDPSendProtocol.CmdPackage | + UDPSendProtocol.DataPackage | + UDPReceiveProcotol.ReadPackage | + UDPReceiveProcotol.WritePackage +) + +type UDPDataType = { address: string, - data: ( - UDPSendProtocol.CmdPackage | - UDPSendProtocol.DataPackage | - UDPReceiveProcotol.ReadPackage | - UDPReceiveProcotol.WritePackage - ), + body: T, port: number, date?: string, } + const udpClientsPool = new Tinypool({ - filename: resolve(__dirname, "./udpClient.ts"), + filename: CLIENT_PATH, workerData: {} }) -const udpServer = new Tinypool({ - filename: resolve(__dirname, "./udpServer.ts"), +const udpServerPool = new Tinypool({ + filename: SERVER_PATH, workerData: {}, maxThreads: 1, minThreads: 1, }) -async function udpServerPort(): Promise { - return udpServer.run(null, { name: "port" }) +export namespace udpServer { + + export async function port(): Promise { + return udpServerPool.run(null, { name: "port" }) + } + + export async function lastestData(address: string): Promise>> { + return udpServerPool.run(address, { name: "lastestData" }) + } + + export async function oldestData(address: string): Promise>> { + return udpServerPool.run(address, { name: "oldestData" }) + } + } -async function uploadBitStream(): Promise { - let ret = await udpClientsPool.run({} as UDPDataType, { name: "send" }) +export namespace udpClient { - return ret + export async function sendString(data: string, port: number, address: string): Promise { + return udpClientsPool.run({ + body: data, + port: port, + address: address + } as UDPDataType, { name: "sendString" }) + } + + export async function sendUint8Array(data: Uint8Array, port: number, address: string): Promise { + return udpClientsPool.run({ + body: data, + port: port, + address: address + } as UDPDataType, { name: "sendUint8Array" }) + } + + export async function sendBunData(data: udp.Data, port: number, address: string): Promise { + return udpClientsPool.run({ + body: data, + port: port, + address: address + } as UDPDataType, { name: "sendUint8Array" }) + } } - -export { udpServerPort, uploadBitStream } export type { UDPDataType } diff --git a/server/udpClient.ts b/server/udpClient.ts index cf4cd1d..c59ff76 100644 --- a/server/udpClient.ts +++ b/server/udpClient.ts @@ -1,8 +1,17 @@ +import type { udp } from "bun" import type { UDPDataType } from "./udp" const udpClient = await Bun.udpSocket({}) -export function send(data: UDPDataType): boolean { - return udpClient.send(data.data.toUint8Array(), data.port, data.address) +export function sendString(data: UDPDataType): boolean { + return udpClient.send(data.body, data.port, data.address) +} + +export function sendUint8Array(data: UDPDataType): boolean { + return udpClient.send(data.body, data.port, data.address) +} + +export function sendBunData(data: UDPDataType): boolean { + return udpClient.send(data.body, data.port, data.address) } diff --git a/server/udpProtocol.ts b/server/udpProtocol.ts index 67b7aa3..3198958 100644 --- a/server/udpProtocol.ts +++ b/server/udpProtocol.ts @@ -100,6 +100,10 @@ export namespace UDPSendProtocol { } } + export function isCmdPackage(obj: any): obj is CmdPackage { + return obj instanceof CmdPackage + } + export class DataPackage { private ID: number = PKG_SIGN_DATA private _reserved: number = 0 @@ -120,6 +124,10 @@ export namespace UDPSendProtocol { } } + export function isDataPackage(obj: any): obj is DataPackage { + return obj instanceof DataPackage + } + } export namespace UDPReceiveProcotol { @@ -155,6 +163,10 @@ export namespace UDPReceiveProcotol { } } + export function isReadPackage(obj: any): obj is ReadPackage { + return obj instanceof ReadPackage + } + export class WritePackage { private ID: number = PKG_SIGN_WRITE private taskID: number = 0 @@ -181,4 +193,9 @@ export namespace UDPReceiveProcotol { ]) } } + + export function isWritePackage(obj: any): obj is WritePackage { + return obj instanceof WritePackage + } + } diff --git a/server/udpServer.ts b/server/udpServer.ts index fbec1e7..d9fe296 100644 --- a/server/udpServer.ts +++ b/server/udpServer.ts @@ -4,9 +4,7 @@ import type { UDPDataType } from "./udp"; import { None, Some, type Option } from "ts-results-es"; import { z } from "zod"; -declare var self: Worker -export type UDPServerMsgType = "port" - +// Bun Data Type interface BinaryTypeList { arraybuffer: ArrayBuffer; buffer: Buffer; @@ -15,7 +13,7 @@ interface BinaryTypeList { type BinaryType = keyof BinaryTypeList; const receivedData: Map> = new Map() @@ -36,7 +34,7 @@ const udpServer = await Bun.udpSocket({ } else { receivedData.set(address, []) arrayData.push({ - data: data, + body: data, port: port, date: new Date().toUTCString() }) @@ -49,7 +47,7 @@ function port(): number { return udpServer.port } -function lastestData(address: string): Option { +function lastestData(address: string): Option> { if (!z.string().ip().safeParse(address).success) { return None } @@ -66,13 +64,13 @@ function lastestData(address: string): Option { return Some({ address: address, - data: data.data, + body: data.body, port: data.port, date: data.date, }) } -function oldestData(address: string): Option { +function oldestData(address: string): Option> { if (!z.string().ip().safeParse(address).success) { return None } @@ -89,7 +87,7 @@ function oldestData(address: string): Option { return Some({ address: address, - data: data.data, + body: data.body, port: data.port, date: data.date, })