mirror of
https://github.com/denoland/deno.git
synced 2024-12-21 23:04:45 -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.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* 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]> {
|
||||
receive(p?: Uint8Array): Promise<[Uint8Array, Addr]>;
|
||||
|
||||
send(p: Uint8Array, addr: Addr): Promise<void>;
|
||||
send(p: Uint8Array, addr: Addr): Promise<number>;
|
||||
|
||||
close(): void;
|
||||
|
||||
|
@ -109,11 +109,12 @@ export class DatagramImpl implements DatagramConn {
|
|||
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 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 {
|
||||
|
|
|
@ -73,7 +73,7 @@ export function receive(
|
|||
transport: string,
|
||||
zeroCopy: Uint8Array
|
||||
): Promise<ReceiveResponse> {
|
||||
return sendAsync("op_receive", { rid, transport }, zeroCopy);
|
||||
return sendAsync("op_datagram_receive", { rid, transport }, zeroCopy);
|
||||
}
|
||||
|
||||
export type SendRequest = {
|
||||
|
@ -83,6 +83,7 @@ export type SendRequest = {
|
|||
export async function send(
|
||||
args: SendRequest,
|
||||
zeroCopy: Uint8Array
|
||||
): Promise<void> {
|
||||
await sendAsync("op_send", args, zeroCopy);
|
||||
): Promise<number> {
|
||||
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_shutdown", s.stateful_json_op2(op_shutdown));
|
||||
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("op_send", s.stateful_json_op2(op_send));
|
||||
i.register_op(
|
||||
"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)]
|
||||
|
@ -161,8 +164,7 @@ fn receive_udp(
|
|||
Ok(JsonOp::Async(op.boxed_local()))
|
||||
}
|
||||
|
||||
// TODO(ry) Rename to op_datagram_receive
|
||||
fn op_receive(
|
||||
fn op_datagram_receive(
|
||||
isolate_state: &mut CoreIsolateState,
|
||||
state: &State,
|
||||
args: Value,
|
||||
|
@ -192,8 +194,7 @@ struct SendArgs {
|
|||
transport_args: ArgsEnum,
|
||||
}
|
||||
|
||||
// TODO(ry) Rename to op_datagram_send
|
||||
fn op_send(
|
||||
fn op_datagram_send(
|
||||
isolate_state: &mut CoreIsolateState,
|
||||
state: &State,
|
||||
args: Value,
|
||||
|
@ -222,7 +223,7 @@ fn op_send(
|
|||
.socket
|
||||
.poll_send_to(cx, &zero_copy, &addr)
|
||||
.map_err(OpError::from)
|
||||
.map_ok(|_| json!({}))
|
||||
.map_ok(|byte_length| json!(byte_length))
|
||||
});
|
||||
Ok(JsonOp::Async(f.boxed_local()))
|
||||
}
|
||||
|
|
|
@ -240,7 +240,9 @@ unitTest(
|
|||
assertEquals(bob.addr.hostname, "127.0.0.1");
|
||||
|
||||
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();
|
||||
assert(remote.transport === "udp");
|
||||
|
|
Loading…
Reference in a new issue