Cleanup socket cleanup code
This commit is contained in:
parent
0768a7dc61
commit
c8a6aa6fbd
|
@ -17,8 +17,6 @@ use slab::Slab;
|
||||||
use crate::protocol::*;
|
use crate::protocol::*;
|
||||||
use crate::service::window::{WindowMessage, WindowMessageSender};
|
use crate::service::window::{WindowMessage, WindowMessageSender};
|
||||||
|
|
||||||
use std::io::{Error, ErrorKind};
|
|
||||||
|
|
||||||
const SOCK_NAME: &str = "magpie.sock";
|
const SOCK_NAME: &str = "magpie.sock";
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -69,11 +67,12 @@ impl DerefMut for Listener {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Listener {
|
impl Listener {
|
||||||
fn new() -> std::io::Result<Self>{
|
fn new() -> std::io::Result<Self> {
|
||||||
let sock_dir = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR not set");
|
let sock_dir = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR not set");
|
||||||
let sock_dir = Path::new(&sock_dir);
|
let sock_dir = Path::new(&sock_dir);
|
||||||
let sock_path = sock_dir.join(SOCK_NAME);
|
let sock_path = sock_dir.join(SOCK_NAME);
|
||||||
|
|
||||||
|
use std::io::{Error, ErrorKind};
|
||||||
match UnixStream::connect(&sock_path) {
|
match UnixStream::connect(&sock_path) {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
eprintln!("Socket is already in use. Another instance of Magpie may be running.");
|
eprintln!("Socket is already in use. Another instance of Magpie may be running.");
|
||||||
|
@ -82,23 +81,20 @@ impl Listener {
|
||||||
return Err(error);
|
return Err(error);
|
||||||
}
|
}
|
||||||
Err(ref err) if err.kind() == ErrorKind::ConnectionRefused => {
|
Err(ref err) if err.kind() == ErrorKind::ConnectionRefused => {
|
||||||
eprintln!("Found leftover socket; removing");
|
eprintln!("Found leftover socket; removing.");
|
||||||
std::fs::remove_file(&sock_path)?;
|
std::fs::remove_file(&sock_path)?;
|
||||||
}
|
}
|
||||||
Err(ref err) if err.kind() == ErrorKind::NotFound => {}
|
Err(ref err) if err.kind() == ErrorKind::NotFound => {}
|
||||||
Err(err) => return Err(err)
|
Err(err) => return Err(err),
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
eprintln!("Making socket at: {:?}", sock_path);
|
eprintln!("Making socket at: {:?}", sock_path);
|
||||||
let uds = UnixListener::bind(&sock_path)?;
|
let uds = UnixListener::bind(&sock_path)?;
|
||||||
let path = sock_path.to_path_buf();
|
let path = sock_path.to_path_buf();
|
||||||
Ok(Self{uds, path})
|
Ok(Self { uds, path })
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub struct IpcData {
|
pub struct IpcData {
|
||||||
poll: Poll,
|
poll: Poll,
|
||||||
window_to_client_panel: HashMap<usize, (usize, PanelId)>,
|
window_to_client_panel: HashMap<usize, (usize, PanelId)>,
|
||||||
|
@ -195,7 +191,6 @@ pub struct Ipc {
|
||||||
|
|
||||||
impl Ipc {
|
impl Ipc {
|
||||||
pub fn new(window_sender: WindowMessageSender) -> std::io::Result<(Self, IpcMessageSender)> {
|
pub fn new(window_sender: WindowMessageSender) -> std::io::Result<(Self, IpcMessageSender)> {
|
||||||
|
|
||||||
let mut listener = Listener::new()?;
|
let mut listener = Listener::new()?;
|
||||||
|
|
||||||
let mut signals = Signals::new(Signal::Interrupt | Signal::Quit)?;
|
let mut signals = Signals::new(Signal::Interrupt | Signal::Quit)?;
|
||||||
|
@ -327,4 +322,3 @@ impl Ipc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue