add test file for udp but not work
This commit is contained in:
parent
35b6fa4a16
commit
5f872e8287
|
@ -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")
|
||||
}
|
||||
})
|
|
@ -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 = {
|
||||
address: string,
|
||||
data: (
|
||||
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<T extends UDPBodyType = UDPBodyType> = {
|
||||
address: string,
|
||||
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<number> {
|
||||
return udpServer.run(null, { name: "port" })
|
||||
export namespace udpServer {
|
||||
|
||||
export async function port(): Promise<number> {
|
||||
return udpServerPool.run(null, { name: "port" })
|
||||
}
|
||||
|
||||
export async function lastestData(address: string): Promise<Option<UDPDataType<udp.Data>>> {
|
||||
return udpServerPool.run(address, { name: "lastestData" })
|
||||
}
|
||||
|
||||
export async function oldestData(address: string): Promise<Option<UDPDataType<udp.Data>>> {
|
||||
return udpServerPool.run(address, { name: "oldestData" })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
async function uploadBitStream(): Promise<boolean> {
|
||||
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<boolean> {
|
||||
return udpClientsPool.run({
|
||||
body: data,
|
||||
port: port,
|
||||
address: address
|
||||
} as UDPDataType<string>, { name: "sendString" })
|
||||
}
|
||||
|
||||
export async function sendUint8Array(data: Uint8Array, port: number, address: string): Promise<boolean> {
|
||||
return udpClientsPool.run({
|
||||
body: data,
|
||||
port: port,
|
||||
address: address
|
||||
} as UDPDataType<Uint8Array>, { name: "sendUint8Array" })
|
||||
}
|
||||
|
||||
export async function sendBunData(data: udp.Data, port: number, address: string): Promise<boolean> {
|
||||
return udpClientsPool.run({
|
||||
body: data,
|
||||
port: port,
|
||||
address: address
|
||||
} as UDPDataType<udp.Data>, { name: "sendUint8Array" })
|
||||
}
|
||||
}
|
||||
|
||||
export { udpServerPort, uploadBitStream }
|
||||
export type { UDPDataType }
|
||||
|
|
|
@ -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<string>): boolean {
|
||||
return udpClient.send(data.body, data.port, data.address)
|
||||
}
|
||||
|
||||
export function sendUint8Array(data: UDPDataType<Uint8Array>): boolean {
|
||||
return udpClient.send(data.body, data.port, data.address)
|
||||
}
|
||||
|
||||
export function sendBunData(data: UDPDataType<udp.Data>): boolean {
|
||||
return udpClient.send(data.body, data.port, data.address)
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<string, Array<{
|
||||
data: udp.Data,
|
||||
body: udp.Data,
|
||||
port: number,
|
||||
date: string
|
||||
}>> = 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<UDPDataType> {
|
||||
function lastestData(address: string): Option<UDPDataType<udp.Data>> {
|
||||
if (!z.string().ip().safeParse(address).success) {
|
||||
return None
|
||||
}
|
||||
|
@ -66,13 +64,13 @@ function lastestData(address: string): Option<UDPDataType> {
|
|||
|
||||
return Some({
|
||||
address: address,
|
||||
data: data.data,
|
||||
body: data.body,
|
||||
port: data.port,
|
||||
date: data.date,
|
||||
})
|
||||
}
|
||||
|
||||
function oldestData(address: string): Option<UDPDataType> {
|
||||
function oldestData(address: string): Option<UDPDataType<udp.Data>> {
|
||||
if (!z.string().ip().safeParse(address).success) {
|
||||
return None
|
||||
}
|
||||
|
@ -89,7 +87,7 @@ function oldestData(address: string): Option<UDPDataType> {
|
|||
|
||||
return Some({
|
||||
address: address,
|
||||
data: data.data,
|
||||
body: data.body,
|
||||
port: data.port,
|
||||
date: data.date,
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue