Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasohland committed Dec 26, 2023
1 parent efd0a5a commit f087bc3
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 636 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ members = [
"tools/netzwerker",
"tools/rist-io",
]
resolver = "2"
6 changes: 3 additions & 3 deletions crates/rist-rs-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ version = "0.0.1"
[dependencies]
rand = "0.8"
rist-rs-core = { path = "../rist-rs-core", features = ["std"] }
rist-rs-transport-dtls-openssl = { path = "../rist-rs-transport-dtls-openssl", optional = true }
rist-rs-types = { path = "../rist-rs-types", features = ["std"] }
rist-rs-util = { path = "../rist-rs-util", features = ["std"] }
socket2 = { version = "0.4" }
slab = { version = "0.4" }
mio = { version = "0.8", features = ["net"] }
tracing = { version = "0.1", default-features = false }

[dev-dependencies]
tracing-subscriber = { version = "0.3" }

[features]
default = ["openssl"]
default = []
log = ["tracing/log"]
openssl = ["rist-rs-transport-dtls-openssl"]
72 changes: 72 additions & 0 deletions crates/rist-rs-std/examples/runtime_simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use std::io;

use rist_rs_std::StdRuntime;
use rist_rs_types::traits::{
protocol::{Ctl, Protocol, ProtocolEvent, IOV},
runtime::{Runtime, self},
};

struct SimpleCtl;

impl Ctl for SimpleCtl {
type Error = io::Error;

type Output = ();

fn start() -> Self {
todo!()
}

fn shutdown() -> Self {
todo!()
}
}

struct Server {}

impl Server {
fn new() -> Self {
return Self { }
}
}

impl<R> Protocol<R> for Server
where
R: Runtime,
{
type Ctl = SimpleCtl;

fn run(
&mut self,
rt: &mut R,
iov: &[IOV<R, Self::Ctl>],
) -> ProtocolEvent<R> {
let mut buf = [0u8; 1500];
for ev in iov {
match ev {
IOV::Readable(socket) => {
match rt.recv_from(*socket, &mut buf) {
Err(err) => tracing::error!(?err, "recv_from"),
Ok((len, addr)) => {
let packet = buf.split_at(len).0;
if let Err(err) = rt.send_to(*socket, packet, addr) {
tracing::error!(?err, "send_to")
}
}
}
},
IOV::Writeable(_) => {},
IOV::Error(socket, err) => {
tracing::error!(?socket, ?err, "error");
}
IOV::Ctl(_) => todo!(),
}
}
ProtocolEvent::asap(&rt.get_default_clock())
}
}

fn main() {
let rt = StdRuntime::new();
rt.run(Server::new());
}
162 changes: 27 additions & 135 deletions crates/rist-rs-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,17 @@

use std::fmt::{Debug, Display};
use std::hash::Hash;
use std::io;
use std::net::SocketAddr as StdSocketAddr;
use std::time::Duration;

use rist_rs_types::traits::protocol::{Ctl, Protocol};
use rist_rs_types::traits::runtime::{self, Runtime, RuntimeError};
use rist_rs_types::traits::protocol::Protocol;
use rist_rs_types::traits::runtime::{self, Runtime};
use rist_rs_types::traits::time::clock::StdSystemClock;
use rist_rs_util::collections::static_vec::StaticVec;

mod net;

pub mod testing;
pub mod transport;

use net::Sockets as NetworkSockets;

