Skip to content

Commit b71476b

Browse files
claudecdecker
authored andcommitted
signerproxy: Convert wire.rs to synchronous I/O (Phase 1)
Replace tokio async I/O with std synchronous I/O in wire.rs: - Remove tokio::net::UnixStream, use std::os::unix::net::UnixStream - Remove tokio::sync::Mutex, use std::sync::Mutex - Convert async read/write methods to blocking - Replace AsyncReadExt/AsyncWriteExt with std::io Read/Write - Manually handle 4-byte length prefix with BigEndian read/write This is Phase 1 of removing old tokio 0.2 dependency and converting the gl-signerproxy subdaemon to use standard library threading instead of async/await. The passfd FD passing implementation works naturally with blocking sockets.
1 parent 028835b commit b71476b

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

libs/gl-signerproxy/src/wire.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ use crate::passfd::SyncFdPassingExt;
22
use anyhow::{anyhow, Error, Result};
33
use byteorder::{BigEndian, ByteOrder};
44
use log::trace;
5+
use std::io::{Read, Write};
56
use std::os::unix::io::{AsRawFd, RawFd};
6-
use tokio::io::{AsyncReadExt, AsyncWriteExt};
7-
use tokio::net::UnixStream;
8-
use tokio::sync::Mutex;
7+
use std::os::unix::net::UnixStream;
8+
use std::sync::Mutex;
99

1010
/// A simple implementation of the inter-daemon protocol wrapping a
1111
/// UnixStream. Easy to read from and write to.
@@ -65,18 +65,26 @@ impl DaemonConnection {
6565
}
6666
}
6767

68-
pub async fn read(&self) -> Result<Message, Error> {
69-
let mut sock = self.conn.lock().await;
70-
let msglen = sock.read_u32().await?;
71-
let mut buf = vec![0 as u8; msglen as usize];
72-
sock.read_exact(&mut buf).await?;
68+
pub fn read(&self) -> Result<Message, Error> {
69+
let mut sock = self.conn.lock().unwrap();
70+
71+
// Read 4-byte length prefix in big-endian
72+
let mut len_buf = [0u8; 4];
73+
sock.read_exact(&mut len_buf)?;
74+
let msglen = BigEndian::read_u32(&len_buf);
75+
76+
// Read the message body
77+
let mut buf = vec![0u8; msglen as usize];
78+
sock.read_exact(&mut buf)?;
79+
7380
if buf.len() < msglen as usize {
7481
return Err(anyhow!("Short read from client"));
7582
}
7683

7784
let typ = BigEndian::read_u16(&buf);
7885
let mut fds = vec![];
7986

87+
// Receive any file descriptors associated with this message type
8088
let numfds = DaemonConnection::count_fds(typ);
8189
for _ in 0..numfds {
8290
fds.push(sock.as_raw_fd().recv_fd()?);
@@ -89,17 +97,24 @@ impl DaemonConnection {
8997
}
9098
}
9199

92-
pub async fn write(&self, msg: Message) -> Result<(), Error> {
100+
pub fn write(&self, msg: Message) -> Result<(), Error> {
93101
trace!(
94102
"Sending message {} ({} bytes, {} FDs)",
95103
msg.typ,
96104
msg.body.len(),
97105
msg.fds.len()
98106
);
99-
let mut client = self.conn.lock().await;
100-
client.write_u32(msg.body.len() as u32).await?;
101-
client.write_all(&msg.body).await?;
107+
let mut client = self.conn.lock().unwrap();
108+
109+
// Write 4-byte length prefix in big-endian
110+
let mut len_buf = [0u8; 4];
111+
BigEndian::write_u32(&mut len_buf, msg.body.len() as u32);
112+
client.write_all(&len_buf)?;
113+
114+
// Write the message body
115+
client.write_all(&msg.body)?;
102116

117+
// Send any file descriptors
103118
for fd in msg.fds {
104119
client.as_raw_fd().send_fd(fd)?;
105120
}

0 commit comments

Comments
 (0)