|
| 1 | +use std::ops::{Deref, DerefMut}; |
| 2 | +use std::path::PathBuf; |
| 3 | +use std::result::Result as StdResult; |
| 4 | + |
| 5 | +use mlua::{Lua, Result, String as LuaString, Table, UserData, UserDataMethods, UserDataRegistry}; |
| 6 | +use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; |
| 7 | + |
| 8 | +use crate::time::Duration; |
| 9 | + |
| 10 | +pub struct UnixStream { |
| 11 | + pub(crate) stream: tokio::net::UnixStream, |
| 12 | + pub(crate) read_timeout: Option<Duration>, |
| 13 | + pub(crate) write_timeout: Option<Duration>, |
| 14 | +} |
| 15 | + |
| 16 | +impl Deref for UnixStream { |
| 17 | + type Target = tokio::net::UnixStream; |
| 18 | + |
| 19 | + #[inline] |
| 20 | + fn deref(&self) -> &Self::Target { |
| 21 | + &self.stream |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +impl DerefMut for UnixStream { |
| 26 | + #[inline] |
| 27 | + fn deref_mut(&mut self) -> &mut Self::Target { |
| 28 | + &mut self.stream |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl From<tokio::net::UnixStream> for UnixStream { |
| 33 | + fn from(stream: tokio::net::UnixStream) -> Self { |
| 34 | + UnixStream { |
| 35 | + stream, |
| 36 | + read_timeout: None, |
| 37 | + write_timeout: None, |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl UserData for UnixStream { |
| 43 | + fn register(registry: &mut UserDataRegistry<Self>) { |
| 44 | + registry.add_async_function("connect", connect); |
| 45 | + |
| 46 | + registry.add_method("local_addr", |_, this, ()| { |
| 47 | + Ok(this.local_addr().map(|addr| { |
| 48 | + addr.as_pathname() |
| 49 | + .map(|p| p.to_string_lossy().to_string()) |
| 50 | + .unwrap_or_else(|| "(unnamed)".to_string()) |
| 51 | + })?) |
| 52 | + }); |
| 53 | + |
| 54 | + registry.add_method("peer_addr", |_, this, ()| { |
| 55 | + Ok(this.peer_addr().map(|addr| { |
| 56 | + addr.as_pathname() |
| 57 | + .map(|p| p.to_string_lossy().to_string()) |
| 58 | + .unwrap_or_else(|| "(unnamed)".to_string()) |
| 59 | + })?) |
| 60 | + }); |
| 61 | + |
| 62 | + registry.add_method_mut("set_read_timeout", |_, this, dur: Option<Duration>| { |
| 63 | + this.read_timeout = dur; |
| 64 | + Ok(()) |
| 65 | + }); |
| 66 | + |
| 67 | + registry.add_method_mut("set_write_timeout", |_, this, dur: Option<Duration>| { |
| 68 | + this.write_timeout = dur; |
| 69 | + Ok(()) |
| 70 | + }); |
| 71 | + |
| 72 | + registry.add_async_method_mut("read", |lua, mut this, size: usize| async move { |
| 73 | + let mut buf = vec![0; size]; |
| 74 | + let n = with_io_timeout!(this.read_timeout, this.read(&mut buf)); |
| 75 | + let n = lua_try!(n); |
| 76 | + buf.truncate(n); |
| 77 | + Ok(Ok(lua.create_string(buf)?)) |
| 78 | + }); |
| 79 | + |
| 80 | + registry.add_async_method_mut("read_to_end", |lua, mut this, ()| async move { |
| 81 | + let mut buf = Vec::new(); |
| 82 | + let n = with_io_timeout!(this.read_timeout, this.read_to_end(&mut buf)); |
| 83 | + let _n = lua_try!(n); |
| 84 | + Ok(Ok(lua.create_string(buf)?)) |
| 85 | + }); |
| 86 | + |
| 87 | + registry.add_async_method_mut("write", |_, mut this, data: LuaString| async move { |
| 88 | + let n = with_io_timeout!(this.write_timeout, this.write(&data.as_bytes())); |
| 89 | + let n = lua_try!(n); |
| 90 | + Ok(Ok(n)) |
| 91 | + }); |
| 92 | + |
| 93 | + registry.add_async_method_mut("write_all", |_, mut this, data: LuaString| async move { |
| 94 | + let r = with_io_timeout!(this.write_timeout, this.write_all(&data.as_bytes())); |
| 95 | + lua_try!(r); |
| 96 | + Ok(Ok(true)) |
| 97 | + }); |
| 98 | + |
| 99 | + registry.add_async_method_mut("flush", |_, mut this, ()| async move { |
| 100 | + let r = with_io_timeout!(this.write_timeout, this.flush()); |
| 101 | + lua_try!(r); |
| 102 | + Ok(Ok(true)) |
| 103 | + }); |
| 104 | + |
| 105 | + registry.add_async_method_mut("shutdown", |_, mut this, ()| async move { |
| 106 | + lua_try!(this.shutdown().await); |
| 107 | + Ok(Ok(true)) |
| 108 | + }); |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +pub async fn connect( |
| 113 | + _: Lua, |
| 114 | + (path, params): (String, Option<Table>), |
| 115 | +) -> Result<StdResult<UnixStream, String>> { |
| 116 | + let path = PathBuf::from(path); |
| 117 | + |
| 118 | + let timeout = opt_param!(Duration, params, "timeout")?; // A single timeout for any operation |
| 119 | + let connect_timeout = opt_param!(Duration, params, "connect_timeout")?.or(timeout); |
| 120 | + let read_timeout = opt_param!(Duration, params, "read_timeout")?.or(timeout); |
| 121 | + let write_timeout = opt_param!(Duration, params, "write_timeout")?.or(timeout); |
| 122 | + |
| 123 | + let stream = with_io_timeout!(connect_timeout, tokio::net::UnixStream::connect(path)); |
| 124 | + let stream = lua_try!(stream); |
| 125 | + |
| 126 | + Ok(Ok(UnixStream { |
| 127 | + stream, |
| 128 | + read_timeout, |
| 129 | + write_timeout, |
| 130 | + })) |
| 131 | +} |
0 commit comments