2020-03-09 19:22:15 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
import { sendSync, sendAsync } from "./dispatch_json.ts";
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
export interface NetAddr {
|
|
|
|
transport: "tcp" | "udp";
|
|
|
|
hostname: string;
|
|
|
|
port: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface UnixAddr {
|
|
|
|
transport: "unix" | "unixpacket";
|
|
|
|
address: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export type Addr = NetAddr | UnixAddr;
|
2020-03-09 19:22:15 -04:00
|
|
|
|
|
|
|
export enum ShutdownMode {
|
|
|
|
// See http://man7.org/linux/man-pages/man2/shutdown.2.html
|
|
|
|
// Corresponding to SHUT_RD, SHUT_WR, SHUT_RDWR
|
|
|
|
Read = 0,
|
|
|
|
Write,
|
2020-03-28 13:03:49 -04:00
|
|
|
ReadWrite, // unused
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function shutdown(rid: number, how: ShutdownMode): void {
|
|
|
|
sendSync("op_shutdown", { rid, how });
|
|
|
|
}
|
|
|
|
|
|
|
|
interface AcceptResponse {
|
|
|
|
rid: number;
|
2020-03-23 18:02:51 -04:00
|
|
|
localAddr: Addr;
|
|
|
|
remoteAddr: Addr;
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
export function accept(
|
|
|
|
rid: number,
|
|
|
|
transport: string
|
|
|
|
): Promise<AcceptResponse> {
|
|
|
|
return sendAsync("op_accept", { rid, transport });
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
export type ListenRequest = Addr;
|
2020-03-09 19:22:15 -04:00
|
|
|
|
|
|
|
interface ListenResponse {
|
|
|
|
rid: number;
|
2020-03-23 18:02:51 -04:00
|
|
|
localAddr: Addr;
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function listen(args: ListenRequest): ListenResponse {
|
|
|
|
return sendSync("op_listen", args);
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ConnectResponse {
|
|
|
|
rid: number;
|
2020-03-23 18:02:51 -04:00
|
|
|
localAddr: Addr;
|
|
|
|
remoteAddr: Addr;
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
export type ConnectRequest = Addr;
|
2020-03-09 19:22:15 -04:00
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
export function connect(args: ConnectRequest): Promise<ConnectResponse> {
|
2020-03-16 05:22:16 -04:00
|
|
|
return sendAsync("op_connect", args);
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
interface ReceiveResponse {
|
|
|
|
size: number;
|
2020-03-23 18:02:51 -04:00
|
|
|
remoteAddr: Addr;
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
2020-03-20 09:38:34 -04:00
|
|
|
export function receive(
|
2020-03-09 19:22:15 -04:00
|
|
|
rid: number,
|
2020-03-23 18:02:51 -04:00
|
|
|
transport: string,
|
2020-03-09 19:22:15 -04:00
|
|
|
zeroCopy: Uint8Array
|
|
|
|
): Promise<ReceiveResponse> {
|
2020-03-23 18:02:51 -04:00
|
|
|
return sendAsync("op_receive", { rid, transport }, zeroCopy);
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:02:51 -04:00
|
|
|
export type SendRequest = {
|
2020-03-09 19:22:15 -04:00
|
|
|
rid: number;
|
2020-03-23 18:02:51 -04:00
|
|
|
} & Addr;
|
2020-03-09 19:22:15 -04:00
|
|
|
|
|
|
|
export async function send(
|
|
|
|
args: SendRequest,
|
|
|
|
zeroCopy: Uint8Array
|
|
|
|
): Promise<void> {
|
|
|
|
await sendAsync("op_send", args, zeroCopy);
|
|
|
|
}
|