-
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
Frank Lee
committed
Jan 4, 2025
1 parent
ebbcb27
commit 7eaf9d0
Showing
33 changed files
with
280 additions
and
135 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "bluefin-proto" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
thiserror = "2.0.9" | ||
rand = "0.9.0-beta.1" |
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,7 @@ | ||
/// The endpoint host type | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
pub enum BluefinHost { | ||
PackLeader, | ||
PackFollower, | ||
Client, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod state_machine; |
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,116 @@ | ||
use crate::context::BluefinHost; | ||
use crate::error::BluefinError; | ||
use crate::BluefinResult; | ||
use rand::{rng, Rng}; | ||
use std::cmp::PartialEq; | ||
use std::collections::VecDeque; | ||
|
||
#[derive(Eq, Debug, PartialEq)] | ||
enum State { | ||
ServerStart, | ||
PendingAccept, | ||
RecvClientHello, | ||
SentServerHello, | ||
RecvClientAck, | ||
ClientStart, | ||
RequestingAccept, | ||
SentClientHello, | ||
RecvServerHello, | ||
SentClientAck, | ||
Conn, | ||
} | ||
|
||
struct PendingAccept { | ||
src_conn_id: u32, | ||
packet_number: u64, | ||
} | ||
|
||
impl PendingAccept { | ||
pub fn new() -> PendingAccept { | ||
Self { | ||
src_conn_id: rng().random(), | ||
packet_number: rng().random(), | ||
} | ||
} | ||
} | ||
|
||
pub struct Transmit { | ||
src_conn_id: u32, | ||
packet_number: u64, | ||
} | ||
|
||
impl Transmit { | ||
pub fn new() -> Transmit { | ||
Self { | ||
src_conn_id: rng().random(), | ||
packet_number: rng().random(), | ||
} | ||
} | ||
} | ||
|
||
pub struct HandshakeHandler { | ||
state: State, | ||
pending_accepts: VecDeque<PendingAccept>, | ||
transmit_queue: VecDeque<Transmit>, | ||
} | ||
|
||
impl HandshakeHandler { | ||
pub fn new(host_type: BluefinHost) -> HandshakeHandler { | ||
let pending_accepts = VecDeque::new(); | ||
let transmit_queue = VecDeque::new(); | ||
match host_type { | ||
BluefinHost::Client => Self { | ||
state: State::ClientStart, | ||
pending_accepts, | ||
transmit_queue, | ||
}, | ||
BluefinHost::PackLeader => Self { | ||
state: State::ServerStart, | ||
pending_accepts, | ||
transmit_queue, | ||
}, | ||
_ => todo!(), | ||
} | ||
} | ||
|
||
/// This initiates the handshake. For the pack leader we enqueue a pending accept request, | ||
/// which indicates that the server is ready to accept a connection. Each pending accept request | ||
/// represents one potential Bluefin connection. For the client we enqueue a transmit request | ||
/// which represents a client-hello packet to be sent on the wire. | ||
pub fn begin(&mut self) -> BluefinResult<()> { | ||
match &self.state { | ||
// Push a pending accept, transition into PendingAccept state and wait | ||
State::ServerStart => { | ||
self.pending_accepts.push_back(PendingAccept::new()); | ||
self.state = State::PendingAccept; | ||
Ok(()) | ||
} | ||
// Push an accept request into the transmit queue, transition into RequestingAccept | ||
// state and wait. | ||
State::ClientStart => { | ||
self.transmit_queue.push_back(Transmit::new()); | ||
self.state = State::RequestingAccept; | ||
Ok(()) | ||
} | ||
// It is not valid to call this function and be in any state other than ServerStart | ||
// or ClientStart. Fail here. | ||
other => Err(BluefinError::InvalidState(format!( | ||
"Cannot begin handshake at state: {:?}", | ||
other | ||
))), | ||
} | ||
} | ||
|
||
pub fn poll_tx_client_hello(&mut self) -> BluefinResult<Option<Transmit>> { | ||
if self.state != State::RequestingAccept { | ||
return Err(BluefinError::InvalidState( | ||
"Cannot poll tx client hello".to_string(), | ||
)); | ||
} | ||
Ok(self.transmit_queue.pop_front()) | ||
} | ||
|
||
pub fn handle(&mut self) -> BluefinResult<()> { | ||
Ok(()) | ||
} | ||
} |
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,7 @@ | ||
use crate::error::BluefinError; | ||
|
||
pub mod context; | ||
pub mod error; | ||
pub mod handshake; | ||
|
||
pub type BluefinResult<T> = Result<T, BluefinError>; |
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 @@ | ||
[package] | ||
name = "bluefin" | ||
version.workspace = true | ||
edition.workspace = true | ||
description.workspace = true | ||
license.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
bluefin-proto = { path = "../bluefin-proto" } | ||
local-ip-address = { workspace = true } | ||
rand = { workspace = true } | ||
rstest = { workspace = true } | ||
tokio = { workspace = true, features = ["full", "tracing"] } | ||
libc = { workspace = true } | ||
socket2 = { workspace = true } | ||
|
||
[dev-dependencies] | ||
local-ip-address = "0.6.3" | ||
rstest = "0.24.0" | ||
|
||
[[bin]] | ||
name = "client" | ||
path = "src/bin/client.rs" | ||
|
||
[[bin]] | ||
name = "server" | ||
path = "src/bin/server.rs" | ||
|
||
[lints] | ||
workspace = true |
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 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 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
File renamed without changes.
File renamed without changes.
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
Oops, something went wrong.