Skip to content

Commit

Permalink
support unix socket addresses in listen and connect
Browse files Browse the repository at this point in the history
  • Loading branch information
EliteTK committed Jun 3, 2024
1 parent 1c6025a commit 5858824
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
18 changes: 13 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeSet;
use std::fs;
use std::net::{IpAddr, Ipv4Addr};
use std::path::PathBuf;

use anyhow::{Context, Result};
use directories::ProjectDirs;
Expand All @@ -21,12 +22,12 @@ mod default {
10
}

pub fn listen() -> (IpAddr, u16) {
pub fn listen() -> ListenAddr {
// localhost & some random unprivileged port
(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 27_631)
ListenAddr::Tcp(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 27_631)
}

pub fn connect() -> (IpAddr, u16) {
pub fn connect() -> ListenAddr {
listen()
}

Expand Down Expand Up @@ -82,6 +83,13 @@ mod de {
}
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum ListenAddr {
Tcp(IpAddr, u16),
Unix(PathBuf),
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct Config {
Expand All @@ -94,10 +102,10 @@ pub struct Config {
pub gc_interval: u32,

#[serde(default = "default::listen")]
pub listen: (IpAddr, u16),
pub listen: ListenAddr,

#[serde(default = "default::connect")]
pub connect: (IpAddr, u16),
pub connect: ListenAddr,

#[serde(default = "default::log_filters")]
pub log_filters: String,
Expand Down
12 changes: 7 additions & 5 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{bail, Context, Result};
use serde::de::{DeserializeOwned, IgnoredAny};
use tokio::io::BufReader;

use crate::config::Config;
use crate::config::{Config, ListenAddr};
use crate::lsp::ext::{self, LspMuxOptions, StatusResponse};
use crate::lsp::jsonrpc::{Message, Request, RequestId, Version};
use crate::lsp::transport::{LspReader, LspWriter};
Expand All @@ -15,10 +15,12 @@ pub async fn ext_request<T>(config: &Config, method: ext::Request) -> Result<T>
where
T: DeserializeOwned,
{
let (reader, writer) = Stream::connect_tcp(config.connect)
.await
.context("connect")?
.into_split();
let (reader, writer) = match config.connect {
ListenAddr::Tcp(ip_addr, port) => Stream::connect_tcp((ip_addr, port)).await,
ListenAddr::Unix(ref path) => Stream::connect_unix(path).await,
}
.context("connect")?
.into_split();
let mut writer = LspWriter::new(writer, "lspmux");
let mut reader = LspReader::new(BufReader::new(reader), "lspmux");

Expand Down
10 changes: 6 additions & 4 deletions src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::env;
use anyhow::{bail, Context as _, Result};
use tokio::io::{self, BufStream};

use crate::config::Config;
use crate::config::{Config, ListenAddr};
use crate::lsp::ext::{LspMuxOptions, Request};
use crate::lsp::jsonrpc::Message;
use crate::lsp::transport::{LspReader, LspWriter};
Expand All @@ -23,9 +23,11 @@ pub async fn run(config: &Config, server: String, args: Vec<String>) -> Result<(
}
}

let mut stream = Stream::connect_tcp(config.connect)
.await
.context("connect")?;
let mut stream = match config.connect {
ListenAddr::Tcp(ip_addr, port) => Stream::connect_tcp((ip_addr, port)).await,
ListenAddr::Unix(ref path) => Stream::connect_unix(path).await,
}
.context("connect")?;
let mut stdio = BufStream::new(io::join(io::stdin(), io::stdout()));

// Wait for the client to send `initialize` request.
Expand Down
10 changes: 6 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ use tokio::task;
use tracing::{error, info, info_span, warn, Instrument};

use crate::client;
use crate::config::Config;
use crate::config::{Config, ListenAddr};
use crate::instance::InstanceMap;
use crate::socketwrapper::Listener;

pub async fn run(config: &Config) -> Result<()> {
let instance_map = InstanceMap::new(config).await;
let next_client_id = AtomicUsize::new(0);

let listener = Listener::bind_tcp(config.listen.into())
.await
.context("listen")?;
let listener = match config.listen {
ListenAddr::Tcp(ip_addr, port) => Listener::bind_tcp((ip_addr, port).into()).await,
ListenAddr::Unix(ref path) => Listener::bind_unix(path),
}
.context("listen")?;
loop {
match listener.accept().await {
Ok((socket, _addr)) => {
Expand Down
2 changes: 1 addition & 1 deletion src/socketwrapper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::Path;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::{io, net};
use std::{fs, io, net};

use pin_project::pin_project;
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
Expand Down

0 comments on commit 5858824

Please sign in to comment.