Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve interfaces around async networking #87

Merged
merged 14 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ members = [
"crates/sel4",
"crates/sel4-async/block-io",
"crates/sel4-async/block-io/fat",
"crates/sel4-async/io",
"crates/sel4-async/network",
"crates/sel4-async/network/rustls",
"crates/sel4-async/network/rustls/utils",
"crates/sel4-async/network/traits",
"crates/sel4-async/single-threaded-executor",
"crates/sel4-async/time",
"crates/sel4-async/unsync",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mk {
sel4-async-unsync
sel4-async-time
sel4-async-network
sel4-async-network-traits
sel4-async-io
sel4-async-network-rustls
sel4-async-network-rustls-utils
sel4-panicking-env
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ log = "0.4.17"
rustls-pemfile = { version = "2.0.0", default-features = false }
sel4-async-block-io = { path = "../../../../../../sel4-async/block-io" }
sel4-async-block-io-fat = { path = "../../../../../../sel4-async/block-io/fat" }
sel4-async-io = { path = "../../../../../../sel4-async/io" }
sel4-async-network = { path = "../../../../../../sel4-async/network" }
sel4-async-network-rustls = { path = "../../../../../../sel4-async/network/rustls" }
sel4-async-network-rustls-utils = { path = "../../../../../../sel4-async/network/rustls/utils" }
sel4-async-network-traits = { path = "../../../../../../sel4-async/network/traits" }
sel4-async-time = { path = "../../../../../../sel4-async/time" }
sel4-async-unsync = { path = "../../../../../../sel4-async/unsync" }
sel4-panicking-env = { path = "../../../../../../sel4-panicking/env" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ use rustls::ServerConfig;

use sel4_async_block_io::{access::ReadOnly, constant_block_sizes, BlockIO};
use sel4_async_block_io_fat as fat;
use sel4_async_io::ReadExactError;
use sel4_async_network::{ManagedInterface, TcpSocket, TcpSocketError};
use sel4_async_network_rustls::{Error as AsyncRustlsError, ServerConnector};
use sel4_async_network_rustls_utils::GetCurrentTimeImpl;
use sel4_async_network_traits::ClosedError;
use sel4_async_single_threaded_executor::LocalSpawner;
use sel4_async_time::{Instant, TimerManager};

Expand Down Expand Up @@ -117,18 +117,18 @@ type SocketUser<T> = Box<
async fn use_socket_for_http<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static>(
server: Server<D, T>,
mut socket: TcpSocket,
) -> Result<(), ClosedError<TcpSocketError>> {
) -> Result<(), ReadExactError<TcpSocketError>> {
socket.accept(HTTP_PORT).await?;
server.handle_connection(&mut socket).await?;
socket.close().await?;
socket.close();
Ok(())
}

async fn use_socket_for_https<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static>(
server: Server<D, T>,
tls_config: Arc<ServerConfig>,
mut socket: TcpSocket,
) -> Result<(), ClosedError<AsyncRustlsError<TcpSocketError>>> {
) -> Result<(), ReadExactError<AsyncRustlsError<TcpSocketError>>> {
socket
.accept(HTTPS_PORT)
.await
Expand All @@ -138,11 +138,7 @@ async fn use_socket_for_https<D: fat::BlockDevice + 'static, T: fat::TimeSource

server.handle_connection(&mut conn).await?;

// TODO TcpSocket doesn't support stateless .poll_close() yet, so we close the socket directly
conn.into_io()
.close()
.await
.map_err(AsyncRustlsError::TransitError)?;
conn.into_io().close();

Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use alloc::string::{String, ToString};
use alloc::vec;

use sel4_async_block_io_fat as fat;
use sel4_async_network_traits::{AsyncIO, AsyncIOExt, ClosedError};
use sel4_async_io::{Read, ReadExactError, Write};
use sel4_async_unsync::Mutex;

use crate::mime::content_type_from_name;
Expand All @@ -29,17 +29,17 @@ impl<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static> Server<D, T> {
}
}

pub(crate) async fn handle_connection<U: AsyncIO + Unpin>(
pub(crate) async fn handle_connection<U: Read + Write + Unpin>(
&self,
conn: &mut U,
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
loop {
let mut buf = vec![0; 1024 * 16];
let mut i = 0;
loop {
let n = conn.read(&mut buf[i..]).await?;
if n == 0 {
return Err(ClosedError::Closed);
return Err(ReadExactError::UnexpectedEof);
}
i += n;
if is_request_complete(&buf[..i]).unwrap_or(false) {
Expand Down Expand Up @@ -68,11 +68,11 @@ impl<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static> Server<D, T> {
Ok(())
}

async fn handle_request<U: AsyncIO + Unpin>(
async fn handle_request<U: Read + Write + Unpin>(
&self,
conn: &mut U,
request_path: &str,
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
match self.lookup_request_path(request_path).await {
RequestPathStatus::Ok { file_name, file } => {
let content_type = content_type_from_name(&file_name);
Expand All @@ -88,12 +88,12 @@ impl<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static> Server<D, T> {
Ok(())
}

async fn serve_file<U: AsyncIO + Unpin>(
async fn serve_file<U: Write + Unpin>(
&self,
conn: &mut U,
content_type: &str,
file: fat::File,
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
let file_len: usize = self
.volume_manager
.lock()
Expand Down Expand Up @@ -133,11 +133,11 @@ impl<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static> Server<D, T> {
Ok(())
}

async fn serve_moved_permanently<U: AsyncIO + Unpin>(
async fn serve_moved_permanently<U: Write + Unpin>(
&self,
conn: &mut U,
location: &str,
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
let phrase = "Moved Permanently";
self.start_response_headers(conn, 301, phrase).await?;
self.send_response_header(conn, "Content-Type", b"text/plain")
Expand All @@ -151,10 +151,10 @@ impl<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static> Server<D, T> {
Ok(())
}

async fn serve_not_found<U: AsyncIO + Unpin>(
async fn serve_not_found<U: Write + Unpin>(
&self,
conn: &mut U,
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
let phrase = "Not Found";
self.start_response_headers(conn, 404, phrase).await?;
self.send_response_header(conn, "Content-Type", b"text/plain")
Expand All @@ -166,12 +166,12 @@ impl<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static> Server<D, T> {
Ok(())
}

async fn start_response_headers<U: AsyncIO + Unpin>(
async fn start_response_headers<U: Write + Unpin>(
&self,
conn: &mut U,
status_code: usize,
reason_phrase: &str,
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
conn.write_all(b"HTTP/1.1 ").await?;
conn.write_all(status_code.to_string().as_bytes()).await?;
conn.write_all(b" ").await?;
Expand All @@ -180,23 +180,23 @@ impl<D: fat::BlockDevice + 'static, T: fat::TimeSource + 'static> Server<D, T> {
Ok(())
}

async fn send_response_header<U: AsyncIO + Unpin>(
async fn send_response_header<U: Write + Unpin>(
&self,
conn: &mut U,
name: &str,
value: &[u8],
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
conn.write_all(name.as_bytes()).await?;
conn.write_all(b": ").await?;
conn.write_all(value).await?;
conn.write_all(b"\r\n").await?;
Ok(())
}

async fn finish_response_headers<U: AsyncIO + Unpin>(
async fn finish_response_headers<U: Write + Unpin>(
&self,
conn: &mut U,
) -> Result<(), ClosedError<U::Error>> {
) -> Result<(), ReadExactError<U::Error>> {
conn.write_all(b"\r\n").await?;
Ok(())
}
Expand Down
2 changes: 2 additions & 0 deletions crates/private/meta/Cargo.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ mk {

sel4-async-block-io
sel4-async-block-io-fat
sel4-async-io
sel4-async-network
sel4-async-single-threaded-executor
sel4-async-time
sel4-async-unsync
sel4-atomic-ptr
sel4-bounce-buffer-allocator
sel4-immediate-sync-once-cell
sel4-immutable-cell
Expand Down
2 changes: 2 additions & 0 deletions crates/private/meta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ log = "0.4.17"
sel4 = { path = "../../sel4" }
sel4-async-block-io = { path = "../../sel4-async/block-io" }
sel4-async-block-io-fat = { path = "../../sel4-async/block-io/fat" }
sel4-async-io = { path = "../../sel4-async/io" }
sel4-async-network = { path = "../../sel4-async/network" }
sel4-async-single-threaded-executor = { path = "../../sel4-async/single-threaded-executor" }
sel4-async-time = { path = "../../sel4-async/time" }
sel4-async-unsync = { path = "../../sel4-async/unsync" }
sel4-atomic-ptr = { path = "../../sel4-atomic-ptr" }
sel4-bounce-buffer-allocator = { path = "../../sel4-bounce-buffer-allocator" }
sel4-config = { path = "../../sel4/config" }
sel4-externally-shared = { path = "../../sel4-externally-shared", features = ["unstable"] }
Expand Down
2 changes: 2 additions & 0 deletions crates/private/meta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,12 @@ definitely! {
sel4
sel4_async_block_io
sel4_async_block_io_fat
sel4_async_io
sel4_async_network
sel4_async_single_threaded_executor
sel4_async_time
sel4_async_unsync
sel4_atomic_ptr
sel4_bounce_buffer_allocator
sel4_config
sel4_externally_shared
Expand Down
15 changes: 15 additions & 0 deletions crates/sel4-async/io/Cargo.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright 2023, Colias Group, LLC
#
# SPDX-License-Identifier: BSD-2-Clause
#

{ mk, mkDefaultFrontmatterWithReuseArgs, defaultReuseFrontmatterArgs, versions }:

mk rec {
nix.frontmatter = mkDefaultFrontmatterWithReuseArgs (defaultReuseFrontmatterArgs // {
licenseID = package.license;
});
package.name = "sel4-async-io";
package.license = "MIT OR Apache-2.0";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Copyright 2023, Colias Group, LLC
#
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-License-Identifier: MIT OR Apache-2.0
#
#
# This file is generated from './Cargo.nix'. You can edit this file directly
Expand All @@ -10,11 +10,8 @@
#

[package]
name = "sel4-async-network-traits"
name = "sel4-async-io"
version = "0.1.0"
authors = ["Nick Spinale <[email protected]>"]
edition = "2021"
license = "BSD-2-Clause"

[dependencies]
futures = { version = "0.3.28", default-features = false, features = ["alloc"] }
license = "MIT OR Apache-2.0"
Loading
Loading