Skip to content

Commit 069965b

Browse files
committed
Merge branch 'develop' of github.com:/BlockProject3D/tools.net into develop
2 parents ade58ff + a980327 commit 069965b

File tree

5 files changed

+106
-55
lines changed

5 files changed

+106
-55
lines changed

core/src/ipc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
3131
#[cfg(unix)]
3232
mod unix;
33+
pub mod util;
3334
#[cfg(windows)]
3435
mod windows;
35-
pub mod util;
3636

3737
#[cfg(unix)]
3838
type ServerImpl = unix::Server;

core/src/ipc/unix.rs

Lines changed: 47 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2727
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2828

29+
use crate::ipc::util::Message;
2930
use std::path::{Path, PathBuf};
30-
use std::sync::Arc;
3131
use std::sync::atomic::{AtomicUsize, Ordering};
32+
use std::sync::Arc;
3233
use tempfile::TempDir;
3334
use tokio::net::UnixDatagram;
3435
use tokio::task::JoinHandle;
35-
use crate::ipc::util::Message;
3636

3737
const INIT_CLIENT_CONNECT: &[u8] = &[0xAB];
3838
const INIT_SERVER_CONNECT: &[u8] = &[0xCD];
@@ -46,7 +46,7 @@ pub struct Server {
4646
socket: UnixDatagram,
4747
dir: TempDir,
4848
num_clients: AtomicUsize,
49-
path: PathBuf
49+
path: PathBuf,
5050
}
5151

5252
impl Server {
@@ -55,14 +55,21 @@ impl Server {
5555
let _ = tokio::fs::remove_file(&path).await;
5656
let socket = UnixDatagram::bind(&path)?;
5757
let dir = tempfile::tempdir()?;
58-
Ok(Self { socket, dir, num_clients: AtomicUsize::new(0), path })
58+
Ok(Self {
59+
socket,
60+
dir,
61+
num_clients: AtomicUsize::new(0),
62+
path,
63+
})
5964
}
6065

6166
pub async fn accept(&self) -> std::io::Result<Client> {
6267
let mut buf = [0; 1];
6368
let (len, tx) = self.socket.recv_from(&mut buf).await?;
64-
let tx = tx.as_pathname()
65-
.ok_or(std::io::Error::other("unable to establish tx link"))?.into();
69+
let tx = tx
70+
.as_pathname()
71+
.ok_or(std::io::Error::other("unable to establish tx link"))?
72+
.into();
6673
if len != 1 || buf != INIT_CLIENT_CONNECT {
6774
self.socket.send_to(END, &tx).await?;
6875
return Err(std::io::Error::other("rejected invalid client"));
@@ -89,13 +96,13 @@ pub struct ClientInner {
8996
// This is needed because this must not be dropped until ClientInner itself is dropped.
9097
#[allow(unused)]
9198
dir: Option<TempDir>,
92-
failures: AtomicUsize
99+
failures: AtomicUsize,
93100
}
94101

95102
#[derive(Debug)]
96103
pub struct Client {
97104
inner: Arc<ClientInner>,
98-
handle: JoinHandle<()>
105+
handle: JoinHandle<()>,
99106
}
100107

101108
impl Client {
@@ -110,8 +117,10 @@ impl Client {
110117
if size != 1 || buf != INIT_SERVER_CONNECT {
111118
return Err(std::io::Error::other("server rejected our connection"));
112119
}
113-
let tx = tx.as_pathname()
114-
.ok_or(std::io::Error::other("unable to establish tx link"))?.into();
120+
let tx = tx
121+
.as_pathname()
122+
.ok_or(std::io::Error::other("unable to establish tx link"))?
123+
.into();
115124
Ok(Self::new(rx, tx, Some(dir)))
116125
}
117126

@@ -120,7 +129,7 @@ impl Client {
120129
rx,
121130
tx,
122131
dir,
123-
failures: AtomicUsize::new(0)
132+
failures: AtomicUsize::new(0),
124133
});
125134
let fuck = inner.clone();
126135
let handle = tokio::spawn(async move {
@@ -134,15 +143,15 @@ impl Client {
134143
}
135144
}
136145
});
137-
Self {
138-
inner,
139-
handle
140-
}
146+
Self { inner, handle }
141147
}
142148

143149
fn check_dead(&self) -> std::io::Result<()> {
144150
if self.inner.failures.load(Ordering::SeqCst) >= MAX_FAILURES {
145-
return Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "lost rx link"));
151+
return Err(std::io::Error::new(
152+
std::io::ErrorKind::BrokenPipe,
153+
"lost rx link",
154+
));
146155
}
147156
Ok(())
148157
}
@@ -153,10 +162,17 @@ impl Client {
153162

154163
pub async fn send(&self, msg: &Message) -> std::io::Result<usize> {
155164
self.check_dead()?;
156-
let len = self.inner.rx.send_to(&msg.buffer[..msg.len + 1], &self.inner.tx).await?;
165+
let len = self
166+
.inner
167+
.rx
168+
.send_to(&msg.buffer[..msg.len + 1], &self.inner.tx)
169+
.await?;
157170
if len == 0 {
158171
self.set_dead();
159-
return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "unexpected EOF"));
172+
return Err(std::io::Error::new(
173+
std::io::ErrorKind::UnexpectedEof,
174+
"unexpected EOF",
175+
));
160176
}
161177
Ok(len - 1)
162178
}
@@ -165,14 +181,20 @@ impl Client {
165181
self.check_dead()?;
166182
msg.set_size(msg.max_size() + 1);
167183
loop {
168-
let res = tokio::time::timeout(std::time::Duration::from_secs(2), async { self.inner.rx.recv(msg).await }).await;
184+
let res = tokio::time::timeout(std::time::Duration::from_secs(2), async {
185+
self.inner.rx.recv(msg).await
186+
})
187+
.await;
169188
match res {
170189
Err(_) => {
171190
let failures = self.inner.failures.fetch_add(1, Ordering::SeqCst);
172191
if failures >= MAX_FAILURES {
173-
return Err(std::io::Error::new(std::io::ErrorKind::BrokenPipe, "lost rx link"));
192+
return Err(std::io::Error::new(
193+
std::io::ErrorKind::BrokenPipe,
194+
"lost rx link",
195+
));
174196
}
175-
},
197+
}
176198
Ok(res) => {
177199
self.inner.failures.store(0, Ordering::SeqCst);
178200
let len = res?;
@@ -186,7 +208,10 @@ impl Client {
186208
return Ok(());
187209
} else if len == 0 {
188210
self.set_dead();
189-
return Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "unexpected EOF"));
211+
return Err(std::io::Error::new(
212+
std::io::ErrorKind::UnexpectedEof,
213+
"unexpected EOF",
214+
));
190215
}
191216
}
192217
}

core/src/ipc/util.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use std::ops::{Deref, DerefMut};
3434
/// Represents an IPC message.
3535
pub struct Message {
3636
pub(super) buffer: Vec<u8>,
37-
pub(super) len: usize
37+
pub(super) len: usize,
3838
}
3939

4040
impl Message {
@@ -48,7 +48,7 @@ impl Message {
4848
pub fn new(max_size: usize) -> Self {
4949
Self {
5050
buffer: vec![0; max_size + 1],
51-
len: max_size
51+
len: max_size,
5252
}
5353
}
5454

0 commit comments

Comments
 (0)