Lindenii Project Forge
Login

hare-ev

Temporary fork of hare-ev for... reasons
Commit info
ID
11e7fa2f00dc6faebf522f9dd5df87923533346f
Author
Drew DeVault <sir@cmpwn.com>
Author date
Wed, 27 Sep 2023 13:37:26 +0200
Committer
Drew DeVault <sir@cmpwn.com>
Committer date
Wed, 27 Sep 2023 13:37:26 +0200
Actions
Implement I/O request cancellation
use io;
use rt;

// A callback for a [[read]] or [[readv]] operation.
export type readcb = fn(file: *file, result: (size | io::EOF | io::error)) void;

// Schedules a read operation on a file object. The provided buffer must be
// valid for the duration of the read operation.
export fn read(
	file: *file,
	cb: *readcb,
	buf: []u8,
) req = {
	file.rvbuf = io::mkvector(buf);
	// XXX: Bit of a hack to avoid allocating a slice
	const vec = (&file.rvbuf: *[*]io::vector)[..1];
	return readv(file, cb, vec...);
};

// Schedules a vectored read operation on a file object. The provided vectors
// must be valid for the duration of the read operation.
export fn readv(
	file: *file,
	cb: *readcb,
	vec: io::vector...
) req = {
	assert(file.op & op::READV == 0);
	if (file.flags & fflags::BLOCKING != 0) {
		const r = io::readv(file.fd, vec...);
		cb(file, r);
		return req { ... };
	};

	file.op |= op::READV;
	file.cb = cb;
	file.rvec = vec;
	file_epoll_ctl(file);
	return req { ... };
	return mkreq(&readv_cancel, file);
};

fn readv_ready(file: *file, ev: *rt::epoll_event) void = {
	assert(file.op & op::READV != 0);
	assert(file.cb != null);
	const cb = file.cb: *readcb;
	file.op &= ~op::READV;
	file_epoll_ctl(file);

	if (ev.events & rt::EPOLLHUP != 0) {
		cb(file, io::EOF);
	} else {
		const vec = file.rvec: []io::vector;
		const r = io::readv(file.fd, vec...);
		cb(file, r);
	};
};

fn readv_cancel(req: *req) void = {
	const file = req.user: *file;
	assert(file.op & op::READV != 0);
	file.op &= ~op::READV;
	file_epoll_ctl(file);
};

// A callback for a [[write]] or [[writev]] operation.
export type writecb = fn(file: *file, result: (size | io::error)) void;

// Schedules a write operation on a file object. The provided buffer must be
// valid for the duration of the write operation.
export fn write(
	file: *file,
	cb: *writecb,
	buf: []u8,
) req = {
	file.wvbuf = io::mkvector(buf);
	// XXX: Bit of a hack to avoid allocating a slice
	const vec = (&file.wvbuf: *[*]io::vector)[..1];
	return writev(file, cb, vec...);
};

// Schedules a vectored read operation on a file object. The provided buffer
// must be valid for the duration of the write operation.
export fn writev(
	file: *file,
	cb: *writecb,
	vec: io::vector...
) req = {
	assert(file.op & op::WRITEV == 0);
	if (file.flags & fflags::BLOCKING != 0) {
		const r = io::writev(file.fd, vec...);
		cb(file, r);
		return req { ... };
	};

	file.op |= op::WRITEV;
	file.cb2 = cb;
	file.wvec = vec;
	file_epoll_ctl(file);
	return req { ... };
	return mkreq(&writev_cancel, file);
};

fn writev_ready(file: *file, ev: *rt::epoll_event) void = {
	assert(file.op & op::WRITEV != 0);
	assert(file.cb != null);
	const vec = file.wvec: []io::vector;
	const r = io::writev(file.fd, vec...);
	const cb = file.cb2: *writecb;
	file.op &= ~op::WRITEV;
	file_epoll_ctl(file);
	cb(file, r);
};

fn writev_cancel(req: *req) void = {
	const file = req.user: *file;
	assert(file.op & op::WRITEV != 0);
	file.op &= ~op::WRITEV;
	file_epoll_ctl(file);
};
use rt;

// A callback for a [[readable]] operation.
export type readablecb = fn(file: *file) void;

