Lindenii Project Forge
Login

hare-ev

Temporary fork of hare-ev for... reasons
Commit info
ID
fab1b277366049cb7ff71f9a67746cc229d948d1
Author
Drew DeVault <sir@cmpwn.com>
Author date
Fri, 16 Dec 2022 15:19:09 +0100
Committer
Drew DeVault <sir@cmpwn.com>
Committer date
Fri, 16 Dec 2022 15:19:09 +0100
Actions
ev::connect_tcp: improve docs
use errors;
use net;
use net::ip;
use net::tcp;
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)?;
};

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

// Creates a socket and connects to a given IP address and port over TCP.
//
// The variadic arguments accept [[net::sockflags]] 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 *void) 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::sockflags | *void)...
) (void | net::error | errors::error) = {
	// XXX: This doesn't let us set keepalive
	let opt: net::sockflags = 0;
	let user: nullable *void = null;
	for (let i = 0z; i < len(opts); i += 1) {
		match (opts[i]) {
		case let o: net::sockflags =>
			opt |= o;
		case let u: *void =>
			assert(user == null);
			user = u;
		};
	};
	const sock = tcp::connect(addr, port, opt | net::sockflags::NONBLOCK)?;
	let file = register(loop, sock)?;
	file.user = user;
	file.cb = cb;
	file.op = op::CONNECT;
	file_epoll_ctl(file);
};

fn connect_ready(
	sock: *file,
	ev: *rt::epoll_event,
) void = {
	assert(sock.op == op::CONNECT);
	assert(ev.events & rt::EPOLLOUT != 0);
	assert(sock.cb != null);
	const cb = sock.cb: *connectcb;
	sock.op = op::NONE;
	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);
	};
};

// 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::sockflags...
) req = {
	assert(sock.op == op::NONE);
	let fl: net::sockflags = 0;
	for (let i = 0z; i < len(flags); i += 1) {
		fl |= flags[i];
	};
	sock.op = op::ACCEPT;
	sock.cb = cb;
	sock.sockflags = fl;
	file_epoll_ctl(sock);
	return req { ... };
};

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.sockflags);
	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);
	};
};