mirror of
https://github.com/denoland/deno.git
synced 2024-12-22 15:24:46 -05:00
fix: DatagramConn.send should return bytes sent (#6265)
This commit is contained in:
parent
f6fa659384
commit
77545219a6
5 changed files with 20 additions and 15 deletions
2
cli/js/lib.deno.unstable.d.ts
vendored
2
cli/js/lib.deno.unstable.d.ts
vendored
|
@ -988,7 +988,7 @@ declare namespace Deno {
|
||||||
/** UNSTABLE: new API, yet to be vetted.
|
/** UNSTABLE: new API, yet to be vetted.
|
||||||
*
|
*
|
||||||
* Sends a message to the target. */
|
* Sends a message to the target. */
|
||||||
send(p: Uint8Array, addr: Addr): Promise<void>;
|
send(p: Uint8Array, addr: Addr): Promise<number>;
|
||||||
/** UNSTABLE: new API, yet to be vetted.
|
/** UNSTABLE: new API, yet to be vetted.
|
||||||
*
|
*
|
||||||
* Close closes the socket. Any pending message promises will be rejected
|
* Close closes the socket. Any pending message promises will be rejected
|
||||||
|
|
|
@ -10,7 +10,7 @@ export { ShutdownMode, shutdown, NetAddr, UnixAddr } from "./ops/net.ts";
|
||||||
export interface DatagramConn extends AsyncIterable<[Uint8Array, Addr]> {
|
export interface DatagramConn extends AsyncIterable<[Uint8Array, Addr]> {
|
||||||
receive(p?: Uint8Array): Promise<[Uint8Array, Addr]>;
|
receive(p?: Uint8Array): Promise<[Uint8Array, Addr]>;
|
||||||
|
|
||||||
send(p: Uint8Array, addr: Addr): Promise<void>;
|
send(p: Uint8Array, addr: Addr): Promise<number>;
|
||||||
|
|
||||||
close(): void;
|
close(): void;
|
||||||
|
|
||||||
|
@ -109,11 +109,12 @@ export class DatagramImpl implements DatagramConn {
|
||||||
return [sub, remoteAddr];
|
return [sub, remoteAddr];
|
||||||
}
|
}
|
||||||
|
|
||||||
async send(p: Uint8Array, addr: Addr): Promise<void> {
|
async send(p: Uint8Array, addr: Addr): Promise<number> {
|
||||||
const remote = { hostname: "127.0.0.1", ...addr };
|
const remote = { hostname: "127.0.0.1", ...addr };
|
||||||
|
|
||||||
const args = { ...remote, rid: this.rid };
|
const args = { ...remote, rid: this.rid };
|
||||||
await netOps.send(args as netOps.SendRequest, p);
|
const byteLength = await netOps.send(args as netOps.SendRequest, p);
|
||||||
|
return byteLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
close(): void {
|
close(): void {
|
||||||
|
|
|
@ -73,7 +73,7 @@ export function receive(
|
||||||
transport: string,
|
transport: string,
|
||||||
zeroCopy: Uint8Array
|
zeroCopy: Uint8Array
|
||||||
): Promise<ReceiveResponse> {
|
): Promise<ReceiveResponse> {
|
||||||
return sendAsync("op_receive", { rid, transport }, zeroCopy);
|
return sendAsync("op_datagram_receive", { rid, transport }, zeroCopy);
|
||||||
}
|
}
|
||||||
|
|
||||||
export type SendRequest = {
|
export type SendRequest = {
|
||||||
|
@ -83,6 +83,7 @@ export type SendRequest = {
|
||||||
export async function send(
|
export async function send(
|
||||||
args: SendRequest,
|
args: SendRequest,
|
||||||
zeroCopy: Uint8Array
|
zeroCopy: Uint8Array
|
||||||
): Promise<void> {
|
): Promise<number> {
|
||||||
await sendAsync("op_send", args, zeroCopy);
|
const byteLength = await sendAsync("op_datagram_send", args, zeroCopy);
|
||||||
|
return byteLength;
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,8 +27,11 @@ pub fn init(i: &mut CoreIsolate, s: &State) {
|
||||||
i.register_op("op_connect", s.stateful_json_op2(op_connect));
|
i.register_op("op_connect", s.stateful_json_op2(op_connect));
|
||||||
i.register_op("op_shutdown", s.stateful_json_op2(op_shutdown));
|
i.register_op("op_shutdown", s.stateful_json_op2(op_shutdown));
|
||||||
i.register_op("op_listen", s.stateful_json_op2(op_listen));
|
i.register_op("op_listen", s.stateful_json_op2(op_listen));
|
||||||
i.register_op("op_receive", s.stateful_json_op2(op_receive));
|
i.register_op(
|
||||||
i.register_op("op_send", s.stateful_json_op2(op_send));
|
"op_datagram_receive",
|
||||||
|
s.stateful_json_op2(op_datagram_receive),
|
||||||
|
);
|
||||||
|
i.register_op("op_datagram_send", s.stateful_json_op2(op_datagram_send));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
|
@ -161,8 +164,7 @@ fn receive_udp(
|
||||||
Ok(JsonOp::Async(op.boxed_local()))
|
Ok(JsonOp::Async(op.boxed_local()))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(ry) Rename to op_datagram_receive
|
fn op_datagram_receive(
|
||||||
fn op_receive(
|
|
||||||
isolate_state: &mut CoreIsolateState,
|
isolate_state: &mut CoreIsolateState,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
|
@ -192,8 +194,7 @@ struct SendArgs {
|
||||||
transport_args: ArgsEnum,
|
transport_args: ArgsEnum,
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(ry) Rename to op_datagram_send
|
fn op_datagram_send(
|
||||||
fn op_send(
|
|
||||||
isolate_state: &mut CoreIsolateState,
|
isolate_state: &mut CoreIsolateState,
|
||||||
state: &State,
|
state: &State,
|
||||||
args: Value,
|
args: Value,
|
||||||
|
@ -222,7 +223,7 @@ fn op_send(
|
||||||
.socket
|
.socket
|
||||||
.poll_send_to(cx, &zero_copy, &addr)
|
.poll_send_to(cx, &zero_copy, &addr)
|
||||||
.map_err(OpError::from)
|
.map_err(OpError::from)
|
||||||
.map_ok(|_| json!({}))
|
.map_ok(|byte_length| json!(byte_length))
|
||||||
});
|
});
|
||||||
Ok(JsonOp::Async(f.boxed_local()))
|
Ok(JsonOp::Async(f.boxed_local()))
|
||||||
}
|
}
|
||||||
|
|
|
@ -240,7 +240,9 @@ unitTest(
|
||||||
assertEquals(bob.addr.hostname, "127.0.0.1");
|
assertEquals(bob.addr.hostname, "127.0.0.1");
|
||||||
|
|
||||||
const sent = new Uint8Array([1, 2, 3]);
|
const sent = new Uint8Array([1, 2, 3]);
|
||||||
await alice.send(sent, bob.addr);
|
const byteLength = await alice.send(sent, bob.addr);
|
||||||
|
|
||||||
|
assertEquals(byteLength, 3);
|
||||||
|
|
||||||
const [recvd, remote] = await bob.receive();
|
const [recvd, remote] = await bob.receive();
|
||||||
assert(remote.transport === "udp");
|
assert(remote.transport === "udp");
|
||||||
|
|
Loading…
Reference in a new issue