mirror of
https://github.com/spacedriveapp/spacedrive
synced 2024-07-02 10:03:28 +00:00
Improve Tunnel error handling (#2479)
p2p: create and use TunnelError enum
This commit is contained in:
parent
dcbe888d03
commit
ed193a3ba5
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -9455,6 +9455,7 @@ name = "sd-p2p-tunnel"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"sd-p2p",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
|
|
|
@ -11,3 +11,4 @@ repository.workspace = true
|
|||
sd-p2p = { path = "../p2p" }
|
||||
|
||||
tokio = { workspace = true, features = ["io-util"] }
|
||||
thiserror = { workspace = true }
|
||||
|
|
|
@ -6,34 +6,44 @@ use std::{
|
|||
|
||||
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, ReadBuf};
|
||||
|
||||
use thiserror::Error;
|
||||
|
||||
use sd_p2p::UnicastStream;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum TunnelError {
|
||||
#[error("Error writing discriminator.")]
|
||||
DiscriminatorWriteError,
|
||||
#[error("Error reading discriminator. Is this stream actually a tunnel?")]
|
||||
DiscriminatorReadError,
|
||||
#[error("Invalid discriminator. Is this stream actually a tunnel?")]
|
||||
InvalidDiscriminator,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Tunnel {
|
||||
stream: UnicastStream,
|
||||
}
|
||||
|
||||
impl Tunnel {
|
||||
// TODO: Proper errors
|
||||
pub async fn initiator(mut stream: UnicastStream) -> Result<Self, &'static str> {
|
||||
pub async fn initiator(mut stream: UnicastStream) -> Result<Self, TunnelError> {
|
||||
stream
|
||||
.write_all(&[b'T'])
|
||||
.await
|
||||
.map_err(|_| "Error writing discriminator")?;
|
||||
.map_err(|_| TunnelError::DiscriminatorWriteError)?;
|
||||
|
||||
// TODO: Do pairing + authentication
|
||||
|
||||
Ok(Self { stream })
|
||||
}
|
||||
|
||||
// TODO: Proper errors
|
||||
pub async fn responder(mut stream: UnicastStream) -> Result<Self, &'static str> {
|
||||
pub async fn responder(mut stream: UnicastStream) -> Result<Self, TunnelError> {
|
||||
let discriminator = stream
|
||||
.read_u8()
|
||||
.await
|
||||
.map_err(|_| "Error reading discriminator. Is this stream actually a tunnel?")?;
|
||||
.map_err(|_| TunnelError::DiscriminatorReadError)?;
|
||||
if discriminator != b'T' {
|
||||
return Err("Invalid discriminator. Is this stream actually a tunnel?");
|
||||
return Err(TunnelError::InvalidDiscriminator);
|
||||
}
|
||||
|
||||
// TODO: Do pairing + authentication
|
||||
|
|
Loading…
Reference in a new issue