allow getting peer in tcp
This commit is contained in:
parent
2446272181
commit
89d14146be
5 changed files with 44 additions and 2 deletions
|
@ -50,6 +50,9 @@ construct Stream {
|
||||||
close { | with this ;
|
close { | with this ;
|
||||||
this:id close-stream
|
this:id close-stream
|
||||||
}
|
}
|
||||||
|
get-peer { ip:str port:int | with this ;
|
||||||
|
this:id get-stream-peer
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
construct StreamType {
|
construct StreamType {
|
||||||
|
|
|
@ -13,6 +13,7 @@ pub const REPL: &str = include_str!("../spl/repl.spl");
|
||||||
pub const PURE: &str = include_str!("../spl/pure.spl");
|
pub const PURE: &str = include_str!("../spl/pure.spl");
|
||||||
pub const TIME: &str = include_str!("../spl/time.spl");
|
pub const TIME: &str = include_str!("../spl/time.spl");
|
||||||
pub const SERVER: &str = include_str!("../spl/server.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 const NOP: &str = "";
|
||||||
|
|
||||||
pub fn register(runtime: &mut Runtime) {
|
pub fn register(runtime: &mut Runtime) {
|
||||||
|
@ -30,6 +31,7 @@ pub fn register(runtime: &mut Runtime) {
|
||||||
insert("pure.spl", PURE);
|
insert("pure.spl", PURE);
|
||||||
insert("time.spl", TIME);
|
insert("time.spl", TIME);
|
||||||
insert("server.spl", SERVER);
|
insert("server.spl", SERVER);
|
||||||
|
insert("http/server.spl", HTTP_SERVER);
|
||||||
insert("nop.spl", NOP);
|
insert("nop.spl", NOP);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,11 @@ use crate::{mutex::Mut, runtime::*};
|
||||||
|
|
||||||
static IS_INITIALIZED: LazyLock<Arc<Mut<bool>>> = LazyLock::new(|| Arc::new(Mut::new(false)));
|
static IS_INITIALIZED: LazyLock<Arc<Mut<bool>>> = LazyLock::new(|| Arc::new(Mut::new(false)));
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct StreamExtraData {
|
||||||
|
peer: Option<(String, u16)>,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn register(r: &mut Stack, o: Arc<Frame>) {
|
pub fn register(r: &mut Stack, o: Arc<Frame>) {
|
||||||
if !*IS_INITIALIZED.lock_ro() {
|
if !*IS_INITIALIZED.lock_ro() {
|
||||||
register_stream_type("file", stream_file);
|
register_stream_type("file", stream_file);
|
||||||
|
@ -20,7 +25,7 @@ pub fn register(r: &mut Stack, o: Arc<Frame>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Fn = fn(&mut Stack) -> OError;
|
type Fn = fn(&mut Stack) -> OError;
|
||||||
let fns: [(&str, Fn, u32); 10] = [
|
let fns: [(&str, Fn, u32); 11] = [
|
||||||
("new-stream", new_stream, 1),
|
("new-stream", new_stream, 1),
|
||||||
("write-stream", write_stream, 1),
|
("write-stream", write_stream, 1),
|
||||||
("write-all-stream", write_all_stream, 0),
|
("write-all-stream", write_all_stream, 0),
|
||||||
|
@ -31,6 +36,7 @@ pub fn register(r: &mut Stack, o: Arc<Frame>) {
|
||||||
("new-server-stream", new_server_stream, 1),
|
("new-server-stream", new_server_stream, 1),
|
||||||
("accept-server-stream", accept_server_stream, 1),
|
("accept-server-stream", accept_server_stream, 1),
|
||||||
("close-server-stream", close_server_stream, 0),
|
("close-server-stream", close_server_stream, 0),
|
||||||
|
("get-stream-peer", get_stream_peer, 2),
|
||||||
];
|
];
|
||||||
for f in fns {
|
for f in fns {
|
||||||
r.define_func(
|
r.define_func(
|
||||||
|
|
|
@ -11,6 +11,7 @@ use crate::*;
|
||||||
|
|
||||||
use fs::OpenOptions;
|
use fs::OpenOptions;
|
||||||
use mutex::Mut;
|
use mutex::Mut;
|
||||||
|
use stream::StreamExtraData;
|
||||||
|
|
||||||
static STREAM_TYPES: LazyLock<Arc<Mut<HashMap<String, StreamType>>>> =
|
static STREAM_TYPES: LazyLock<Arc<Mut<HashMap<String, StreamType>>>> =
|
||||||
LazyLock::new(|| Arc::new(Mut::new(HashMap::new())));
|
LazyLock::new(|| Arc::new(Mut::new(HashMap::new())));
|
||||||
|
@ -47,6 +48,7 @@ pub struct Stream {
|
||||||
pub(super) reader: Box<dyn Read + Send + Sync + 'static>,
|
pub(super) reader: Box<dyn Read + Send + Sync + 'static>,
|
||||||
pub(super) _writer_storage: Option<Box<dyn Write + Send + Sync + 'static>>,
|
pub(super) _writer_storage: Option<Box<dyn Write + Send + Sync + 'static>>,
|
||||||
pub(super) writer: &'static mut (dyn Write + Send + Sync + 'static),
|
pub(super) writer: &'static mut (dyn Write + Send + Sync + 'static),
|
||||||
|
pub extra: StreamExtraData,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Stream {
|
impl Stream {
|
||||||
|
@ -60,6 +62,7 @@ impl Stream {
|
||||||
},
|
},
|
||||||
_writer_storage: None,
|
_writer_storage: None,
|
||||||
reader: rw,
|
reader: rw,
|
||||||
|
extra: StreamExtraData::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn new_split(
|
pub fn new_split(
|
||||||
|
@ -75,8 +78,14 @@ impl Stream {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
},
|
},
|
||||||
_writer_storage: Some(bx),
|
_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 {
|
impl Read for Stream {
|
||||||
|
@ -340,3 +349,24 @@ pub(super) fn stream_cmd(stack: &mut Stack) -> Result<Stream, Error> {
|
||||||
command.stdin.take().unwrap(),
|
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(())
|
||||||
|
}
|
||||||
|
|
|
@ -99,7 +99,8 @@ pub(crate) fn server_stream_tcp(stack: &mut Stack) -> Result<ServerStream, Error
|
||||||
let socket = tcp
|
let socket = tcp
|
||||||
.accept()
|
.accept()
|
||||||
.map_err(|e| stack.error(ErrorKind::IO(format!("{e:?}"))))?;
|
.map_err(|e| stack.error(ErrorKind::IO(format!("{e:?}"))))?;
|
||||||
Ok(Stream::new(socket.0))
|
Ok(Stream::new(socket.0)
|
||||||
|
.append_extra(move |d| d.peer = Some((socket.1.ip().to_string(), socket.1.port()))))
|
||||||
});
|
});
|
||||||
Ok(stream)
|
Ok(stream)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue