Skip to content

Commit

Permalink
Unify Server and ServerBuffers; TaskHandler; remove unnecessary lifet…
Browse files Browse the repository at this point in the history
…imes
  • Loading branch information
ivmarkov committed Jan 29, 2024
1 parent 40789fc commit 9b707c1
Show file tree
Hide file tree
Showing 7 changed files with 247 additions and 99 deletions.
11 changes: 11 additions & 0 deletions edge-http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.0] - ????-??-??
* Remove unnecessary lifetimes when implementing the `embedded-svc` traits
* Server: new trait, `TaskHandler` which has an extra `task_id` parameter of type `usize`. This allows the request handling code to take advantage of the fact that - since the number of handlers when running a `Server` instance is fixed - it can store data related to handlers in a simple static array of the same size as the number of handlers that the server is running
* Breaking change: structures `Server` and `ServerBuffers` united, because `Server` was actually stateless. Turbofish syntax for specifying max number of HTTP headers and queue size is no longer necessary
14 changes: 5 additions & 9 deletions edge-http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ async fn request<'b, const N: usize, T: TcpConnect>(
### HTTP server

```rust
use edge_http::io::server::{Connection, Handler, Server, ServerBuffers};
use edge_http::io::server::{Connection, DefaultServer, Handler};
use edge_http::Method;

use embedded_nal_async_xtra::TcpListen;
Expand All @@ -107,14 +107,12 @@ fn main() {
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);

let mut buffers: ServerBuffers = ServerBuffers::new();
let mut server = DefaultServer::new();

futures_lite::future::block_on(run(&mut buffers)).unwrap();
futures_lite::future::block_on(run(&mut server)).unwrap();
}

pub async fn run<const P: usize, const B: usize>(
buffers: &mut ServerBuffers<P, B>,
) -> Result<(), anyhow::Error> {
pub async fn run(server: &mut DefaultServer) -> Result<(), anyhow::Error> {
let addr = "0.0.0.0:8881";

info!("Running HTTP server on {addr}");
Expand All @@ -123,9 +121,7 @@ pub async fn run<const P: usize, const B: usize>(
.listen(addr.parse().unwrap())
.await?;

let mut server: Server<_, _> = Server::new(acceptor, HttpHandler);

server.process::<2, P, B>(buffers).await?;
server.run(acceptor, HttpHandler).await?;

Ok(())
}
Expand Down
10 changes: 5 additions & 5 deletions edge-http/src/io/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ where

impl<'b, T, const N: usize> Read for Connection<'b, T, N>
where
T: TcpConnect + 'b,
T: TcpConnect,
{
async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
self.response_mut()?.io.read(buf).await
Expand All @@ -356,7 +356,7 @@ where

impl<'b, T, const N: usize> Write for Connection<'b, T, N>
where
T: TcpConnect + 'b,
T: TcpConnect,
{
async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.request_mut()?.io.write(buf).await
Expand Down Expand Up @@ -408,7 +408,7 @@ mod embedded_svc_compat {

impl<'b, T, const N: usize> Headers for super::Connection<'b, T, N>
where
T: TcpConnect + 'b,
T: TcpConnect,
{
fn header(&self, name: &str) -> Option<&'_ str> {
let response = self.response_ref().expect("Not in response state");
Expand All @@ -419,7 +419,7 @@ mod embedded_svc_compat {

impl<'b, T, const N: usize> Status for super::Connection<'b, T, N>
where
T: TcpConnect + 'b,
T: TcpConnect,
{
fn status(&self) -> u16 {
let response = self.response_ref().expect("Not in response state");
Expand All @@ -436,7 +436,7 @@ mod embedded_svc_compat {

impl<'b, T, const N: usize> Connection for super::Connection<'b, T, N>
where
T: TcpConnect + 'b,
T: TcpConnect,
{
type Read = Body<'b, T::Connection<'b>>;

Expand Down
Loading

0 comments on commit 9b707c1

Please sign in to comment.