2024-03-09 11:45:28 +00:00
|
|
|
use net;
|
|
|
|
use net::ip;
|
|
|
|
use net::tcp;
|
2024-06-01 13:44:54 +00:00
|
|
|
use net::tcp::{listen_option};
|
2024-03-09 11:45:28 +00:00
|
|
|
|
|
|
|
export type server = struct {
|
|
|
|
socket: net::socket,
|
|
|
|
};
|
|
|
|
|
|
|
|
export type server_request = struct {
|
|
|
|
socket: net::socket,
|
|
|
|
request: request,
|
|
|
|
};
|
|
|
|
|
2024-06-01 13:44:54 +00:00
|
|
|
export fn listen(ip: ip::addr, port: u16, options: listen_option...) (*server | net::error) = {
|
2024-03-09 11:45:28 +00:00
|
|
|
return alloc(server {
|
2024-06-01 13:44:54 +00:00
|
|
|
socket = tcp::listen(ip, port, options...)?,
|
2024-03-09 11:45:28 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export fn server_finish(server: *server) void = {
|
|
|
|
net::close(server.socket)!;
|
|
|
|
free(server);
|
|
|
|
};
|
|
|
|
|
|
|
|
export fn accept(server: *server) (net::socket | net::error) = {
|
|
|
|
return net::accept(server.socket)?;
|
|
|
|
};
|
|
|
|
|
|
|
|
export fn serve(server: *server) (*server_request | net::error) = {
|
|
|
|
const socket = accept(server)?;
|
|
|
|
const request = request_parse(socket)!;
|
|
|
|
|
|
|
|
return alloc(server_request {
|
|
|
|
request = request,
|
|
|
|
socket = socket,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export fn serve_finish(serv_req: *server_request) void = {
|
|
|
|
parsed_request_finish(&serv_req.request);
|
|
|
|
net::close(serv_req.socket)!;
|
|
|
|
free(serv_req);
|
|
|
|
};
|