Skip to content

Commit

Permalink
feat(client): support listen and subscribe (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
fu050409 authored Sep 29, 2024
1 parent f7e20f6 commit fb42aaf
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changes/client-subscribe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"oblivion": patch:feat
---

Support listen and subscribe in Oblivion client.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions crates/oblivion/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
name = "oblivion"
version = "2.2.0"
authors = ["苏向夜 <fu050409@163.com>"]
description = "Rust High Concurrency Implementation of Oblivion, an End-to-End Encryption Protocol Based on ECDHE Encryption Algorithm"
description = "A fast, lightweight, and secure end-to-end encryption protocol based on ECDHE"
license = "AGPL-3.0"
repository = "https://github.com/noctisynth/oblivion-rust"

readme = "../../README.md"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -23,7 +23,7 @@ hkdf = "0.12"

# Utils
arc-swap = "1.7.1"
oblivion-codegen = { version = "^0.3.0", path = "../oblivion-codegen" }
oblivion-codegen = { version = "0.3.2", path = "../oblivion-codegen" }
proc-macro2 = { workspace = true }
futures = { workspace = true }
regex = "1"
Expand Down
24 changes: 23 additions & 1 deletion crates/oblivion/src/models/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use std::sync::Arc;
use anyhow::{Error, Result};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use tokio::net::TcpStream;
use tokio::{
net::TcpStream,
sync::mpsc::{Receiver, Sender},
task::JoinHandle,
};

use crate::exceptions::Exception;
#[cfg(feature = "pyo3")]
Expand Down Expand Up @@ -112,6 +116,8 @@ pub struct Client {
pub entrance: String,
pub path: OblivionPath,
pub session: Arc<Session>,
sender: Arc<Sender<Response>>,
receiver: Receiver<Response>,
}

impl Client {
Expand All @@ -134,13 +140,29 @@ impl Client {

session.handshake(0).await?;

let (sender, receiver) = tokio::sync::mpsc::channel(1024);
Ok(Self {
entrance: entrance.to_string(),
path,
session: Arc::new(session),
sender: Arc::new(sender),
receiver,
})
}

pub async fn listen(&self) -> JoinHandle<()> {
let session = self.session.clone();
let sender = self.sender.clone();
tokio::spawn(async move {
let response = session.recv().await.unwrap();
sender.send(response).await.unwrap();
})
}

pub async fn next(&mut self) -> Option<Response> {
self.receiver.recv().await
}

pub async fn send(&self, data: Vec<u8>) -> Result<()> {
self.session.send(data).await
}
Expand Down

0 comments on commit fb42aaf

Please sign in to comment.