完善jni调用

This commit is contained in:
lubeilin
2024-04-07 20:57:10 +08:00
parent a33ffd96fd
commit 28522f2f07
7 changed files with 71 additions and 10 deletions
+9 -1
View File
@@ -26,6 +26,11 @@ impl CallBack {
}
impl CallBack {
fn success0(&self) -> jni::errors::Result<()> {
let env = &mut self.jvm.attach_current_thread()? as &mut JNIEnv;
env.call_method(&self.this, "success", "()V", &[])?;
Ok(())
}
fn create_tun0(&self, info: DeviceInfo) -> jni::errors::Result<()> {
let env = &mut self.jvm.attach_current_thread()? as &mut JNIEnv;
let param = env.new_object(
@@ -139,13 +144,16 @@ impl CallBack {
}
fn stop0(&self) -> jni::errors::Result<()> {
let env = &mut self.jvm.attach_current_thread()? as &mut JNIEnv;
env.call_method(&self.this, "error", "()V", &[])?;
env.call_method(&self.this, "stop", "()V", &[])?;
Ok(())
}
}
impl VntCallback for CallBack {
fn success(&self) {
if let Err(e) = self.success0() {
log::warn!("success {:?}", e);
}
}
fn create_tun(&self, info: DeviceInfo) {
if let Err(e) = self.create_tun0(info) {
+7 -2
View File
@@ -29,6 +29,11 @@ pub fn new_config(env: &mut JNIEnv, config: JObject) -> Result<Config, Error> {
let use_channel = to_string(env, &config, "useChannel")?;
let finger = env.get_field(&config, "finger", "Z")?.z()?;
let first_latency = env.get_field(&config, "firstLatency", "Z")?.z()?;
let packet_delay = to_integer(env, &config, "packetDelay")?
.map(|v| v as u32)
.unwrap_or_default();
let packet_loss_rate = to_double(env, &config, "packetLossRate")?;
let in_ips = to_string_array(env, &config, "inIps")?;
let out_ips = to_string_array(env, &config, "outIps")?;
let ports =
@@ -132,8 +137,8 @@ pub fn new_config(env: &mut JNIEnv, config: JObject) -> Result<Config, Error> {
#[cfg(target_os = "android")]
device_fd,
UseChannelType::from_str(&use_channel.unwrap_or_default()).unwrap_or_default(),
None,
0,
packet_loss_rate,
packet_delay,
) {
Ok(config) => config,
Err(e) => {
+10
View File
@@ -119,3 +119,13 @@ pub fn to_integer(env: &mut JNIEnv, config: &JObject, name: &str) -> Result<Opti
env.call_method(value, "intValue", "()I", &[])?.i()? as _
));
}
pub fn to_double(env: &mut JNIEnv, config: &JObject, name: &str) -> Result<Option<f64>, Error> {
let value = env.get_field(config, name, "Ljava/lang/Double;")?.l()?;
if value.is_null() {
return Ok(None);
}
// 调用 intValue
return Ok(Some(
env.call_method(value, "doubleValue", "()D", &[])?.d()? as _,
));
}