#[derive(Debug)]
pub enum StdRuntimeError {
UnknownSocket(u32),
IOE(std::io::Error),
Panic(&'static str),
}

impl Display for StdRuntimeError {
fn fmt(&self, _: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
}
}

impl RuntimeError for StdRuntimeError {
fn is_not_ready(&self) -> bool {
if let StdRuntimeError::IOE(err) = self {
return err.kind() == std::io::ErrorKind::WouldBlock;
}
false
}

fn io_error(&self) -> Option<&io::Error> {
match self {
StdRuntimeError::IOE(e) => Some(e),
_ => None,
}
}

fn into_io_error(self) -> Option<io::Error> {
match self {
StdRuntimeError::IOE(e) => Some(e),
_ => None,
}
}
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SocketAddr {
Expand All @@ -71,7 +31,6 @@ impl runtime::SocketAddr for SocketAddr {

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Socket {
NetworkSocket(net::SocketId),
Empty,
}

Expand All @@ -84,27 +43,24 @@ impl Socket {
}

pub enum IoEventKind {
Accept(SocketAddr, Socket),
Readable(Socket),
Writable(Socket),
Error(StdRuntimeError),
Error(runtime::Error),
None,
}

pub struct IoEvent {
pub socket: Socket,
pub kind: IoEventKind,
pub buf: StaticVec<u8>,
pub len: usize,
}

impl IoEvent {
pub fn allocate(num: usize, buf_len: usize) -> Vec<IoEvent> {
pub fn allocate(num: usize) -> Vec<IoEvent> {
(0..num)
.map(|_| IoEvent {
kind: IoEventKind::None,
len: 0,
buf: StaticVec::new(buf_len),
socket: Socket::empty(),
})
.collect()
Expand All @@ -121,11 +77,9 @@ impl IoEvent {
}

pub struct StdRuntime {
network_sockets: NetworkSockets,
}

impl Runtime for StdRuntime {
type Error = StdRuntimeError;

type Clock = StdSystemClock;

Expand All @@ -137,104 +91,53 @@ impl Runtime for StdRuntime {
StdSystemClock
}

fn bind(&mut self, address: Self::SocketAddr) -> Result<Self::Socket, Self::Error> {
match address {
SocketAddr::NetworkAddress(address) => self
.network_sockets
.bind(address)
.map_err(StdRuntimeError::IOE)
.map(Into::into),
}
fn bind(&mut self, address: Self::SocketAddr, flags: runtime::SocketFlags) -> Result<Self::Socket, runtime::Error> {
Err(runtime::Error::AddrInUse)
}

fn connect(
&mut self,
local_sock_id: Self::Socket,
address: Self::SocketAddr,
) -> Result<Self::Socket, Self::Error> {
match address {
SocketAddr::NetworkAddress(address) => {
if let Socket::NetworkSocket(socket) = local_sock_id {
self.network_sockets
.connect(socket, address)
.map_err(StdRuntimeError::IOE)
.map(Into::into)
} else {
Err(StdRuntimeError::UnknownSocket(8))
}
}
}
) -> Result<Self::Socket, runtime::Error> {
Ok(local_sock_id)
}

fn send(&mut self, socket: Self::Socket, buf: &[u8]) -> Result<(), Self::Error> {
match socket {
Socket::NetworkSocket(socket) => self
.network_sockets
.send_non_blocking(socket, buf)
.map_err(StdRuntimeError::IOE),
Socket::Empty => Ok(()),
}
fn send(&mut self, socket: Self::Socket, buf: &[u8]) -> Result<(), runtime::Error> {
Ok(())
}

fn get_remote_address(&self, _: Self::Socket) -> Result<Self::SocketAddr, Self::Error> {
fn get_remote_address(&self, _: Self::Socket) -> Result<Self::SocketAddr, runtime::Error> {
todo!()
}

fn close(&mut self, socket: Self::Socket) {
match socket {
Socket::NetworkSocket(socket) => self.network_sockets.close(socket),
Socket::Empty => {}
}
}

fn send_to(&mut self, socket: Self::Socket, buf: &[u8], address: Self::SocketAddr) -> Result<(), runtime::Error> {
todo!()
}

fn recv(&mut self, socket: Self::Socket, buf: &mut [u8]) -> Result<usize, runtime::Error> {
todo!()
}

fn recv_from(&mut self, socket: Self::Socket, buf: &mut [u8]) -> Result<(usize, Self::SocketAddr), runtime::Error> {
todo!()
}
}

impl StdRuntime {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
network_sockets: NetworkSockets::new(),
}
}

pub fn run_protocol<P: Protocol<Self>>(mut self, mut protocol: P) {
let mut events = IoEvent::allocate(24, 1500);
protocol.ctl(&mut self, <P::Ctl as Ctl>::start()).unwrap();
loop {
self.network_sockets.poll_events(&mut events);
for event in events
.iter_mut()
.take_while(|e| !matches!(e.kind, IoEventKind::None))
{
match &event.kind {
IoEventKind::None => unreachable!(),
IoEventKind::Accept(remote_address, remote_socket_id) => {
protocol.accept(
&mut self,
event.socket,
*remote_socket_id,
*remote_address,
);
}
IoEventKind::Readable(remote_socket_id) => {
protocol.receive(
&mut self,
*remote_socket_id,
event.buf.split_at(event.len).0,
);
}
IoEventKind::Writable(socket) => {
protocol.writeable(&mut self, *socket);
}
IoEventKind::Error(error) => {
tracing::error!(?error, "socket error");
if let Socket::NetworkSocket(socket) = event.socket {
self.network_sockets.close(socket)
}
}
}
}
std::thread::sleep(Duration::from_millis(5));
}
pub fn run<P: Protocol<Self>>(mut self, mut protocol: P) {



}
}

Expand All @@ -249,26 +152,15 @@ impl Display for SocketAddr {
impl Display for Socket {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Socket::NetworkSocket(net) => Display::fmt(net, f),
Socket::Empty => write!(f, "<empty>"),
}
}
}

impl From<std::io::Error> for StdRuntimeError {
fn from(value: std::io::Error) -> Self {
StdRuntimeError::IOE(value)
}
}

impl From<StdSocketAddr> for SocketAddr {
fn from(value: StdSocketAddr) -> Self {
Self::NetworkAddress(value)
}
}

impl From<net::SocketId> for Socket {
fn from(value: net::SocketId) -> Self {
Self::NetworkSocket(value)
}
}
Loading

0 comments on commit f087bc3

Please sign in to comment.