// Executes the callback when a given file is readable. Cannot be combined with
// [[read]] or [[readv]].
export fn readable(
	file: *file,
	cb: *readablecb,
) req = {
	assert(file.op & op::READABLE == 0 && file.op & op::READV == 0);
	if (file.flags & fflags::BLOCKING != 0) {
		cb(file);
		return req { ... };
	};

	file.op |= op::READABLE;
	file.cb = cb;
	file_epoll_ctl(file);
	return req { ... };
	return mkreq(&readable_cancel, file);
};

fn readable_ready(file: *file, ev: *rt::epoll_event) void = {
	assert(file.op & op::READABLE != 0);
	assert(file.cb != null);
	const cb = file.cb: *readablecb;
	file.op &= ~op::READABLE;
	file_epoll_ctl(file);
	cb(file);
};

fn readable_cancel(req: *req) void = {
	const file = req.user: *file;
	assert(file.op & op::READABLE != 0);
	file.op &= ~op::READABLE;
	file_epoll_ctl(file);
};

// A callback for a [[writable]] operation.
export type writablecb = fn(file: *file) void;

// Executes the callback when a given file is writable. Cannot be combined with
// [[write]] or [[writev]].
export fn writable(
	file: *file,
	cb: *writablecb,
) req = {
	assert(file.op & op::WRITABLE == 0 && file.op & op::WRITEV == 0);
	if (file.flags & fflags::BLOCKING != 0) {
		cb(file);
		return req { ... };
	};

	file.op |= op::WRITABLE;
	file.cb = cb;
	file_epoll_ctl(file);
	return req { ... };
	return mkreq(&writable_cancel, file);
};

fn writable_ready(file: *file, ev: *rt::epoll_event) void = {
	assert(file.op & op::WRITABLE != 0);
	assert(file.cb != null);
	const cb = file.cb: *writablecb;
	file.op &= ~op::WRITABLE;
	file_epoll_ctl(file);
	cb(file);
};

fn writable_cancel(req: *req) void = {
	const file = req.user: *file;
	assert(file.op & op::WRITABLE != 0);
	file.op &= ~op::WRITABLE;
	file_epoll_ctl(file);
};
export type req = struct {
	cancel: nullable *cancelfn,
	user: nullable *opaque,
};

// Makes a new request object.
export fn mkreq(cancel: *cancelfn, user: nullable *opaque) req = {
	return req {
		cancel = cancel,
		user = user,
	};
};

// A function which cancels an in-flight request.
export type cancelfn = fn(req: *req) void;

// Cancels an in-flight request. The user can safely cancel a request more than
// once. A request cannot be cancelled once it has completed.
export fn cancel(req: *req) void = {
	match (req.cancel) {
	case let cancel: *cancelfn =>
		cancel(req);
	case null => yield;
	};
	req.cancel = null;
};
use errors;
use net;
use net::ip;
use net::tcp;
use net::udp;
use rt;

// Creates a socket which listens for incoming TCP connections on the given
// IP address and port.
export fn listen_tcp(
	loop: *loop,
	addr: ip::addr,
	port: u16,
	opts: tcp::listen_option...
) (*file | net::error | errors::error) = {
	const sock = tcp::listen(addr, port, opts...)?;
	return register(loop, sock)?;
};

// Creates a socket which listens for incoming UDP packets on the given IP
// address and port.
export fn listen_udp(
	loop: *loop,
	addr: ip::addr,
	port: u16,
	opts: udp::listen_option...
) (*file | net::error | errors::error) = {
	const sock = udp::listen(addr, port, opts...)?;
	return register(loop, sock)?;
};

// Creates a UDP socket on this event loop and sets the default destination to
// the given address.
export fn connect_udp(
	loop: *loop,
	dest: ip::addr,
	port: u16,
	opts: udp::connect_option...
) (*file | net::error | errors::error) = {
	const sock = udp::connect(dest, port, opts...)?;
	const file = register(loop, sock)?;
	return file;
};

export type connectcb = fn(result: (*file | net::error), user: nullable *opaque) void;

