Skip to content

Commit

Permalink
feat(lib/[udp|system].js): added types, a bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lostrepo committed Jun 29, 2024
1 parent cb6651f commit 6faf8e5
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 17 deletions.
29 changes: 21 additions & 8 deletions lib/system.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { system } = lo.load('system')
// const { system } = lo.load('system')

const { cstr, ptr } = lo
const { sysconf, _SC_CLK_TCK } = system

//const { wrap } = lo
// const { sysconf, _SC_CLK_TCK } = system

/** @param {string[]} args */
function make_args (args) {
const argb = new Array(args.length)
if (!args.length) return { args: new Uint8Array(0) }
Expand All @@ -22,6 +21,7 @@ function make_args (args) {

function wrapStrError () {
const eb = new Uint8Array(1024)
const decoder = new TextDecoder()
const { strerror_r } = system
system.strerror = (errnum = lo.errno) => {
const rc = strerror_r(errnum, eb, 1024)
Expand All @@ -39,6 +39,7 @@ function wrapCPUTime () {
}

class SystemError extends Error {
/** @param {string} name */
constructor (name) {
const { errno } = lo
super(`${name} ${system.strerror(errno)}`)
Expand All @@ -49,13 +50,25 @@ class SystemError extends Error {

/*
const sys_assert = (...args) => {
return lo.assert(...[...args, err => console.log(`errno ${lo.errno}\n${strerror()}\n${err.stack}`)])
return lo.assert(...[...args, err => console.log(`errno ${lo.errno}\n${strerror()}\n${err.stack}`)])
}
*/

system.clock_ticks_per_second = sysconf(_SC_CLK_TCK)
const decoder = new TextDecoder()
system.SystemError = SystemError
const system = (() => {
const strerror = (_ = 0) => _ == 0 ? void 0 : `${_}: ${''}`
const cputime = () => new Uint32Array(0)
const system = lo.load('system').system
const clock_ticks_per_second = system.sysconf(system._SC_CLK_TCK)

return Object.assign({}, system, {
clock_ticks_per_second,
SystemError,
// type placeholders
strerror,
cputime
})
})()

wrapStrError()
wrapCPUTime()

Expand Down
52 changes: 43 additions & 9 deletions lib/udp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { net } from 'lib/net.js'

//const { strerror } = system
const { assert, ptr, core } = lo
const {
const {
sendmsg, recvmsg, connect, listen,
AF_INET, SOCK_DGRAM, SOCK_NONBLOCK, SOCKADDR_LEN, EINPROGRESS
AF_INET, SOCK_DGRAM, SOCK_NONBLOCK, SOCKADDR_LEN
} = net
const { socket, bind, close } = net
const { sockaddr_in } = net.types
Expand All @@ -19,13 +19,18 @@ class IoVec {
this.view = new DataView(this.raw.buffer)
}

set payload (u8) {
if (u8 === this.u8) return
/** @param {Uint8Array & Partial<Ptr<Uint8Array>>} _u8 */
set payload (_u8) {
if (_u8 === this.u8) return
const u8 = /**@type {Ptr<Uint8Array>}*/(
_u8.ptr ? _u8 : ptr(_u8)
)
this.u8 = u8.ptr ? u8 : ptr(u8)
this.view.setBigUint64(0, BigInt(u8.ptr), true)
this.view.setUint32(8, u8.length, true)
}

/** @param {number} len */
set length (len) {
this.view.setUint32(8, len, true)
}
Expand All @@ -38,13 +43,18 @@ class MsgHdr {
this.view = new DataView(this.raw.buffer)
}

set address (sockaddr) {
if (sockaddr === this.sockaddr) return
this.sockaddr = sockaddr.ptr ? sockaddr : ptr(sockaddr)
/** @param {Uint8Array & Partial<Ptr<Uint8Array>>} _sockaddr */
set address (_sockaddr) {
if (_sockaddr === this.sockaddr) return
const sockaddr = /**@type {Ptr<Uint8Array>}*/(
_sockaddr.ptr ? _sockaddr : ptr(_sockaddr)
)
this.sockaddr = sockaddr
this.view.setBigUint64(0, BigInt(sockaddr.ptr), true)
this.view.setUint32(8, sockaddr.length, true)
}

/** @param {IoVec} iov */
set payload (iov) {
if (iov === this.iov) return
this.iov = iov
Expand All @@ -57,24 +67,34 @@ class MsgHdr {
this.view.setUint32(40, u8.length, true)
}
*/
/** @param {number} flags */
set flags (flags) {
this.view.setUint32(48, flags, true)
}
}

class Node {
/** @type {import('lib/loop.d.ts').bindings['Loop'] | undefined} */
loop = undefined
fd = -1
msghdr = new MsgHdr()
iov = new IoVec()
#port = 0
address = '127.0.0.1'
/** @type {ReturnType<typeof sockaddr_in> | undefined} */
peer_address = undefined

/**
* @param {Node['loop']} loop
*/
constructor (loop) {
if (loop) this.loop = loop
}

/**
* @param {string} address
* @param {(fd: number, events: number) => void} on_readable
*/
bind (address, on_readable, port = 0, on_error = noop) {
assert(this.fd === -1)
const { loop } = this
Expand All @@ -93,6 +113,10 @@ class Node {
if (!loop) lo.nextTick(on_readable)
}

/**
* @param {string} address
* @param {number} port
*/
connect (address, port) {
return connect(this.fd, sockaddr_in(address, port), SOCKADDR_LEN)
}
Expand All @@ -101,21 +125,31 @@ class Node {
assert(listen(this.fd, 128) === 0)
}

/**
* @param {IoVec['payload']} u8
*/
recv (u8, flags = 0) {
this.iov.payload = u8
this.iov.length = u8.length
return recvmsg(this.fd, this.msghdr.raw, flags)
}

/**
* @param {IoVec['payload']} u8
*/
send (u8, size = u8.length, flags = 0) {
this.iov.payload = u8
this.iov.length = size
return sendmsg(this.fd, this.msghdr.raw, flags)
}

/**
* @param {string} address
* @param {number} port
*/
peer (address, port) {
this.peer_address = sockaddr_in(address, port)
this.msghdr.address = this.peer_address
this.msghdr.address = /**@type {MsgHdr['address']}*/(this.peer_address)
}

close () {
Expand All @@ -129,7 +163,7 @@ class Node {
const src = sockaddr_in(this.address, this.#port)
net.get_sockname(this.fd, src)
this.#port = (new DataView(src.buffer)).getUint16(2, true)
return this.#port
return this.#port
}
}

Expand Down

0 comments on commit 6faf8e5

Please sign in to comment.