Skip to content

Commit

Permalink
chore: add SOCKET_UDP_CHECK_SEND
Browse files Browse the repository at this point in the history
  • Loading branch information
manekinekko committed Dec 12, 2023
1 parent 594eefc commit cbb1bfd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/preview2-shim/lib/io/calls.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const SOCKET_UDP_CREATE_HANDLE = ++call_id << 24;
export const SOCKET_UDP_BIND = ++call_id << 24;
export const SOCKET_UDP_CONNECT = ++call_id << 24;
export const SOCKET_UDP_DISCONNECT = ++call_id << 24;
export const SOCKET_UDP_CHECK_SEND = ++call_id << 24;
export const SOCKET_UDP_SEND = ++call_id << 24;
export const SOCKET_UDP_RECEIVE = ++call_id << 24;
export const SOCKET_UDP_DISPOSE = ++call_id << 24;
Expand Down
10 changes: 10 additions & 0 deletions packages/preview2-shim/lib/io/worker-thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
SOCKET_RESOLVE_ADDRESS_DISPOSE_REQUEST,
SOCKET_RESOLVE_ADDRESS_GET_AND_DISPOSE_REQUEST,
SOCKET_UDP_BIND,
SOCKET_UDP_CHECK_SEND,
SOCKET_UDP_CONNECT,
SOCKET_UDP_CREATE_HANDLE,
SOCKET_UDP_DISCONNECT,
Expand Down Expand Up @@ -316,6 +317,15 @@ function handle(call, id, payload) {
});
}

case SOCKET_UDP_CHECK_SEND: {
const socket = getSocketOrThrow(id);
try {
return socket.getSendBufferSize() - socket.getSendQueueSize();
} catch (err) {
return err.errno;
}
}

case SOCKET_UDP_SEND: {
let { remoteHost, remotePort, data } = payload;
const socket = getSocketOrThrow(id);
Expand Down
15 changes: 7 additions & 8 deletions packages/preview2-shim/lib/nodejs/sockets/udp-socket-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { isIP } from "node:net";
import { assert } from "../../common/assert.js";
import {
SOCKET_UDP_BIND,
SOCKET_UDP_CHECK_SEND,
SOCKET_UDP_CONNECT,
SOCKET_UDP_CREATE_HANDLE,
SOCKET_UDP_DISCONNECT,
Expand Down Expand Up @@ -129,14 +130,13 @@ export class OutgoingDatagramStream {
/**
*
* @returns {bigint}
* @throws {invalid-state} The socket is not bound to any local address. (EINVAL)
*/
checkSend() {
// TODO: implement the actual check
// socket.getSendBufferSize() - socket.getSendQueueSize() <= 0 --> throw
// attach an event listener to the socket to listen for the drain event
// untill the next sent completes
return 1024n;
const ret = ioCall(SOCKET_UDP_CHECK_SEND, this.#socketId);
// TODO: When this function returns ok(0), the `subscribe` pollable will
// become ready when this function will report at least ok(1), or an
// error.
return ret;
}

/**
Expand Down Expand Up @@ -179,8 +179,7 @@ export class OutgoingDatagramStream {
);
if (ret === 0) {
datagramsSent++;
}
else {
} else {
assert(ret === -65, "remote-unreachable");
}
}
Expand Down

0 comments on commit cbb1bfd

Please sign in to comment.