2020-03-09 19:22:15 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-07-06 21:45:39 -04:00
|
|
|
|
2020-03-09 19:22:15 -04:00
|
|
|
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";
|
2020-04-28 12:37:59 -04:00
|
|
|
path: string;
|
2020-03-23 18:02:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2020-07-06 21:45:39 -04:00
|
|
|
Write = 1,
|
2020-03-28 13:03:49 -04:00
|
|
|
ReadWrite, // unused
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
2020-04-28 01:36:47 -04:00
|
|
|
export function shutdown(rid: number, how: ShutdownMode): Promise<void> {
|
2020-03-09 19:22:15 -04:00
|
|
|
sendSync("op_shutdown", { rid, how });
|
2020-04-28 01:36:47 -04:00
|
|
|
return Promise.resolve();
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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-06-13 10:14:31 -04:00
|
|
|
return sendAsync("op_datagram_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
|
|
|
|
2020-07-06 21:45:39 -04:00
|
|
|
export function send(args: SendRequest, zeroCopy: Uint8Array): Promise<number> {
|
|
|
|
return sendAsync("op_datagram_send", args, zeroCopy);
|
2020-03-09 19:22:15 -04:00
|
|
|
}
|