Refactor IPC and DTO structures; remove unused code and streamline message handling
- Removed unused structs and methods from `response.rs`, `usage.rs`, and `client.rs`. - Simplified `Connection` handling in `conn.rs` to only support Unix sockets. - Updated `IpcServer` to exclusively use Unix sockets and removed TCP handling. - Cleaned up `editlog.rs` by removing loading and recent entry methods. - Refactored `memory.rs` to eliminate unused functions related to lesson promotion and retrospective creation. - Enhanced `search.rs` to support multiple search providers and improved error handling. - Updated chat view logic to simplify message display and improve user experience. - Removed deprecated modules and constants from various files to streamline the codebase.
This commit is contained in:
@@ -6,11 +6,6 @@ pub struct IpcClient {
|
||||
}
|
||||
|
||||
impl IpcClient {
|
||||
pub fn connect_tcp(addr: &str) -> Result<Self> {
|
||||
let conn = Connection::connect_tcp(addr)?;
|
||||
Ok(IpcClient { conn })
|
||||
}
|
||||
|
||||
pub fn connect_unix(path: &str) -> Result<Self> {
|
||||
let conn = Connection::connect_unix(path)?;
|
||||
Ok(IpcClient { conn })
|
||||
@@ -23,15 +18,4 @@ impl IpcClient {
|
||||
pub fn receive<T: serde::de::DeserializeOwned>(&mut self) -> Result<Option<T>> {
|
||||
self.conn.receive()
|
||||
}
|
||||
|
||||
pub fn request<T: serde::Serialize, R: serde::de::DeserializeOwned>(
|
||||
&mut self,
|
||||
request: &T,
|
||||
) -> Result<R> {
|
||||
self.conn.send(request)?;
|
||||
match self.conn.receive::<R>()? {
|
||||
Some(response) => Ok(response),
|
||||
None => anyhow::bail!("connection closed before response"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+7
-30
@@ -1,38 +1,28 @@
|
||||
use std::net::TcpStream;
|
||||
use std::os::unix::net::UnixStream;
|
||||
use anyhow::Result;
|
||||
use super::frame;
|
||||
|
||||
pub enum Connection {
|
||||
Tcp(TcpStream),
|
||||
Unix(UnixStream),
|
||||
pub struct Connection {
|
||||
inner: UnixStream,
|
||||
}
|
||||
|
||||
impl Connection {
|
||||
pub fn connect_tcp(addr: &str) -> Result<Self> {
|
||||
let stream = TcpStream::connect(addr)?;
|
||||
stream.set_nodelay(true)?;
|
||||
Ok(Connection::Tcp(stream))
|
||||
pub fn from_stream(stream: UnixStream) -> Result<Self> {
|
||||
Ok(Connection { inner: stream })
|
||||
}
|
||||
|
||||
pub fn connect_unix(path: &str) -> Result<Self> {
|
||||
let stream = UnixStream::connect(path)?;
|
||||
Ok(Connection::Unix(stream))
|
||||
Ok(Connection { inner: stream })
|
||||
}
|
||||
|
||||
pub fn send<T: serde::Serialize>(&mut self, value: &T) -> Result<()> {
|
||||
let data = frame::serialize_frame(value)?;
|
||||
match self {
|
||||
Connection::Tcp(ref mut s) => frame::write_frame(s, &data),
|
||||
Connection::Unix(ref mut s) => frame::write_frame(s, &data),
|
||||
}
|
||||
frame::write_frame(&mut self.inner, &data)
|
||||
}
|
||||
|
||||
pub fn receive<T: serde::de::DeserializeOwned>(&mut self) -> Result<Option<T>> {
|
||||
let data = match self {
|
||||
Connection::Tcp(ref mut s) => frame::read_frame(s)?,
|
||||
Connection::Unix(ref mut s) => frame::read_frame(s)?,
|
||||
};
|
||||
let data = frame::read_frame(&mut self.inner)?;
|
||||
match data {
|
||||
Some(bytes) => {
|
||||
let value: T = frame::deserialize_frame(&bytes)?;
|
||||
@@ -41,17 +31,4 @@ impl Connection {
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_clone(&self) -> Result<Self> {
|
||||
match self {
|
||||
Connection::Tcp(s) => {
|
||||
let cloned = s.try_clone()?;
|
||||
Ok(Connection::Tcp(cloned))
|
||||
}
|
||||
Connection::Unix(s) => {
|
||||
let cloned = s.try_clone()?;
|
||||
Ok(Connection::Unix(cloned))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
pub mod client;
|
||||
pub mod conn;
|
||||
pub mod diff;
|
||||
pub mod frame;
|
||||
pub mod protocol;
|
||||
pub mod server;
|
||||
pub mod snapshot;
|
||||
|
||||
+4
-68
@@ -1,84 +1,20 @@
|
||||
use std::net::TcpListener;
|
||||
use std::os::unix::net::UnixListener;
|
||||
use std::thread;
|
||||
use anyhow::Result;
|
||||
use super::conn::Connection;
|
||||
|
||||
enum ListenerKind {
|
||||
Tcp(TcpListener),
|
||||
Unix(UnixListener),
|
||||
}
|
||||
|
||||
pub struct IpcServer {
|
||||
listener: ListenerKind,
|
||||
listener: UnixListener,
|
||||
}
|
||||
|
||||
impl IpcServer {
|
||||
pub fn bind(addr: &str) -> Result<Self> {
|
||||
let listener = TcpListener::bind(addr)?;
|
||||
Ok(IpcServer { listener: ListenerKind::Tcp(listener) })
|
||||
}
|
||||
|
||||
pub fn bind_unix(path: &str) -> Result<Self> {
|
||||
let _ = std::fs::remove_file(path);
|
||||
let listener = UnixListener::bind(path)?;
|
||||
Ok(IpcServer { listener: ListenerKind::Unix(listener) })
|
||||
Ok(IpcServer { listener })
|
||||
}
|
||||
|
||||
pub fn accept(&self) -> Result<Connection> {
|
||||
match &self.listener {
|
||||
ListenerKind::Tcp(l) => {
|
||||
let (stream, _addr) = l.accept()?;
|
||||
stream.set_nodelay(true)?;
|
||||
Ok(Connection::Tcp(stream))
|
||||
}
|
||||
ListenerKind::Unix(l) => {
|
||||
let (stream, _addr) = l.accept()?;
|
||||
Ok(Connection::Unix(stream))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn accept_with_handler<F>(self, handler: F) -> thread::JoinHandle<()>
|
||||
where
|
||||
F: Fn(Connection) -> Result<()> + Send + 'static,
|
||||
{
|
||||
match self.listener {
|
||||
ListenerKind::Tcp(l) => {
|
||||
thread::spawn(move || {
|
||||
for stream in l.incoming() {
|
||||
match stream {
|
||||
Ok(s) => {
|
||||
let _ = s.set_nodelay(true);
|
||||
if let Err(e) = handler(Connection::Tcp(s)) {
|
||||
eprintln!("ipc handler error: {}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("ipc accept error: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
ListenerKind::Unix(l) => {
|
||||
thread::spawn(move || {
|
||||
for stream in l.incoming() {
|
||||
match stream {
|
||||
Ok(s) => {
|
||||
if let Err(e) = handler(Connection::Unix(s)) {
|
||||
eprintln!("ipc handler error: {}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("ipc accept error: {}", e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
let (stream, _addr) = self.listener.accept()?;
|
||||
Connection::from_stream(stream)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user