Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/vyfor/cord.nvim
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed Apr 10, 2024
2 parents ce1e565 + d59b10f commit ce4eb7a
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Rust

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
7 changes: 7 additions & 0 deletions src/ipc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ pub struct RichClient {
pub last_activity: Option<Activity>,
}

#[cfg(not(target_os = "windows"))]
pub struct RichClient {
pub client_id: u64,
pub stream: std::os::unix::net::UnixStream,
pub last_activity: Option<Activity>,
}

pub trait Connection {
fn connect(
client_id: u64,
Expand Down
3 changes: 3 additions & 0 deletions src/ipc/platform/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
#[cfg(target_os = "windows")]
pub mod windows_connection;

#[cfg(not(target_os = "windows"))]
pub mod unix_connection;
77 changes: 77 additions & 0 deletions src/ipc/platform/unix_connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::env::var;
use std::io::{self, Read, Write};
use std::os::unix::net::UnixStream;

use crate::ipc::client::{Connection, RichClient};
use crate::ipc::utils;

impl Connection for RichClient {
fn connect(client_id: u64) -> Result<Self, Box<dyn std::error::Error>> {
let path = var("XDG_RUNTIME_DIR")
.or_else(|_| var("TMPDIR"))
.or_else(|_| var("TMP"))
.or_else(|_| var("TEMP"))
.unwrap_or_else(|_| "/tmp".to_string());
for i in 0..10 {
match UnixStream::connect(format!("{}/discord-ipc-{}", path, i)) {
Ok(stream) => {
return Ok(RichClient {
client_id: client_id,
stream: stream,
last_activity: None,
})
}
Err(e) => match e.kind() {
io::ErrorKind::ConnectionRefused => continue,
_ => return Err(e.into()),
},
}
}

Err("Socket not found".into())
}

fn write(&mut self, opcode: u32, data: Option<&[u8]>) -> io::Result<()> {
if let Some(packet) = data {
self.stream
.write_all(utils::encode(opcode, packet.len() as u32).as_slice())?;
self.stream.write_all(packet)?;
} else {
self.stream.write_all(utils::encode(opcode, 0).as_slice())?;
}
Ok(())
}

fn read(&mut self) -> io::Result<Vec<u8>> {
let mut header = [0; 8];
self.stream.read_exact(&mut header)?;
let mut buffer = vec![0u8; utils::decode(&header) as usize];
self.stream.read_exact(&mut buffer)?;
Ok(buffer)
}

fn close(&mut self) -> io::Result<()> {
self.stream.write_all(utils::encode(2, 0).as_slice())?;
self.stream.flush()?;
Ok(())
}

fn handshake(&mut self) -> io::Result<()> {
self.write(
0,
Some((format!("{{\"v\": 1,\"client_id\":\"{}\"}}", self.client_id)).as_bytes()),
)
}

fn update(&mut self, packet: &crate::rpc::packet::Packet) -> io::Result<()> {
if packet.activity != self.last_activity {
self.write(1, Some(packet.to_json().unwrap().as_bytes()))
} else {
Ok(())
}
}

fn clear(&mut self) -> io::Result<()> {
self.write(1, None)
}
}
2 changes: 0 additions & 2 deletions src/ipc/platform/windows_connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![cfg(target_os = "windows")]

use std::fs::OpenOptions;
use std::io::{self, Read, Write};
use std::os::windows::fs::OpenOptionsExt;
Expand Down

0 comments on commit ce4eb7a

Please sign in to comment.