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 { resolve } from "path";
|
||||||
import Tinypool from "tinypool";
|
import Tinypool from "tinypool";
|
||||||
import type { UDPReceiveProcotol, UDPSendProtocol } from "./udpProtocol";
|
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<T extends UDPBodyType = UDPBodyType> = {
|
||||||
address: string,
|
address: string,
|
||||||
data: (
|
body: T,
|
||||||
UDPSendProtocol.CmdPackage |
|
|
||||||
UDPSendProtocol.DataPackage |
|
|
||||||
UDPReceiveProcotol.ReadPackage |
|
|
||||||
UDPReceiveProcotol.WritePackage
|
|
||||||
),
|
|
||||||
port: number,
|
port: number,
|
||||||
date?: string,
|
date?: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const udpClientsPool = new Tinypool({
|
const udpClientsPool = new Tinypool({
|
||||||
filename: resolve(__dirname, "./udpClient.ts"),
|
filename: CLIENT_PATH,
|
||||||
workerData: {}
|
workerData: {}
|
||||||
})
|
})
|
||||||
|
|
||||||
const udpServer = new Tinypool({
|
const udpServerPool = new Tinypool({
|
||||||
filename: resolve(__dirname, "./udpServer.ts"),
|
filename: SERVER_PATH,
|
||||||
workerData: {},
|
workerData: {},
|
||||||
maxThreads: 1,
|
maxThreads: 1,
|
||||||
minThreads: 1,
|
minThreads: 1,
|
||||||
})
|
})
|
||||||
|
|
||||||
async function udpServerPort(): Promise<number> {
|
export namespace udpServer {
|
||||||
return udpServer.run(null, { name: "port" })
|
|
||||||
|
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> {
|
export namespace udpClient {
|
||||||
let ret = await udpClientsPool.run({} as UDPDataType, { name: "send" })
|
|
||||||
|
|
||||||
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 }
|
export type { UDPDataType }
|
||||||
|
|
|
@ -1,8 +1,17 @@
|
||||||
|
import type { udp } from "bun"
|
||||||
import type { UDPDataType } from "./udp"
|
import type { UDPDataType } from "./udp"
|
||||||
|
|
||||||
const udpClient = await Bun.udpSocket({})
|
const udpClient = await Bun.udpSocket({})
|
||||||
|
|
||||||
export function send(data: UDPDataType): boolean {
|
export function sendString(data: UDPDataType<string>): boolean {
|
||||||
return udpClient.send(data.data.toUint8Array(), data.port, data.address)
|
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 {
|
export class DataPackage {
|
||||||
private ID: number = PKG_SIGN_DATA
|
private ID: number = PKG_SIGN_DATA
|
||||||
private _reserved: number = 0
|
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 {
|
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 {
|
export class WritePackage {
|
||||||
private ID: number = PKG_SIGN_WRITE
|
private ID: number = PKG_SIGN_WRITE
|
||||||
private taskID: number = 0
|
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 { None, Some, type Option } from "ts-results-es";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
|
|
||||||
declare var self: Worker
|
// Bun Data Type
|
||||||
export type UDPServerMsgType = "port"
|
|
||||||
|
|
||||||
interface BinaryTypeList {
|
interface BinaryTypeList {
|
||||||
arraybuffer: ArrayBuffer;
|
arraybuffer: ArrayBuffer;
|
||||||
buffer: Buffer;
|
buffer: Buffer;
|
||||||
|
@ -15,7 +13,7 @@ interface BinaryTypeList {
|
||||||
type BinaryType = keyof BinaryTypeList;
|
type BinaryType = keyof BinaryTypeList;
|
||||||
|
|
||||||
const receivedData: Map<string, Array<{
|
const receivedData: Map<string, Array<{
|
||||||
data: udp.Data,
|
body: udp.Data,
|
||||||
port: number,
|
port: number,
|
||||||
date: string
|
date: string
|
||||||
}>> = new Map()
|
}>> = new Map()
|
||||||
|
@ -36,7 +34,7 @@ const udpServer = await Bun.udpSocket({
|
||||||
} else {
|
} else {
|
||||||
receivedData.set(address, [])
|
receivedData.set(address, [])
|
||||||
arrayData.push({
|
arrayData.push({
|
||||||
data: data,
|
body: data,
|
||||||
port: port,
|
port: port,
|
||||||
date: new Date().toUTCString()
|
date: new Date().toUTCString()
|
||||||
})
|
})
|
||||||
|
@ -49,7 +47,7 @@ function port(): number {
|
||||||
return udpServer.port
|
return udpServer.port
|
||||||
}
|
}
|
||||||
|
|
||||||
function lastestData(address: string): Option<UDPDataType> {
|
function lastestData(address: string): Option<UDPDataType<udp.Data>> {
|
||||||
if (!z.string().ip().safeParse(address).success) {
|
if (!z.string().ip().safeParse(address).success) {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
@ -66,13 +64,13 @@ function lastestData(address: string): Option<UDPDataType> {
|
||||||
|
|
||||||
return Some({
|
return Some({
|
||||||
address: address,
|
address: address,
|
||||||
data: data.data,
|
body: data.body,
|
||||||
port: data.port,
|
port: data.port,
|
||||||
date: data.date,
|
date: data.date,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function oldestData(address: string): Option<UDPDataType> {
|
function oldestData(address: string): Option<UDPDataType<udp.Data>> {
|
||||||
if (!z.string().ip().safeParse(address).success) {
|
if (!z.string().ip().safeParse(address).success) {
|
||||||
return None
|
return None
|
||||||
}
|
}
|
||||||
|
@ -89,7 +87,7 @@ function oldestData(address: string): Option<UDPDataType> {
|
||||||
|
|
||||||
return Some({
|
return Some({
|
||||||
address: address,
|
address: address,
|
||||||
data: data.data,
|
body: data.body,
|
||||||
port: data.port,
|
port: data.port,
|
||||||
date: data.date,
|
date: data.date,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue