Rustã§Unix Domain Socketãæ±ãæ¹æ³
"rust domain socket"ã¨ãã§æ¤ç´¢ããã¨ããããå¼ã£ããããã©ãããããã©ã¤ãã©ãªã®æ´æ°ãæ©ãã¦æ å ±ãå¤ãã£ããããã®ã§ããã«ç¾ç¶ãæ¸ãã¾ãï¼ãã®æç« ãããã«å¤ããªããããããªãã®ã§å¾ããèªã人ã¯æ³¨æãã¦ãã ãã..ï¼
åºæ¬
std::os::unix::net
- å 㯠rust-lang-nursery/unix-socketã ã£ããã®ãï¼stdã«åãè¾¼ã¾ãããã®ï¼rfcï¼
std::net::TcpListener
çã¨åæ§ã®ä½¿ãåæ
- nix
- setsockoptãããã¨ããancillary dataãsendmsgã§éãããã¨ãã«æç¨ï¼ä¾ï¼
- libc
- 硬派ãªäººåã
async/await
- mio-uds
- mioãdomain socketã«å¯¾å¿ããããã®
- å¾è¿°ã®tokio, async-stdããããå©ç¨
tokio::net::unix
- å
ã¯
tokio-uds
ã¨ããåå¥ã®ãªãã¸ããªã ã£ãã®ãï¼tokio
æ¬ä½ã«å¸åããï¼ããã«ä»ã§ã¯tokio-net
ã®ä¸é¨ï¼ - async/awaitããã人åã
- å
ã¯
async_std::os::unix::net
- async-stdç
- ç¾å¨ãã°ã£ã¦ã¦åãã¾ãã
- 1.0ãåºãããã«ã¯ãã£ã¨ä¿®æ£ããã¦ããã¯ã
HTTPãµã¼ã
- actix-web
- udsã®ãµãã¼ããããï¼ä¾ï¼
- hyper
- hyperlocalã使ã
ä¾
std::os::unix::net::{UnixListner, UnixStream}
use std::fs; use std::io::prelude::*; use std::os::unix::net::{UnixListener, UnixStream}; use std::path::Path; use std::thread; fn handle_client(mut stream: UnixStream) -> std::io::Result<()> { let mut buf = [0; 1024]; let n = stream.read(&mut buf)?; let s = String::from_utf8_lossy(&buf[..n]); println!("{}", s); Ok(()) } fn main() -> Result<(), Box<dyn std::error::Error>> { let sockfile = Path::new("/tmp/uds.sock"); if sockfile.exists() { fs::remove_file(&sockfile)?; } let listner = UnixListener::bind(sockfile)?; for stream in listner.incoming() { let stream = stream?; thread::spawn(move || handle_client(stream).unwrap()); } Ok(()) }
// cargo-deps: tokio="0.2.0-alpha.6", tokio-net="0.2.0-alpha.6", futures-preview="0.3.0-alpha.19" use std::fs; use std::path::Path; use futures::StreamExt; use tokio; use tokio::io::AsyncReadExt; use tokio::net::unix::{UnixListener, UnixStream}; async fn handle_client(mut stream: UnixStream) -> Result<(), Box<dyn std::error::Error>> { let mut buf = [0; 1024]; let n = stream.read(&mut buf).await?; let s = String::from_utf8_lossy(&buf[..n]); println!("{}", s); Ok(()) } #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { let sockfile = Path::new("/tmp/uds.sock"); if sockfile.exists() { fs::remove_file(&sockfile)?; } let listner = UnixListener::bind(sockfile)?; let mut incoming = listner.incoming(); while let Some(stream) = incoming.next().await { let stream = stream?; tokio::spawn(async move { handle_client(stream).await.unwrap(); }); } Ok(()) }