// Creates a socket and connects to a given IP address and port over TCP.
//
// The variadic arguments accept [[net::sockflag]] and/or no more than one user
// data pointer. If the user data pointer is provided, it will be passed to the
// callback. This allows the user to pass a state object through the connection
// process:
//
//	let user: state = // ...
// 	ev::connect_tcp(&loop, &connected, addr, port, &user);
//
// 	fn connected(result: (*ev::file | net::error), user: nullable *opaque) void = {
// 		let user = user: *state;
// 	};
//
// The user data object provided will be assigned to the [[file]] which is
// provided to the callback after the connection is established.
//
// If you don't need a user data object you can just omit it:
//
// 	ev::connect_tcp(&loop, &connected, addr, port, &user);
export fn connect_tcp(
	loop: *loop,
	cb: *connectcb,
	addr: ip::addr,
	port: u16,
	opts: (net::sockflag | *opaque)...
) (void | net::error | errors::error) = {
) (req | net::error | errors::error) = {
	// XXX: This doesn't let us set keepalive
	let opt: net::sockflag = 0;
	let user: nullable *opaque = null;
	for (let i = 0z; i < len(opts); i += 1) {
		match (opts[i]) {
		case let o: net::sockflag =>
			opt |= o;
		case let u: *opaque =>
			assert(user == null);
			user = u;
		};
	};
	const sock = tcp::connect(addr, port, opt | net::sockflag::NONBLOCK)?;
	let file = register(loop, sock)?;
	file.user = user;
	file.cb = cb;
	file.op = op::CONNECT_TCP;
	file_epoll_ctl(file);
	return mkreq(&connect_tcp_cancel, file);
};

fn connect_tcp_ready(
	sock: *file,
	ev: *rt::epoll_event,
) void = {
	assert(sock.op == op::CONNECT_TCP);
	assert(ev.events & rt::EPOLLOUT != 0);
	assert(sock.cb != null);
	const cb = sock.cb: *connectcb;
	sock.op = op::NONE;
	sock.op &= ~op::CONNECT_TCP;
	file_epoll_ctl(sock);

	let errno = 0i, optsz = size(int): u32;
	rt::getsockopt(sock.fd, rt::SOL_SOCKET, rt::SO_ERROR, &errno, &optsz)!;
	if (errno != 0) {
		cb(errors::errno(errno), sock.user);
		close(sock);
	} else {
		// XXX: If the user puts NONBLOCK into the opts provided at
		// [[connect_tcp]] we could try to preserve that here
		const fl = rt::fcntl(sock.fd, rt::F_GETFL, void)!;
		rt::fcntl(sock.fd, rt::F_SETFL, fl & ~rt::O_NONBLOCK)!;
		cb(sock, sock.user);
	};
};

fn connect_tcp_cancel(req: *req) void = {
	const sock = req.user: *file;
	assert(sock.op == op::CONNECT_TCP);
	sock.op = op::NONE;
	file_epoll_ctl(sock);
};

// A callback for an [[accept]] operation.
export type acceptcb = fn(file: *file, result: (*file | net::error)) void;

// Schedules an accept operation on a socket.
export fn accept(
	sock: *file,
	cb: *acceptcb,
	flags: net::sockflag...
) req = {
	assert(sock.op == op::NONE);
	let fl: net::sockflag = 0;
	for (let i = 0z; i < len(flags); i += 1) {
		fl |= flags[i];
	};
	sock.op = op::ACCEPT;
	sock.cb = cb;
	sock.sockflag = fl;
	file_epoll_ctl(sock);
	return req { ... };
	return mkreq(&accept_cancel, sock);
};

fn accept_ready(
	sock: *file,
	ev: *rt::epoll_event,
) void = {
	assert(sock.op == op::ACCEPT);
	assert(ev.events & rt::EPOLLIN != 0);
	assert(sock.cb != null);
	const cb = sock.cb: *acceptcb;
	sock.op = op::NONE;
	file_epoll_ctl(sock);

	const r = tcp::accept(sock.fd, sock.sockflag);
	match (r) {
	case let fd: net::socket =>
		// TODO: Bubble up errors from here?
		const file = register(sock.ev, fd)!;
		cb(sock, file);
	case let err: net::error =>
		cb(sock, err);
	};
};

fn accept_cancel(req: *req) void = {
	const sock = req.user: *file;
	assert(sock.op == op::ACCEPT);
	sock.op = op::NONE;
	file_epoll_ctl(sock);
};

// TODO: Support recv & send in parallel

// Callback for a [[recvfrom]] operation. The second parameter is either an
// error or a tuple of the number of bytes received and the IP address and port
// of the sender.
export type recvfromcb = fn(
	file: *file,
	r: ((size, ip::addr, u16) | net::error),
) void;

// Schedules a receive operation on a socket.
export fn recvfrom(
	sock: *file,
	cb: *recvfromcb,
	buf: []u8,
) req = {
	assert(sock.op == op::NONE);
	sock.op = op::RECVFROM;
	sock.cb = cb;
	sock.sendrecv.rbuf = buf;
	file_epoll_ctl(sock);
	return req { ... };
	return mkreq(&recvfrom_cancel, sock);
};

