-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ef657d
commit 7769131
Showing
25 changed files
with
556 additions
and
665 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// For format details, see https://aka.ms/devcontainer.json. For config options, see the | ||
// README at: https://github.com/devcontainers/templates/tree/main/src/rust | ||
{ | ||
"name": "Rust", | ||
// Or use a Dockerfile or Docker Compose file. More info: https://containers.dev/guide/dockerfile | ||
"image": "mcr.microsoft.com/devcontainers/rust:1-1-bullseye", | ||
// Use 'mounts' to make the cargo cache persistent in a Docker Volume. | ||
// "mounts": [ | ||
// { | ||
// "source": "devcontainer-cargo-cache-${devcontainerId}", | ||
// "target": "/usr/local/cargo", | ||
// "type": "volume" | ||
// } | ||
// ] | ||
// Features to add to the dev container. More info: https://containers.dev/features. | ||
// "features": {}, | ||
// Use 'forwardPorts' to make a list of ports inside the container available locally. | ||
// "forwardPorts": [], | ||
// Use 'postCreateCommand' to run commands after the container is created. | ||
// "postCreateCommand": "rustc --version", | ||
// Configure tool-specific properties. | ||
"customizations": { | ||
"vscode": { | ||
"extensions": [ | ||
"swellaby.rust-pack" | ||
] | ||
} | ||
} | ||
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. | ||
// "remoteUser": "root" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# To get started with Dependabot version updates, you'll need to specify which | ||
# package ecosystems to update and where the package manifests are located. | ||
# Please see the documentation for more information: | ||
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates | ||
# https://containers.dev/guide/dependabot | ||
|
||
version: 2 | ||
updates: | ||
- package-ecosystem: "devcontainers" | ||
directory: "/" | ||
schedule: | ||
interval: weekly |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use anyhow::anyhow; | ||
use core::fmt::Debug; | ||
use core::str::Utf8Error; | ||
use embedded_websocket::framer_async::FramerError; | ||
use thiserror_no_std::Error; | ||
|
||
#[derive(Debug, PartialEq, Eq, Error)] | ||
pub enum WebsocketError<E: Debug> { | ||
#[error("Stream is not connected.")] | ||
NotConnected, | ||
// FramerError | ||
#[error("I/O error: {0:?}")] | ||
Io(E), | ||
#[error("Frame too large (size: {0:?})")] | ||
FrameTooLarge(usize), | ||
#[error("Failed to interpret u8 to string (error: {0:?})")] | ||
Utf8(Utf8Error), | ||
#[error("Invalid HTTP header")] | ||
HttpHeader, | ||
#[error("Websocket error: {0:?}")] | ||
WebSocket(embedded_websocket::Error), | ||
#[error("Disconnected")] | ||
Disconnected, | ||
#[error("Read buffer is too small (size: {0:?})")] | ||
RxBufferTooSmall(usize), | ||
} | ||
|
||
impl<E: Debug> From<FramerError<E>> for WebsocketError<E> { | ||
fn from(e: FramerError<E>) -> Self { | ||
match e { | ||
FramerError::Io(e) => WebsocketError::Io(e), | ||
FramerError::FrameTooLarge(size) => WebsocketError::FrameTooLarge(size), | ||
FramerError::Utf8(e) => WebsocketError::Utf8(e), | ||
FramerError::HttpHeader(_) => WebsocketError::HttpHeader, | ||
FramerError::WebSocket(e) => WebsocketError::WebSocket(e), | ||
FramerError::Disconnected => WebsocketError::Disconnected, | ||
FramerError::RxBufferTooSmall(size) => WebsocketError::RxBufferTooSmall(size), | ||
} | ||
} | ||
} | ||
|
||
impl<E: Debug> Into<anyhow::Error> for WebsocketError<E> { | ||
fn into(self) -> anyhow::Error { | ||
anyhow!(self) | ||
} | ||
} | ||
|
||
#[cfg(feature = "std")] | ||
impl<E: Debug> alloc::error::Error for WebsocketError<E> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,76 @@ | ||
mod async_websocket_client; | ||
pub mod errors; | ||
pub mod exceptions; | ||
|
||
pub use async_websocket_client::*; | ||
use core::marker::PhantomData; | ||
|
||
use alloc::string::ToString; | ||
use anyhow::Result; | ||
use embedded_io_async::{Read, Write}; | ||
use embedded_websocket::{framer_async::Framer, Client, WebSocketClient, WebSocketOptions}; | ||
use exceptions::WebsocketError; | ||
use rand_core::RngCore; | ||
use url::Url; | ||
|
||
use crate::Err; | ||
|
||
pub struct WebsocketClosed; | ||
pub struct WebsocketOpen; | ||
|
||
pub struct AsyncWebsocketClient<T: RngCore, Status = WebsocketClosed> { | ||
inner: Framer<T, Client>, | ||
status: PhantomData<Status>, | ||
} | ||
|
||
impl<T: RngCore, Status> AsyncWebsocketClient<T, Status> { | ||
pub fn is_open(&self) -> bool { | ||
core::any::type_name::<Status>() == core::any::type_name::<WebsocketOpen>() | ||
} | ||
} | ||
|
||
impl<T: RngCore> AsyncWebsocketClient<T, WebsocketClosed> { | ||
pub async fn open<S>( | ||
buf: &mut [u8], | ||
stream: &mut S, | ||
uri: Url, | ||
rng: T, | ||
) -> Result<AsyncWebsocketClient<T, WebsocketOpen>> | ||
where | ||
S: Read + Write + Unpin, | ||
{ | ||
// replace the scheme with http or https | ||
let scheme = match uri.scheme() { | ||
"wss" => "https", | ||
"ws" => "http", | ||
_ => uri.scheme(), | ||
}; | ||
let port = match uri.port() { | ||
Some(port) => port, | ||
None => match uri.scheme() { | ||
"wss" => 443, | ||
"ws" => 80, | ||
_ => 80, | ||
}, | ||
} | ||
.to_string(); | ||
let path = uri.path(); | ||
let host = match uri.host_str() { | ||
Some(host) => host, | ||
None => return Err(WebsocketError::Disconnected.into()), | ||
}; | ||
let origin = scheme.to_string() + "://" + host + ":" + &port + path; | ||
let websocket_options = WebSocketOptions { | ||
path, | ||
host, | ||
origin: &origin, | ||
sub_protocols: None, | ||
additional_headers: None, | ||
}; | ||
let websocket = Framer::new(WebSocketClient::new_client(rng)); | ||
match websocket.connect(stream, buf, &websocket_options).await { | ||
Ok(_) => Ok(AsyncWebsocketClient { | ||
inner: websocket, | ||
status: PhantomData::<WebsocketOpen>, | ||
}), | ||
Err(e) => Err!(e), | ||
} | ||
} | ||
} |
Oops, something went wrong.