完善jni调用
This commit is contained in:
@@ -8,6 +8,10 @@ import top.wherewego.vnt.jni.param.*;
|
||||
* @author https://github.com/lbl8603/vnt
|
||||
*/
|
||||
public interface CallBack {
|
||||
/**
|
||||
* 连接成功的回调
|
||||
*/
|
||||
void success();
|
||||
/**
|
||||
* 创建虚拟网卡成功的回调方法
|
||||
*
|
||||
|
||||
@@ -90,6 +90,18 @@ public class Config {
|
||||
* 虚拟网卡fd 仅在android上支持
|
||||
*/
|
||||
private int deviceFd;
|
||||
/**
|
||||
* enum: relay/p2p/all
|
||||
*/
|
||||
private String useChannel;
|
||||
/**
|
||||
* 模拟丢包率,取0~1之间的数,为null表示不丢包,1表示全部丢包
|
||||
*/
|
||||
private Double packetLossRate;
|
||||
/**
|
||||
* 模拟延迟 单位毫秒(ms)
|
||||
*/
|
||||
private Integer packetDelay;
|
||||
|
||||
public Config() {
|
||||
}
|
||||
@@ -261,4 +273,28 @@ public class Config {
|
||||
public void setDeviceFd(int deviceFd) {
|
||||
this.deviceFd = deviceFd;
|
||||
}
|
||||
|
||||
public String getUseChannel() {
|
||||
return useChannel;
|
||||
}
|
||||
|
||||
public void setUseChannel(String useChannel) {
|
||||
this.useChannel = useChannel;
|
||||
}
|
||||
|
||||
public Double getPacketLossRate() {
|
||||
return packetLossRate;
|
||||
}
|
||||
|
||||
public void setPacketLossRate(Double packetLossRate) {
|
||||
this.packetLossRate = packetLossRate;
|
||||
}
|
||||
|
||||
public Integer getPacketDelay() {
|
||||
return packetDelay;
|
||||
}
|
||||
|
||||
public void setPacketDelay(Integer packetDelay) {
|
||||
this.packetDelay = packetDelay;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package top.wherewego.vnt.jni;
|
||||
|
||||
/**
|
||||
* @author lubeilin
|
||||
* @date: 2024/02/27 18:31
|
||||
* ip转换
|
||||
*
|
||||
* @author https://github.com/lbl8603/vnt
|
||||
*/
|
||||
public class IpUtils {
|
||||
public static String intToIpAddress(int ipAddress) {
|
||||
|
||||
@@ -11,11 +11,8 @@ import java.io.IOException;
|
||||
public class Vnt implements Closeable {
|
||||
private final long raw;
|
||||
|
||||
public Vnt(Config config, CallBack callBack) {
|
||||
public Vnt(Config config, CallBack callBack) throws Exception{
|
||||
this.raw = new0(config, callBack);
|
||||
if(this.raw == 0){
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
@@ -30,7 +27,7 @@ public class Vnt implements Closeable {
|
||||
return list0(raw);
|
||||
}
|
||||
|
||||
private native long new0(Config config, CallBack callBack);
|
||||
private native long new0(Config config, CallBack callBack) throws Exception;
|
||||
|
||||
private native void stop0(long raw);
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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 _,
|
||||
));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user