fn recvfrom_ready(
	sock: *file,
	ev: *rt::epoll_event,
) void = {
	assert(sock.op == op::RECVFROM);
	assert(sock.cb != null);
	const cb = sock.cb: *recvfromcb;
	sock.op = op::NONE;
	file_epoll_ctl(sock);

	let src: ip::addr = ip::ANY_V4, port = 0u16;
	match (udp::recvfrom(sock.fd, sock.sendrecv.rbuf, &src, &port)) {
	case let err: net::error =>
		cb(sock, err);
	case let n: size =>
		cb(sock, (n, src, port));
	};
};

fn recvfrom_cancel(req: *req) void = {
	const sock = req.user: *file;
	assert(sock.op == op::RECVFROM);
	sock.op = op::NONE;
	file_epoll_ctl(sock);
};

// Callback for a [[recv]] operation.
export type recvcb = fn(file: *file, r: (size | net::error)) void;

// Schedules a receive operation on a (connected) socket.
export fn recv(
	sock: *file,
	cb: *recvcb,
	buf: []u8,
) req = {
	assert(sock.op == op::NONE);
	sock.op = op::RECV;
	sock.cb = cb;
	sock.sendrecv.rbuf = buf;
	file_epoll_ctl(sock);
	return req { ... };
	return mkreq(&recv_cancel, sock);
};

fn recv_ready(
	sock: *file,
	ev: *rt::epoll_event,
) void = {
	assert(sock.op == op::RECV);
	assert(sock.cb != null);
	const cb = sock.cb: *recvcb;
	sock.op = op::NONE;
	file_epoll_ctl(sock);

	const r = udp::recv(sock.fd, sock.sendrecv.rbuf);
	cb(sock, r);
};

fn recv_cancel(req: *req) void = {
	const sock = req.user: *file;
	assert(sock.op == op::RECV);
	sock.op = op::NONE;
	file_epoll_ctl(sock);
};

// Callback for a [[send]] or [[sendto]] operation.
export type sendtocb = fn(file: *file, r: (size | net::error)) void;

// Schedules a send operation on a (connected) socket.
export fn send(
	sock: *file,
	cb: *sendtocb,
	buf: []u8,
) req = {
	assert(sock.op == op::NONE);
	sock.op = op::SEND;
	sock.cb = cb;
	sock.sendrecv.sbuf = buf;
	file_epoll_ctl(sock);
	return req { ... };
	return mkreq(&send_cancel, sock);
};

fn send_ready(
	sock: *file,
	ev: *rt::epoll_event,
) void = {
	assert(sock.op == op::SEND);
	assert(sock.cb != null);
	const cb = sock.cb: *sendtocb;
	sock.op = op::NONE;
	file_epoll_ctl(sock);

	const r = udp::send(sock.fd, sock.sendrecv.sbuf);
	cb(sock, r);
};

fn send_cancel(req: *req) void = {
	const sock = req.user: *file;
	assert(sock.op == op::SEND);
	sock.op = op::NONE;
	file_epoll_ctl(sock);
};

// Schedules a send operation on a socket.
export fn sendto(
	sock: *file,
	cb: *sendtocb,
	buf: []u8,
	dest: ip::addr,
	port: u16,
) req = {
	assert(sock.op == op::NONE);
	sock.op = op::SENDTO;
	sock.cb = cb;
	sock.sendrecv.sbuf = buf;
	sock.sendrecv.dest = dest;
	sock.sendrecv.port = port;
	file_epoll_ctl(sock);
	return req { ... };
	return mkreq(&sendto_cancel, sock);
};

fn sendto_ready(
	sock: *file,
	ev: *rt::epoll_event,
) void = {
	assert(sock.op == op::SENDTO);
	assert(sock.cb != null);
	const cb = sock.cb: *sendtocb;
	sock.op = op::NONE;
	file_epoll_ctl(sock);

	const r = udp::sendto(
		sock.fd,
		sock.sendrecv.sbuf,
		sock.sendrecv.dest,
		sock.sendrecv.port,
	);
	cb(sock, r);
};

fn sendto_cancel(req: *req) void = {
	const sock = req.user: *file;
	assert(sock.op == op::SENDTO);
	sock.op = op::NONE;
	file_epoll_ctl(sock);
};
export type req = struct {
	placeholder: uint,
};