Skip to content
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

feat(client): support listen and subscribe #77

Merged
merged 1 commit into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = ["苏向夜 <[email protected]>"]
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
Loading