2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-03-10 15:14:22 -04:00
|
|
|
import { errors } from "./errors.ts";
|
2019-09-02 17:07:11 -04:00
|
|
|
import { EOF, Reader, Writer, Closer } from "./io.ts";
|
2020-03-09 10:18:02 -04:00
|
|
|
import { read, write } from "./ops/io.ts";
|
2020-03-08 08:09:22 -04:00
|
|
|
import { close } from "./ops/resources.ts";
|
2020-03-09 19:22:15 -04:00
|
|
|
import * as netOps from "./ops/net.ts";
|
|
|
|
import { Transport } from "./ops/net.ts";
|
|
|
|
export { ShutdownMode, shutdown, Transport } from "./ops/net.ts";
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2019-08-27 11:35:32 -04:00
|
|
|
export interface Addr {
|
2019-09-20 18:32:18 -04:00
|
|
|
transport: Transport;
|
2020-01-18 15:49:55 -05:00
|
|
|
hostname: string;
|
|
|
|
port: number;
|
2019-08-27 11:35:32 -04:00
|
|
|
}
|
2018-10-03 23:58:29 -04:00
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
export interface UDPAddr {
|
|
|
|
transport?: Transport;
|
|
|
|
hostname?: string;
|
|
|
|
port: number;
|
|
|
|
}
|
|
|
|
|
2020-03-10 15:14:22 -04:00
|
|
|
export interface UDPConn extends AsyncIterable<[Uint8Array, Addr]> {
|
2020-02-21 11:26:54 -05:00
|
|
|
receive(p?: Uint8Array): Promise<[Uint8Array, Addr]>;
|
|
|
|
|
|
|
|
send(p: Uint8Array, addr: UDPAddr): Promise<void>;
|
|
|
|
|
|
|
|
close(): void;
|
|
|
|
|
|
|
|
addr: Addr;
|
|
|
|
|
|
|
|
[Symbol.asyncIterator](): AsyncIterator<[Uint8Array, Addr]>;
|
|
|
|
}
|
|
|
|
|
2020-03-10 15:14:22 -04:00
|
|
|
export interface Listener extends AsyncIterable<Conn> {
|
2018-10-03 23:58:29 -04:00
|
|
|
accept(): Promise<Conn>;
|
|
|
|
|
|
|
|
close(): void;
|
|
|
|
|
2020-01-18 15:49:55 -05:00
|
|
|
addr: Addr;
|
2019-05-01 16:58:09 -04:00
|
|
|
|
|
|
|
[Symbol.asyncIterator](): AsyncIterator<Conn>;
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
2019-09-23 14:40:38 -04:00
|
|
|
export class ConnImpl implements Conn {
|
2018-10-03 23:58:29 -04:00
|
|
|
constructor(
|
2018-10-10 11:59:36 -04:00
|
|
|
readonly rid: number,
|
2020-01-18 15:49:55 -05:00
|
|
|
readonly remoteAddr: Addr,
|
|
|
|
readonly localAddr: Addr
|
2018-10-03 23:58:29 -04:00
|
|
|
) {}
|
|
|
|
|
2018-11-08 01:18:21 -05:00
|
|
|
write(p: Uint8Array): Promise<number> {
|
2018-10-10 11:59:36 -04:00
|
|
|
return write(this.rid, p);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
2019-07-06 10:16:03 -04:00
|
|
|
read(p: Uint8Array): Promise<number | EOF> {
|
2018-10-10 11:59:36 -04:00
|
|
|
return read(this.rid, p);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
2018-10-10 11:59:36 -04:00
|
|
|
close(this.rid);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
closeRead(): void {
|
2020-03-09 19:22:15 -04:00
|
|
|
netOps.shutdown(this.rid, netOps.ShutdownMode.Read);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
closeWrite(): void {
|
2020-03-09 19:22:15 -04:00
|
|
|
netOps.shutdown(this.rid, netOps.ShutdownMode.Write);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-21 14:38:28 -04:00
|
|
|
export class ListenerImpl implements Listener {
|
2020-03-10 15:14:22 -04:00
|
|
|
constructor(readonly rid: number, readonly addr: Addr) {}
|
2019-03-09 12:30:38 -05:00
|
|
|
|
|
|
|
async accept(): Promise<Conn> {
|
2020-03-09 19:22:15 -04:00
|
|
|
const res = await netOps.accept(this.rid);
|
2019-08-27 11:35:32 -04:00
|
|
|
return new ConnImpl(res.rid, res.remoteAddr, res.localAddr);
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
|
2020-03-10 15:14:22 -04:00
|
|
|
async *[Symbol.asyncIterator](): AsyncIterator<Conn> {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
yield await this.accept();
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof errors.BadResource) {
|
|
|
|
break;
|
2019-11-09 14:40:22 -05:00
|
|
|
}
|
2020-03-10 15:14:22 -04:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2019-05-01 16:58:09 -04:00
|
|
|
}
|
2018-10-05 12:16:24 -04:00
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
export async function recvfrom(
|
|
|
|
rid: number,
|
|
|
|
p: Uint8Array
|
|
|
|
): Promise<[number, Addr]> {
|
2020-03-09 19:22:15 -04:00
|
|
|
const { size, remoteAddr } = await netOps.receive(rid, p);
|
2020-02-21 11:26:54 -05:00
|
|
|
return [size, remoteAddr];
|
|
|
|
}
|
|
|
|
|
|
|
|
export class UDPConnImpl implements UDPConn {
|
|
|
|
constructor(
|
|
|
|
readonly rid: number,
|
|
|
|
readonly addr: Addr,
|
2020-03-10 15:14:22 -04:00
|
|
|
public bufSize: number = 1024
|
2020-02-21 11:26:54 -05:00
|
|
|
) {}
|
|
|
|
|
|
|
|
async receive(p?: Uint8Array): Promise<[Uint8Array, Addr]> {
|
|
|
|
const buf = p || new Uint8Array(this.bufSize);
|
|
|
|
const [size, remoteAddr] = await recvfrom(this.rid, buf);
|
|
|
|
const sub = buf.subarray(0, size);
|
|
|
|
return [sub, remoteAddr];
|
|
|
|
}
|
|
|
|
|
|
|
|
async send(p: Uint8Array, addr: UDPAddr): Promise<void> {
|
|
|
|
const remote = { hostname: "127.0.0.1", transport: "udp", ...addr };
|
|
|
|
if (remote.transport !== "udp") throw Error("Remote transport must be UDP");
|
|
|
|
const args = { ...remote, rid: this.rid };
|
2020-03-09 19:22:15 -04:00
|
|
|
await netOps.send(args as netOps.SendRequest, p);
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
|
2020-03-10 15:14:22 -04:00
|
|
|
async *[Symbol.asyncIterator](): AsyncIterator<[Uint8Array, Addr]> {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
yield await this.receive();
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof errors.BadResource) {
|
|
|
|
break;
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
2020-03-10 15:14:22 -04:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
2020-02-21 11:26:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
export interface Conn extends Reader, Writer, Closer {
|
2020-01-18 15:49:55 -05:00
|
|
|
localAddr: Addr;
|
|
|
|
remoteAddr: Addr;
|
2019-03-09 12:30:38 -05:00
|
|
|
rid: number;
|
|
|
|
closeRead(): void;
|
|
|
|
closeWrite(): void;
|
2018-10-05 12:16:24 -04:00
|
|
|
}
|
|
|
|
|
2019-09-20 18:32:18 -04:00
|
|
|
export interface ListenOptions {
|
|
|
|
port: number;
|
|
|
|
hostname?: string;
|
|
|
|
transport?: Transport;
|
|
|
|
}
|
|
|
|
|
2020-02-21 11:26:54 -05:00
|
|
|
export function listen(
|
|
|
|
options: ListenOptions & { transport?: "tcp" }
|
|
|
|
): Listener;
|
|
|
|
export function listen(options: ListenOptions & { transport: "udp" }): UDPConn;
|
2020-03-10 12:08:58 -04:00
|
|
|
export function listen({
|
|
|
|
port,
|
|
|
|
hostname = "0.0.0.0",
|
|
|
|
transport = "tcp"
|
|
|
|
}: ListenOptions): Listener | UDPConn {
|
|
|
|
const res = netOps.listen({ port, hostname, transport });
|
|
|
|
|
|
|
|
if (transport === "tcp") {
|
2020-02-21 11:26:54 -05:00
|
|
|
return new ListenerImpl(res.rid, res.localAddr);
|
|
|
|
} else {
|
|
|
|
return new UDPConnImpl(res.rid, res.localAddr);
|
|
|
|
}
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
2020-01-18 12:35:12 -05:00
|
|
|
export interface ConnectOptions {
|
2019-09-20 18:32:18 -04:00
|
|
|
port: number;
|
|
|
|
hostname?: string;
|
|
|
|
transport?: Transport;
|
|
|
|
}
|
|
|
|
|
2020-03-10 12:08:58 -04:00
|
|
|
export async function connect({
|
|
|
|
port,
|
|
|
|
hostname = "127.0.0.1",
|
|
|
|
transport = "tcp"
|
|
|
|
}: ConnectOptions): Promise<Conn> {
|
|
|
|
const res = await netOps.connect({ port, hostname, transport });
|
2019-08-26 08:50:21 -04:00
|
|
|
return new ConnImpl(res.rid, res.remoteAddr!, res.localAddr!);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|