Merge pull request #545 from ludovic111/perf/integrated-memory-connection
Integrated server: join through memory instead of a socket
This commit is contained in:
@@ -418,6 +418,7 @@ impl Game<'_> {
|
||||
enforces_secure_chat: false,
|
||||
accepts_transfers: props.accepts_transfers,
|
||||
announce: false,
|
||||
listen: true,
|
||||
bypasses_limit: Some(Arc::new(move |uuid| {
|
||||
bypass_lists
|
||||
.lock()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! The integrated server (`IntegratedServer`): single player's world runs on
|
||||
//! its own "Server thread", as vanilla's does, and the window joins it as a
|
||||
//! player over a local connection, so a world tick never holds up a frame.
|
||||
//! player through memory, so a world tick never holds up a frame.
|
||||
//!
|
||||
//! Opt-in for now (`--server-thread` or `HORIZON_SERVER_THREAD=1`): the
|
||||
//! player joins as a network player, and the server side of a network player
|
||||
@@ -20,9 +20,9 @@ use horizon_client::{
|
||||
settings::Settings,
|
||||
title::WorldOptions,
|
||||
};
|
||||
use horizon_protocol::connection::Pipe;
|
||||
use horizon_protocol::{crypto::offline_uuid, text::translatable};
|
||||
use std::{
|
||||
net::{IpAddr, Ipv4Addr},
|
||||
path::Path,
|
||||
sync::{
|
||||
Arc,
|
||||
@@ -89,7 +89,7 @@ pub(super) fn wanted(options: &Options, world: &Path) -> bool {
|
||||
|
||||
impl Game<'_> {
|
||||
/// `Minecraft.doWorldLoad`: start the server thread on the world, wait
|
||||
/// until it listens, then join it.
|
||||
/// until it can be joined, then join it through memory.
|
||||
pub(super) fn open_integrated(
|
||||
&mut self,
|
||||
folder: &str,
|
||||
@@ -149,12 +149,12 @@ impl Game<'_> {
|
||||
flags,
|
||||
thread: Some(thread),
|
||||
};
|
||||
let port = match ready.recv() {
|
||||
Ok(Ok(port)) => port,
|
||||
let pipe = match ready.recv() {
|
||||
Ok(Ok(pipe)) => pipe,
|
||||
Ok(Err(e)) => return Err(e.into()),
|
||||
Err(_) => return Err("the integrated server stopped".into()),
|
||||
};
|
||||
self.connect_server(&format!("127.0.0.1:{port}"), &player)?;
|
||||
self.connect_local(pipe, &player)?;
|
||||
self.integrated = Some(handle);
|
||||
Ok(())
|
||||
}
|
||||
@@ -165,7 +165,7 @@ impl Game<'_> {
|
||||
&mut self,
|
||||
launch: Launch,
|
||||
flags: &Flags,
|
||||
ready: &Sender<std::result::Result<u16, String>>,
|
||||
ready: &Sender<std::result::Result<Pipe, String>>,
|
||||
) -> Result<()> {
|
||||
self.open_world(
|
||||
&launch.folder,
|
||||
@@ -191,8 +191,9 @@ impl Game<'_> {
|
||||
};
|
||||
let owner = launch.owner;
|
||||
let net = NetConfig {
|
||||
bind: Some(IpAddr::V4(Ipv4Addr::LOCALHOST)),
|
||||
// A local connection is not compressed (`Connection.isMemoryConnection`).
|
||||
// No network until it is opened to the LAN.
|
||||
listen: false,
|
||||
// A memory connection is not compressed (`Connection.isMemoryConnection`).
|
||||
compression_threshold: -1,
|
||||
enable_status: false,
|
||||
announce: false,
|
||||
@@ -208,7 +209,7 @@ impl Game<'_> {
|
||||
};
|
||||
self.lan_host(settings, net, config, None)?;
|
||||
let commands = self.allow_commands();
|
||||
let port = {
|
||||
let pipe = {
|
||||
let host = self
|
||||
.lan
|
||||
.as_mut()
|
||||
@@ -220,9 +221,9 @@ impl Game<'_> {
|
||||
uuid: owner,
|
||||
saved: Some(saved),
|
||||
});
|
||||
host.settings.port
|
||||
host.hub.lan.connect_local()?
|
||||
};
|
||||
let _ = ready.send(Ok(port));
|
||||
let _ = ready.send(Ok(pipe));
|
||||
self.settle_spawn()?;
|
||||
let mut next = Instant::now();
|
||||
while !flags.stop.load(Ordering::Relaxed) {
|
||||
@@ -279,7 +280,7 @@ fn serve(
|
||||
settings: Settings,
|
||||
launch: Launch,
|
||||
flags: &Flags,
|
||||
ready: &Sender<std::result::Result<u16, String>>,
|
||||
ready: &Sender<std::result::Result<Pipe, String>>,
|
||||
) -> Result<()> {
|
||||
let atlas = HudAtlas::build(&options.resources)?;
|
||||
let audio = Audio::silent(&options.resources);
|
||||
|
||||
@@ -153,6 +153,22 @@ fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFo
|
||||
impl Game<'_> {
|
||||
/// Leave the title for a server (ConnectScreen): an empty world fed by the connection.
|
||||
pub(super) fn connect_server(&mut self, address: &str, name: &str) -> Result<()> {
|
||||
self.join_server(address, name, None)
|
||||
}
|
||||
/// Join single player's integrated server through memory.
|
||||
pub(super) fn connect_local(
|
||||
&mut self,
|
||||
pipe: horizon_protocol::connection::Pipe,
|
||||
name: &str,
|
||||
) -> Result<()> {
|
||||
self.join_server("local", name, Some(pipe))
|
||||
}
|
||||
fn join_server(
|
||||
&mut self,
|
||||
address: &str,
|
||||
name: &str,
|
||||
local: Option<horizon_protocol::connection::Pipe>,
|
||||
) -> Result<()> {
|
||||
self.perf.world_opening();
|
||||
let root = self.options.resources.clone();
|
||||
let builtin = Builtin::find(&root)
|
||||
@@ -160,12 +176,22 @@ impl Game<'_> {
|
||||
.ok_or("registries.json not found next to the resources; run the data generator reports (horizon-prepare)")?;
|
||||
let lang = Arc::new(horizon_client::net::load_language(&root));
|
||||
let view = self.settings.render_distance.clamp(2, 32) as i8;
|
||||
horizon_client::crash::set_detail("Server", horizon_client::net::shown_address(address));
|
||||
if local.is_none() {
|
||||
horizon_client::crash::set_detail(
|
||||
"Server",
|
||||
horizon_client::net::shown_address(address),
|
||||
);
|
||||
}
|
||||
// A signed-in account always plays under its own name.
|
||||
let auth = horizon_client::auth::current();
|
||||
let name = auth.as_ref().map_or(name, |a| a.name.as_str());
|
||||
let session = Session::connect(address, name, auth.clone(), view, lang.clone());
|
||||
self.extras_connecting(address, name);
|
||||
let session = match local {
|
||||
Some(pipe) => Session::connect_local(pipe, name, view, lang.clone()),
|
||||
None => {
|
||||
self.extras_connecting(address, name);
|
||||
Session::connect(address, name, auth.clone(), view, lang.clone())
|
||||
}
|
||||
};
|
||||
let player_type = super::perspective::player_type(&root);
|
||||
let mut streaming = Streaming::new(
|
||||
Arc::new(NoTerrain),
|
||||
|
||||
@@ -217,7 +217,25 @@ pub fn serve(
|
||||
{
|
||||
return serve_legacy(stream, &shared);
|
||||
}
|
||||
let mut c = Connection::from_stream(stream).map_err(e)?;
|
||||
serve_connection(Connection::from_stream(stream).map_err(e)?, address, shared)
|
||||
}
|
||||
|
||||
/// `ServerConnectionListener.startMemoryChannel`: an integrated server's
|
||||
/// own player, over one end of an in-memory pipe.
|
||||
pub fn serve_local(
|
||||
pipe: horizon_protocol::connection::Pipe,
|
||||
shared: Arc<Shared>,
|
||||
) -> Result<(), String> {
|
||||
let address = std::net::SocketAddr::from(([127, 0, 0, 1], 0));
|
||||
serve_connection(Connection::local(pipe), address, shared)
|
||||
}
|
||||
|
||||
fn serve_connection(
|
||||
mut c: Connection,
|
||||
address: std::net::SocketAddr,
|
||||
shared: Arc<Shared>,
|
||||
) -> Result<(), String> {
|
||||
let e = |e: horizon_protocol::Error| e.to_string();
|
||||
c.reader
|
||||
.set_read_timeout(Some(Duration::from_secs(30)))
|
||||
.map_err(e)?;
|
||||
|
||||
@@ -101,6 +101,9 @@ pub struct NetConfig {
|
||||
pub accepts_transfers: bool,
|
||||
/// LanServerPinger: announce the world on the local network.
|
||||
pub announce: bool,
|
||||
/// Accept connections from the network. Without it only local ones
|
||||
/// (`Lan::connect_local`): an integrated server not open to the LAN.
|
||||
pub listen: bool,
|
||||
/// Ops bypass a full server (ServerOpList.canBypassPlayerLimit).
|
||||
pub bypasses_limit: Option<Arc<dyn Fn(u128) -> bool + Send + Sync>>,
|
||||
pub gate: Option<LoginGate>,
|
||||
@@ -120,6 +123,7 @@ impl NetConfig {
|
||||
enforces_secure_chat: false,
|
||||
accepts_transfers: false,
|
||||
announce: true,
|
||||
listen: true,
|
||||
bypasses_limit: None,
|
||||
gate: None,
|
||||
}
|
||||
@@ -321,13 +325,21 @@ impl Lan {
|
||||
}
|
||||
/// ServerConnectionListener.startTcpServerListener with a configuration.
|
||||
pub fn start_with(sync: Arc<data::SyncData>, config: NetConfig) -> io::Result<Self> {
|
||||
let bind = config
|
||||
.bind
|
||||
.unwrap_or(std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED));
|
||||
let listener = TcpListener::bind((bind, config.port))?;
|
||||
listener.set_nonblocking(true)?;
|
||||
let port = listener.local_addr()?.port();
|
||||
let _pinger = if config.announce {
|
||||
let listener = if config.listen {
|
||||
let bind = config
|
||||
.bind
|
||||
.unwrap_or(std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED));
|
||||
let listener = TcpListener::bind((bind, config.port))?;
|
||||
listener.set_nonblocking(true)?;
|
||||
Some(listener)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let port = match &listener {
|
||||
Some(l) => l.local_addr()?.port(),
|
||||
None => 0,
|
||||
};
|
||||
let _pinger = if config.announce && listener.is_some() {
|
||||
discovery::Pinger::start(config.motd.clone(), port).ok()
|
||||
} else {
|
||||
None
|
||||
@@ -346,17 +358,37 @@ impl Lan {
|
||||
config,
|
||||
});
|
||||
let accept_shared = shared.clone();
|
||||
let listener = std::thread::Builder::new()
|
||||
.name("horizon-lan-accept".into())
|
||||
.spawn(move || accept_loop(listener, accept_shared))?;
|
||||
let listener = listener
|
||||
.map(|listener| {
|
||||
std::thread::Builder::new()
|
||||
.name("horizon-lan-accept".into())
|
||||
.spawn(move || accept_loop(listener, accept_shared))
|
||||
})
|
||||
.transpose()?;
|
||||
Ok(Self {
|
||||
shared,
|
||||
events,
|
||||
port,
|
||||
listener: Some(listener),
|
||||
listener,
|
||||
_pinger,
|
||||
})
|
||||
}
|
||||
/// `Connection.connectToLocalServer`: a connection through memory, its
|
||||
/// server end served on its own thread like an accepted socket.
|
||||
pub fn connect_local(&self) -> io::Result<horizon_protocol::connection::Pipe> {
|
||||
let (client, server) = horizon_protocol::connection::Pipe::pair();
|
||||
let shared = self.shared.clone();
|
||||
std::thread::Builder::new()
|
||||
.name("horizon-local-conn".into())
|
||||
.spawn(move || {
|
||||
if let Err(e) = conn::serve_local(server, shared)
|
||||
&& std::env::var_os("HORIZON_NET_DEBUG").is_some()
|
||||
{
|
||||
log::warn!("[lan] local: {e}");
|
||||
}
|
||||
})?;
|
||||
Ok(client)
|
||||
}
|
||||
/// Everything the connection threads reported since the last call.
|
||||
pub fn poll(&self) -> Vec<NetEvent> {
|
||||
self.events.try_iter().collect()
|
||||
|
||||
@@ -67,6 +67,25 @@ impl Session {
|
||||
auth: Option<Arc<AuthSession>>,
|
||||
view_distance: i8,
|
||||
lang: Arc<Language>,
|
||||
) -> Self {
|
||||
Self::start(address, name, auth, view_distance, lang, None)
|
||||
}
|
||||
/// Join an integrated server through memory (`Connection.connectToLocalServer`).
|
||||
pub fn connect_local(
|
||||
pipe: horizon_protocol::connection::Pipe,
|
||||
name: &str,
|
||||
view_distance: i8,
|
||||
lang: Arc<Language>,
|
||||
) -> Self {
|
||||
Self::start("local", name, None, view_distance, lang, Some(pipe))
|
||||
}
|
||||
fn start(
|
||||
address: &str,
|
||||
name: &str,
|
||||
auth: Option<Arc<AuthSession>>,
|
||||
view_distance: i8,
|
||||
lang: Arc<Language>,
|
||||
local: Option<horizon_protocol::connection::Pipe>,
|
||||
) -> Self {
|
||||
let (send, events) = channel();
|
||||
let writer = Arc::new(Mutex::new(None));
|
||||
@@ -77,6 +96,7 @@ impl Session {
|
||||
.name("horizon-net".into())
|
||||
.spawn(move || {
|
||||
let result = run(
|
||||
local,
|
||||
&address_owned,
|
||||
&name_owned,
|
||||
auth.as_deref(),
|
||||
@@ -222,6 +242,7 @@ fn tr(lang: &Language, key: &str) -> String {
|
||||
/// The whole connection. Ok: why it ended for good (the server's reason, or
|
||||
/// a refusal no retry fixes); Err: the connection failed.
|
||||
fn run(
|
||||
local: Option<horizon_protocol::connection::Pipe>,
|
||||
address: &str,
|
||||
name: &str,
|
||||
auth: Option<&AuthSession>,
|
||||
@@ -231,18 +252,23 @@ fn run(
|
||||
shared_writer: &Mutex<Option<ConnWriter>>,
|
||||
shared_counters: &Mutex<Option<Arc<Counters>>>,
|
||||
) -> Result<String, String> {
|
||||
let (host, port) = match address.strip_prefix(FRIEND) {
|
||||
Some(code) => join_friend(code, send)?,
|
||||
None => {
|
||||
let Some(target) = parse_address(address) else {
|
||||
return Ok(format!("Invalid server address: {address}"));
|
||||
};
|
||||
status(send, "Connecting to the server...");
|
||||
target
|
||||
}
|
||||
let (host, port, mut c) = if let Some(pipe) = local {
|
||||
(address.to_owned(), 0, Connection::local(pipe))
|
||||
} else {
|
||||
let (host, port) = match address.strip_prefix(FRIEND) {
|
||||
Some(code) => join_friend(code, send)?,
|
||||
None => {
|
||||
let Some(target) = parse_address(address) else {
|
||||
return Ok(format!("Invalid server address: {address}"));
|
||||
};
|
||||
status(send, "Connecting to the server...");
|
||||
target
|
||||
}
|
||||
};
|
||||
let c =
|
||||
Connection::connect(&host, port, Duration::from_secs(10)).map_err(|e| e.to_string())?;
|
||||
(host, port, c)
|
||||
};
|
||||
let mut c =
|
||||
Connection::connect(&host, port, Duration::from_secs(10)).map_err(|e| e.to_string())?;
|
||||
*shared_counters.lock().unwrap() = Some(c.reader.counters.clone());
|
||||
let e = |e: horizon_protocol::Error| e.to_string();
|
||||
c.send(
|
||||
|
||||
@@ -1,17 +1,163 @@
|
||||
//! A blocking TCP connection with vanilla's pipeline: optional AES/CFB8
|
||||
//! A blocking connection with vanilla's pipeline: optional AES/CFB8
|
||||
//! encryption, VarInt21 framing and threshold compression. After login it
|
||||
//! splits into a reader (for a receive thread) and a shareable writer.
|
||||
//! splits into a reader (for a receive thread) and a shareable writer. It runs
|
||||
//! over TCP, or over an in-memory pipe between two threads of one process
|
||||
//! (vanilla's `LocalChannel`, which an integrated server's player uses).
|
||||
use crate::{Error, Result, buf::Reader, crypto::Cfb8, frame};
|
||||
use std::{
|
||||
io::{Read, Write},
|
||||
net::{Shutdown, TcpStream, ToSocketAddrs},
|
||||
sync::{
|
||||
Arc,
|
||||
Arc, Condvar, Mutex,
|
||||
atomic::{AtomicI64, Ordering},
|
||||
},
|
||||
time::Duration,
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
/// One direction of a [`Pipe`]: the bytes written and not read yet, and
|
||||
/// whether either end closed it.
|
||||
#[derive(Default)]
|
||||
struct Channel {
|
||||
state: Mutex<(std::collections::VecDeque<u8>, bool)>,
|
||||
ready: Condvar,
|
||||
}
|
||||
impl Channel {
|
||||
fn close(&self) {
|
||||
self.state.lock().unwrap().1 = true;
|
||||
self.ready.notify_all();
|
||||
}
|
||||
}
|
||||
/// The channels of one end; dropping the end's last clone closes them, as
|
||||
/// a socket closes with its process.
|
||||
struct End {
|
||||
incoming: Arc<Channel>,
|
||||
outgoing: Arc<Channel>,
|
||||
}
|
||||
impl Drop for End {
|
||||
fn drop(&mut self) {
|
||||
self.incoming.close();
|
||||
self.outgoing.close();
|
||||
}
|
||||
}
|
||||
/// One end of an in-memory byte pipe: what one end writes the other reads.
|
||||
pub struct Pipe {
|
||||
end: Arc<End>,
|
||||
/// The read timeout (`set_read_timeout`).
|
||||
timeout: Mutex<Option<Duration>>,
|
||||
}
|
||||
impl Pipe {
|
||||
/// Two connected ends.
|
||||
pub fn pair() -> (Self, Self) {
|
||||
let (a, b) = (Arc::new(Channel::default()), Arc::new(Channel::default()));
|
||||
let end = |incoming, outgoing| Self {
|
||||
end: Arc::new(End { incoming, outgoing }),
|
||||
timeout: Mutex::new(None),
|
||||
};
|
||||
(end(a.clone(), b.clone()), end(b, a))
|
||||
}
|
||||
fn try_clone(&self) -> Self {
|
||||
Self {
|
||||
end: self.end.clone(),
|
||||
timeout: Mutex::new(*self.timeout.lock().unwrap()),
|
||||
}
|
||||
}
|
||||
/// Close both directions: reads at either end then see the end of the stream.
|
||||
pub fn shutdown(&self) {
|
||||
self.end.incoming.close();
|
||||
self.end.outgoing.close();
|
||||
}
|
||||
}
|
||||
impl Read for Pipe {
|
||||
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
|
||||
let deadline = self.timeout.lock().unwrap().map(|t| Instant::now() + t);
|
||||
let mut state = self.end.incoming.state.lock().unwrap();
|
||||
while state.0.is_empty() && !state.1 {
|
||||
state = match deadline {
|
||||
None => self.end.incoming.ready.wait(state).unwrap(),
|
||||
Some(deadline) => {
|
||||
let left = deadline.saturating_duration_since(Instant::now());
|
||||
if left.is_zero() {
|
||||
return Err(std::io::ErrorKind::TimedOut.into());
|
||||
}
|
||||
self.end.incoming.ready.wait_timeout(state, left).unwrap().0
|
||||
}
|
||||
};
|
||||
}
|
||||
let n = out.len().min(state.0.len());
|
||||
for (o, b) in out.iter_mut().zip(state.0.drain(..n)) {
|
||||
*o = b;
|
||||
}
|
||||
Ok(n)
|
||||
}
|
||||
}
|
||||
impl Write for Pipe {
|
||||
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
|
||||
let mut state = self.end.outgoing.state.lock().unwrap();
|
||||
if state.1 {
|
||||
return Err(std::io::ErrorKind::BrokenPipe.into());
|
||||
}
|
||||
state.0.extend(data);
|
||||
self.end.outgoing.ready.notify_all();
|
||||
Ok(data.len())
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// What a connection runs over.
|
||||
enum Stream {
|
||||
Tcp(TcpStream),
|
||||
Memory(Pipe),
|
||||
}
|
||||
impl Stream {
|
||||
fn try_clone(&self) -> std::io::Result<Self> {
|
||||
Ok(match self {
|
||||
Self::Tcp(s) => Self::Tcp(s.try_clone()?),
|
||||
Self::Memory(p) => Self::Memory(p.try_clone()),
|
||||
})
|
||||
}
|
||||
fn set_read_timeout(&self, timeout: Option<Duration>) -> std::io::Result<()> {
|
||||
match self {
|
||||
Self::Tcp(s) => s.set_read_timeout(timeout),
|
||||
Self::Memory(p) => {
|
||||
*p.timeout.lock().unwrap() = timeout;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
fn shutdown(&self) {
|
||||
match self {
|
||||
Self::Tcp(s) => {
|
||||
let _ = s.shutdown(Shutdown::Both);
|
||||
}
|
||||
Self::Memory(p) => p.shutdown(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Read for Stream {
|
||||
fn read(&mut self, out: &mut [u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Self::Tcp(s) => s.read(out),
|
||||
Self::Memory(p) => p.read(out),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Write for Stream {
|
||||
fn write(&mut self, data: &[u8]) -> std::io::Result<usize> {
|
||||
match self {
|
||||
Self::Tcp(s) => s.write(data),
|
||||
Self::Memory(p) => p.write(data),
|
||||
}
|
||||
}
|
||||
fn flush(&mut self) -> std::io::Result<()> {
|
||||
match self {
|
||||
Self::Tcp(s) => s.flush(),
|
||||
Self::Memory(p) => p.flush(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Packets received/sent and bytes in/out, for the debug screen.
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Counters {
|
||||
@@ -22,14 +168,14 @@ pub struct Counters {
|
||||
}
|
||||
|
||||
pub struct ConnReader {
|
||||
stream: TcpStream,
|
||||
stream: Stream,
|
||||
buffer: Vec<u8>,
|
||||
decrypt: Option<Cfb8>,
|
||||
pub threshold: Option<usize>,
|
||||
pub counters: Arc<Counters>,
|
||||
}
|
||||
pub struct ConnWriter {
|
||||
stream: TcpStream,
|
||||
stream: Stream,
|
||||
encrypt: Option<Cfb8>,
|
||||
pub threshold: Option<usize>,
|
||||
counters: Arc<Counters>,
|
||||
@@ -56,6 +202,13 @@ impl Connection {
|
||||
}
|
||||
pub fn from_stream(stream: TcpStream) -> Result<Self> {
|
||||
stream.set_nodelay(true)?;
|
||||
Self::over(Stream::Tcp(stream))
|
||||
}
|
||||
/// A connection over one end of an in-memory [`Pipe`].
|
||||
pub fn local(pipe: Pipe) -> Self {
|
||||
Self::over(Stream::Memory(pipe)).expect("a pipe end clones")
|
||||
}
|
||||
fn over(stream: Stream) -> Result<Self> {
|
||||
let counters = Arc::new(Counters::default());
|
||||
let write = stream.try_clone()?;
|
||||
Ok(Self {
|
||||
@@ -144,7 +297,7 @@ impl ConnWriter {
|
||||
Ok(())
|
||||
}
|
||||
pub fn shutdown(&self) {
|
||||
let _ = self.stream.shutdown(Shutdown::Both);
|
||||
self.stream.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +337,47 @@ mod tests {
|
||||
server.join().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_pipe_carries_packets_and_closes() {
|
||||
let (a, b) = Pipe::pair();
|
||||
let server = std::thread::spawn(move || {
|
||||
let mut c = Connection::local(b);
|
||||
c.set_compression(64);
|
||||
let (id, payload) = c.recv().unwrap();
|
||||
assert_eq!((id, payload.len()), (5, 1000));
|
||||
c.send(&[9, 1, 2]).unwrap();
|
||||
let (_, writer) = c.split();
|
||||
writer.shutdown();
|
||||
});
|
||||
let mut c = Connection::local(a);
|
||||
c.set_compression(64);
|
||||
let mut big = vec![5];
|
||||
big.extend(std::iter::repeat_n(3u8, 1000));
|
||||
c.send(&big).unwrap();
|
||||
assert_eq!(c.recv().unwrap(), (9, vec![1, 2]));
|
||||
server.join().unwrap();
|
||||
assert!(c.recv().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropping_a_pipe_end_closes_it() {
|
||||
let (a, b) = Pipe::pair();
|
||||
let mut c = Connection::local(a);
|
||||
drop(b);
|
||||
assert!(c.recv().is_err());
|
||||
assert!(c.send(&[1]).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn local_pipe_read_times_out() {
|
||||
let (a, _b) = Pipe::pair();
|
||||
let mut c = Connection::local(a);
|
||||
c.reader
|
||||
.set_read_timeout(Some(Duration::from_millis(20)))
|
||||
.unwrap();
|
||||
assert!(c.recv().is_err());
|
||||
}
|
||||
|
||||
/// Encrypted bytes that arrive in the same read as the last plain packet
|
||||
/// are decrypted once encryption is enabled.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user