-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Desktop: Forward file args from secondary launches to the running instance via local socket #4123
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
timon-schelling
wants to merge
4
commits into
master
Choose a base branch
from
desktop-socket-mvp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| use interprocess::local_socket::{GenericFilePath, GenericNamespaced, ListenerNonblockingMode, ListenerOptions, Name, prelude::*}; | ||
| use std::io::{ErrorKind, Read, Write}; | ||
| use std::sync::mpsc; | ||
| use std::thread; | ||
| use std::time::Duration; | ||
|
|
||
| use crate::consts::APP_SOCKET_FILE_NAME; | ||
| use crate::event::{AppEvent, AppEventScheduler}; | ||
|
|
||
| // TODO: Needs to be integrated/replaced with the action system. | ||
| // TODO: At that point this should just wrap the action, meaning all actions bindable by the user can also be accessed via the socket. | ||
| #[derive(serde::Serialize, serde::Deserialize)] | ||
| pub(crate) enum Message { | ||
| OpenFiles(Vec<std::path::PathBuf>), | ||
| } | ||
|
|
||
| fn handle_message(message: Message, app_event_scheduler: &AppEventScheduler) { | ||
| match message { | ||
| Message::OpenFiles(paths) => { | ||
| app_event_scheduler.schedule(AppEvent::OpenFiles(paths)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn send(message: Message) -> std::io::Result<()> { | ||
| let data = ron::ser::to_string(&message).map_err(|error| std::io::Error::new(std::io::ErrorKind::InvalidData, error))?; | ||
| let mut connection = interprocess::local_socket::Stream::connect(socket_name())?; | ||
| connection.write_all(data.as_bytes()) | ||
| } | ||
|
|
||
| pub(crate) struct SocketHandle { | ||
| thread: Option<thread::JoinHandle<()>>, | ||
| shutdown_sender: mpsc::Sender<()>, | ||
| } | ||
| impl Drop for SocketHandle { | ||
| fn drop(&mut self) { | ||
| let _ = self.shutdown_sender.send(()); | ||
| let _ = self.thread.take().expect("SocketHandle can only be dropped once").join(); | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn start(app_event_scheduler: AppEventScheduler) -> SocketHandle { | ||
| let (shutdown_sender, shutdown_receiver) = mpsc::channel(); | ||
|
|
||
| let thread = thread::Builder::new() | ||
| .name("socket".to_string()) | ||
| .spawn(move || run(app_event_scheduler, shutdown_receiver)) | ||
| .expect("Failed to spawn socket thread"); | ||
|
|
||
| SocketHandle { | ||
| shutdown_sender, | ||
| thread: Some(thread), | ||
| } | ||
| } | ||
|
|
||
| fn run(app_event_scheduler: AppEventScheduler, shutdown_receiver: mpsc::Receiver<()>) { | ||
| let listener = match ListenerOptions::new() | ||
| .name(socket_name()) | ||
| .nonblocking(ListenerNonblockingMode::Accept) | ||
| .try_overwrite(true) | ||
| .max_spin_time(Duration::from_millis(100)) | ||
| .create_sync() | ||
| { | ||
| Ok(listener) => listener, | ||
| Err(error) => { | ||
| tracing::error!("Failed to bind socket: {}", error); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| let max_backoff = Duration::from_millis(100); | ||
| let mut backoff = Duration::ZERO; | ||
|
|
||
| loop { | ||
| if backoff.is_zero() { | ||
| match shutdown_receiver.try_recv() { | ||
| Ok(()) | Err(mpsc::TryRecvError::Disconnected) => break, | ||
| Err(mpsc::TryRecvError::Empty) => {} | ||
| } | ||
| backoff = Duration::from_nanos(1); | ||
| } else { | ||
| match shutdown_receiver.recv_timeout(backoff) { | ||
| Ok(()) | Err(mpsc::RecvTimeoutError::Disconnected) => break, | ||
| Err(mpsc::RecvTimeoutError::Timeout) => {} | ||
| } | ||
| backoff = (backoff * 2).min(max_backoff); | ||
| } | ||
|
|
||
| match listener.accept() { | ||
| Ok(mut connection) => { | ||
| backoff = Duration::ZERO; | ||
|
|
||
| let app_event_scheduler = app_event_scheduler.clone(); | ||
| let spawn_result = thread::Builder::new().name("socket-connection".to_string()).spawn(move || { | ||
| let mut data = String::new(); | ||
| if let Err(error) = connection.read_to_string(&mut data) { | ||
| tracing::error!("Failed to read socket message: {}", error); | ||
| return; | ||
| } | ||
|
|
||
| match ron::de::from_str(&data) { | ||
| Ok(message) => handle_message(message, &app_event_scheduler), | ||
| Err(error) => tracing::error!("Failed to deserialize socket message: {}", error), | ||
| } | ||
| }); | ||
| if let Err(error) = spawn_result { | ||
| tracing::error!("Failed to spawn socket connection thread: {}", error); | ||
| } | ||
| } | ||
| Err(error) if matches!(error.kind(), ErrorKind::WouldBlock | ErrorKind::Interrupted) => {} | ||
| Err(error) => { | ||
| tracing::error!("Failed to accept socket connection: {}", error); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn socket_name() -> Name<'static> { | ||
| if cfg!(target_os = "windows") { | ||
| let dir = crate::dirs::app_data_dir().to_string_lossy().replace('\\', "-"); | ||
| let name = format!("graphite-{dir}-{APP_SOCKET_FILE_NAME}"); | ||
| name.to_ns_name::<GenericNamespaced>().expect("should be avalid named pipe name") | ||
| } else { | ||
| crate::dirs::app_data_dir().join(APP_SOCKET_FILE_NAME).to_fs_name::<GenericFilePath>().expect("valid socket path") | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.