From 89d14146be3d89ea346d902b5cc1ff589f1bd94f Mon Sep 17 00:00:00 2001 From: TudbuT Date: Thu, 12 Sep 2024 16:27:44 +0200 Subject: [PATCH] allow getting peer in tcp --- spl/stream.spl | 3 +++ src/stdlib.rs | 2 ++ src/stream/mod.rs | 8 +++++++- src/stream/rw.rs | 30 ++++++++++++++++++++++++++++++ src/stream/server.rs | 3 ++- 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/spl/stream.spl b/spl/stream.spl index 07fdd67..bde247c 100644 --- a/spl/stream.spl +++ b/spl/stream.spl @@ -50,6 +50,9 @@ construct Stream { close { | with this ; this:id close-stream } + get-peer { ip:str port:int | with this ; + this:id get-stream-peer + } } construct StreamType { diff --git a/src/stdlib.rs b/src/stdlib.rs index 21005e3..b3e59c3 100644 --- a/src/stdlib.rs +++ b/src/stdlib.rs @@ -13,6 +13,7 @@ pub const REPL: &str = include_str!("../spl/repl.spl"); pub const PURE: &str = include_str!("../spl/pure.spl"); pub const TIME: &str = include_str!("../spl/time.spl"); pub const SERVER: &str = include_str!("../spl/server.spl"); +pub const HTTP_SERVER: &str = include_str!("../spl/http/server.spl"); pub const NOP: &str = ""; pub fn register(runtime: &mut Runtime) { @@ -30,6 +31,7 @@ pub fn register(runtime: &mut Runtime) { insert("pure.spl", PURE); insert("time.spl", TIME); insert("server.spl", SERVER); + insert("http/server.spl", HTTP_SERVER); insert("nop.spl", NOP); } } diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 88649b7..eb1af17 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -9,6 +9,11 @@ use crate::{mutex::Mut, runtime::*}; static IS_INITIALIZED: LazyLock>> = LazyLock::new(|| Arc::new(Mut::new(false))); +#[derive(Default)] +pub struct StreamExtraData { + peer: Option<(String, u16)>, +} + pub fn register(r: &mut Stack, o: Arc) { if !*IS_INITIALIZED.lock_ro() { register_stream_type("file", stream_file); @@ -20,7 +25,7 @@ pub fn register(r: &mut Stack, o: Arc) { } type Fn = fn(&mut Stack) -> OError; - let fns: [(&str, Fn, u32); 10] = [ + let fns: [(&str, Fn, u32); 11] = [ ("new-stream", new_stream, 1), ("write-stream", write_stream, 1), ("write-all-stream", write_all_stream, 0), @@ -31,6 +36,7 @@ pub fn register(r: &mut Stack, o: Arc) { ("new-server-stream", new_server_stream, 1), ("accept-server-stream", accept_server_stream, 1), ("close-server-stream", close_server_stream, 0), + ("get-stream-peer", get_stream_peer, 2), ]; for f in fns { r.define_func( diff --git a/src/stream/rw.rs b/src/stream/rw.rs index dc05aaa..dc43cc7 100644 --- a/src/stream/rw.rs +++ b/src/stream/rw.rs @@ -11,6 +11,7 @@ use crate::*; use fs::OpenOptions; use mutex::Mut; +use stream::StreamExtraData; static STREAM_TYPES: LazyLock>>> = LazyLock::new(|| Arc::new(Mut::new(HashMap::new()))); @@ -47,6 +48,7 @@ pub struct Stream { pub(super) reader: Box, pub(super) _writer_storage: Option>, pub(super) writer: &'static mut (dyn Write + Send + Sync + 'static), + pub extra: StreamExtraData, } impl Stream { @@ -60,6 +62,7 @@ impl Stream { }, _writer_storage: None, reader: rw, + extra: StreamExtraData::default(), } } pub fn new_split( @@ -75,8 +78,14 @@ impl Stream { .unwrap() }, _writer_storage: Some(bx), + extra: StreamExtraData::default(), } } + + pub fn append_extra(mut self, f: impl Fn(&mut StreamExtraData)) -> Stream { + f(&mut self.extra); + self + } } impl Read for Stream { @@ -340,3 +349,24 @@ pub(super) fn stream_cmd(stack: &mut Stack) -> Result { command.stdin.take().unwrap(), )) } + +pub(super) fn get_stream_peer(stack: &mut Stack) -> OError { + require_on_stack!(id, Mega, stack, "get-stream-peer"); + let Some((addr, port)) = runtime(|rt| -> Result<_, Error> { + Ok(rt + .get_stream(id as u128) + .ok_or(stack.error(ErrorKind::VariableNotFound(format!("__stream-{id}"))))? + .lock_ro() + .extra + .peer + .clone()) + })? + else { + stack.push(Value::Null.spl()); + stack.push(Value::Null.spl()); + return Ok(()); + }; + stack.push(addr.spl()); + stack.push((port as i32).spl()); + Ok(()) +} diff --git a/src/stream/server.rs b/src/stream/server.rs index 7dbeeef..c79b8be 100644 --- a/src/stream/server.rs +++ b/src/stream/server.rs @@ -99,7 +99,8 @@ pub(crate) fn server_stream_tcp(stack: &mut Stack) -> Result