2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2018-10-03 23:58:29 -04:00
|
|
|
import { ReadResult, Reader, Writer, Closer } from "./io";
|
2019-03-30 14:45:36 -04:00
|
|
|
import * as msg from "gen/cli/msg_generated";
|
2018-10-03 23:58:29 -04:00
|
|
|
import { assert, notImplemented } from "./util";
|
|
|
|
import * as dispatch from "./dispatch";
|
2018-10-17 13:04:28 -04:00
|
|
|
import * as flatbuffers from "./flatbuffers";
|
2018-10-03 23:58:29 -04:00
|
|
|
import { read, write, close } from "./files";
|
|
|
|
|
|
|
|
export type Network = "tcp";
|
|
|
|
// TODO support other types:
|
|
|
|
// export type Network = "tcp" | "tcp4" | "tcp6" | "unix" | "unixpacket";
|
|
|
|
|
|
|
|
// TODO Support finding network from Addr, see https://golang.org/pkg/net/#Addr
|
|
|
|
export type Addr = string;
|
|
|
|
|
|
|
|
/** A Listener is a generic network listener for stream-oriented protocols. */
|
|
|
|
export interface Listener {
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Waits for and resolves to the next connection to the `Listener`. */
|
2018-10-03 23:58:29 -04:00
|
|
|
accept(): Promise<Conn>;
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Close closes the listener. Any pending accept promises will be rejected
|
|
|
|
* with errors.
|
2018-10-03 23:58:29 -04:00
|
|
|
*/
|
|
|
|
close(): void;
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Return the address of the `Listener`. */
|
2018-10-03 23:58:29 -04:00
|
|
|
addr(): Addr;
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
enum ShutdownMode {
|
|
|
|
// See http://man7.org/linux/man-pages/man2/shutdown.2.html
|
|
|
|
// Corresponding to SHUT_RD, SHUT_WR, SHUT_RDWR
|
|
|
|
Read = 0,
|
|
|
|
Write,
|
|
|
|
ReadWrite // unused
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
function shutdown(rid: number, how: ShutdownMode): void {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.Shutdown.createShutdown(builder, rid, how);
|
2019-03-09 12:30:38 -05:00
|
|
|
const baseRes = dispatch.sendSync(builder, msg.Any.Shutdown, inner);
|
|
|
|
assert(baseRes == null);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
class ConnImpl implements Conn {
|
|
|
|
constructor(
|
2018-10-10 11:59:36 -04:00
|
|
|
readonly rid: number,
|
2018-10-03 23:58:29 -04:00
|
|
|
readonly remoteAddr: string,
|
|
|
|
readonly localAddr: string
|
|
|
|
) {}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-08 01:18:21 -05:00
|
|
|
read(p: Uint8Array): Promise<ReadResult> {
|
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 shuts down (shutdown(2)) the reading side of the TCP connection.
|
|
|
|
* Most callers should just use close().
|
|
|
|
*/
|
|
|
|
closeRead(): void {
|
2018-10-10 11:59:36 -04:00
|
|
|
shutdown(this.rid, ShutdownMode.Read);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/** closeWrite shuts down (shutdown(2)) the writing side of the TCP
|
|
|
|
* connection. Most callers should just use close().
|
|
|
|
*/
|
|
|
|
closeWrite(): void {
|
2018-10-10 11:59:36 -04:00
|
|
|
shutdown(this.rid, ShutdownMode.Write);
|
2018-10-03 23:58:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
class ListenerImpl implements Listener {
|
|
|
|
constructor(readonly rid: number) {}
|
|
|
|
|
|
|
|
async accept(): Promise<Conn> {
|
|
|
|
const builder = flatbuffers.createBuilder();
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.Accept.createAccept(builder, this.rid);
|
2019-03-09 12:30:38 -05:00
|
|
|
const baseRes = await dispatch.sendAsync(builder, msg.Any.Accept, inner);
|
|
|
|
assert(baseRes != null);
|
|
|
|
assert(msg.Any.NewConn === baseRes!.innerType());
|
|
|
|
const res = new msg.NewConn();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
|
|
|
return new ConnImpl(res.rid(), res.remoteAddr()!, res.localAddr()!);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(): void {
|
|
|
|
close(this.rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
addr(): Addr {
|
|
|
|
return notImplemented();
|
|
|
|
}
|
2018-10-05 12:16:24 -04:00
|
|
|
}
|
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
export interface Conn extends Reader, Writer, Closer {
|
|
|
|
/** The local address of the connection. */
|
|
|
|
localAddr: string;
|
|
|
|
/** The remote address of the connection. */
|
|
|
|
remoteAddr: string;
|
|
|
|
/** The resource ID of the connection. */
|
|
|
|
rid: number;
|
|
|
|
/** Shuts down (`shutdown(2)`) the reading side of the TCP connection. Most
|
|
|
|
* callers should just use `close()`.
|
|
|
|
*/
|
|
|
|
closeRead(): void;
|
|
|
|
/** Shuts down (`shutdown(2)`) the writing side of the TCP connection. Most
|
|
|
|
* callers should just use `close()`.
|
|
|
|
*/
|
|
|
|
closeWrite(): void;
|
2018-10-05 12:16:24 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 23:58:29 -04:00
|
|
|
/** Listen announces on the local network address.
|
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* The network must be `tcp`, `tcp4`, `tcp6`, `unix` or `unixpacket`.
|
2018-10-03 23:58:29 -04:00
|
|
|
*
|
|
|
|
* For TCP networks, if the host in the address parameter is empty or a literal
|
2018-10-14 16:29:50 -04:00
|
|
|
* unspecified IP address, `listen()` listens on all available unicast and
|
|
|
|
* anycast IP addresses of the local system. To only use IPv4, use network
|
|
|
|
* `tcp4`. The address can use a host name, but this is not recommended,
|
|
|
|
* because it will create a listener for at most one of the host's IP
|
|
|
|
* addresses. If the port in the address parameter is empty or `0`, as in
|
|
|
|
* `127.0.0.1:` or `[::1]:0`, a port number is automatically chosen. The
|
|
|
|
* `addr()` method of `Listener` can be used to discover the chosen port.
|
2018-10-03 23:58:29 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* See `dial()` for a description of the network and address parameters.
|
2018-10-03 23:58:29 -04:00
|
|
|
*/
|
|
|
|
export function listen(network: Network, address: string): Listener {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-10-03 23:58:29 -04:00
|
|
|
const network_ = builder.createString(network);
|
|
|
|
const address_ = builder.createString(address);
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.Listen.createListen(builder, network_, address_);
|
2018-10-03 21:41:59 -04:00
|
|
|
const baseRes = dispatch.sendSync(builder, msg.Any.Listen, inner);
|
2018-10-03 23:58:29 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:41:59 -04:00
|
|
|
assert(msg.Any.ListenRes === baseRes!.innerType());
|
|
|
|
const res = new msg.ListenRes();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-10-03 23:58:29 -04:00
|
|
|
return new ListenerImpl(res.rid());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Dial connects to the address on the named network.
|
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* Supported networks are only `tcp` currently.
|
|
|
|
*
|
|
|
|
* TODO: `tcp4` (IPv4-only), `tcp6` (IPv6-only), `udp`, `udp4` (IPv4-only),
|
|
|
|
* `udp6` (IPv6-only), `ip`, `ip4` (IPv4-only), `ip6` (IPv6-only), `unix`,
|
|
|
|
* `unixgram` and `unixpacket`.
|
2018-10-03 23:58:29 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* For TCP and UDP networks, the address has the form `host:port`. The host must
|
2018-10-03 23:58:29 -04:00
|
|
|
* be a literal IP address, or a host name that can be resolved to IP addresses.
|
|
|
|
* The port must be a literal port number or a service name. If the host is a
|
|
|
|
* literal IPv6 address it must be enclosed in square brackets, as in
|
2018-10-14 16:29:50 -04:00
|
|
|
* `[2001:db8::1]:80` or `[fe80::1%zone]:80`. The zone specifies the scope of
|
2018-10-03 23:58:29 -04:00
|
|
|
* the literal IPv6 address as defined in RFC 4007. The functions JoinHostPort
|
|
|
|
* and SplitHostPort manipulate a pair of host and port in this form. When using
|
|
|
|
* TCP, and the host resolves to multiple IP addresses, Dial will try each IP
|
|
|
|
* address in order until one succeeds.
|
|
|
|
*
|
|
|
|
* Examples:
|
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* dial("tcp", "golang.org:http")
|
|
|
|
* dial("tcp", "192.0.2.1:http")
|
|
|
|
* dial("tcp", "198.51.100.1:80")
|
|
|
|
* dial("udp", "[2001:db8::1]:domain")
|
|
|
|
* dial("udp", "[fe80::1%lo0]:53")
|
|
|
|
* dial("tcp", ":80")
|
2018-10-03 23:58:29 -04:00
|
|
|
*/
|
|
|
|
export async function dial(network: Network, address: string): Promise<Conn> {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-10-03 23:58:29 -04:00
|
|
|
const network_ = builder.createString(network);
|
|
|
|
const address_ = builder.createString(address);
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.Dial.createDial(builder, network_, address_);
|
2018-10-03 21:41:59 -04:00
|
|
|
const baseRes = await dispatch.sendAsync(builder, msg.Any.Dial, inner);
|
2018-10-03 23:58:29 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:41:59 -04:00
|
|
|
assert(msg.Any.NewConn === baseRes!.innerType());
|
|
|
|
const res = new msg.NewConn();
|
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-10-03 23:58:29 -04:00
|
|
|
return new ConnImpl(res.rid(), res.remoteAddr()!, res.localAddr()!);
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** **RESERVED** */
|
2018-10-03 23:58:29 -04:00
|
|
|
export async function connect(
|
2019-03-09 12:30:38 -05:00
|
|
|
_network: Network,
|
|
|
|
_address: string
|
2018-10-03 23:58:29 -04:00
|
|
|
): Promise<Conn> {
|
|
|
|
return notImplemented();
|
|
|
|
}
|