more fixups

pull/869/head
jeff 5 years ago
parent 4c584dd2da
commit bcf792d772

@ -24,7 +24,7 @@ extern "C
Java_network_loki_lokinet_LokinetConfig_Load(JNIEnv* env, jobject self, Java_network_loki_lokinet_LokinetConfig_Load(JNIEnv* env, jobject self,
jstring fname) jstring fname)
{ {
llarp_config* conf = FromObjectMember< llarp_config >(env, self, "impl"); llarp_config* conf = GetImpl< llarp_config >(env, self);
if(conf == nullptr) if(conf == nullptr)
return JNI_FALSE; return JNI_FALSE;
return VisitStringAsStringView< jboolean >( return VisitStringAsStringView< jboolean >(

@ -25,8 +25,8 @@ extern "C"
Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv *env, jobject self, Java_network_loki_lokinet_LokinetDaemon_Configure(JNIEnv *env, jobject self,
jobject conf) jobject conf)
{ {
llarp_main *ptr = FromObjectMember< llarp_main >(env, self, "impl"); llarp_main *ptr = GetImpl< llarp_main >(env, self);
llarp_config *config = FromObjectMember< llarp_config >(env, conf, "impl"); llarp_config *config = GetImpl< llarp_config >(env, conf);
if(ptr == nullptr || config == nullptr) if(ptr == nullptr || config == nullptr)
return JNI_FALSE; return JNI_FALSE;
if(llarp_main_configure(ptr, config)) if(llarp_main_configure(ptr, config))
@ -38,7 +38,7 @@ extern "C"
Java_network_loki_lokinet_LokinetDaemon_Mainloop(JNIEnv *env, jobject self) Java_network_loki_lokinet_LokinetDaemon_Mainloop(JNIEnv *env, jobject self)
{ {
static llarp_main_runtime_opts opts; static llarp_main_runtime_opts opts;
llarp_main *ptr = FromObjectMember< llarp_main >(env, self, "impl"); llarp_main *ptr = GetImpl< llarp_main >(env, self);
if(ptr == nullptr) if(ptr == nullptr)
return -1; return -1;
return llarp_main_run(ptr, opts); return llarp_main_run(ptr, opts);
@ -47,7 +47,7 @@ extern "C"
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_IsRunning(JNIEnv *env, jobject self) Java_network_loki_lokinet_LokinetDaemon_IsRunning(JNIEnv *env, jobject self)
{ {
llarp_main *ptr = FromObjectMember< llarp_main >(env, self, "impl"); llarp_main *ptr = GetImpl< llarp_main >(env, self);
return (ptr != nullptr && llarp_main_is_running(ptr)) ? JNI_TRUE return (ptr != nullptr && llarp_main_is_running(ptr)) ? JNI_TRUE
: JNI_FALSE; : JNI_FALSE;
} }
@ -55,7 +55,7 @@ extern "C"
JNIEXPORT jboolean JNICALL JNIEXPORT jboolean JNICALL
Java_network_loki_lokinet_LokinetDaemon_Stop(JNIEnv *env, jobject self) Java_network_loki_lokinet_LokinetDaemon_Stop(JNIEnv *env, jobject self)
{ {
llarp_main *ptr = FromObjectMember< llarp_main >(env, self, "impl"); llarp_main *ptr = GetImpl< llarp_main >(env, self);
if(ptr == nullptr) if(ptr == nullptr)
return JNI_FALSE; return JNI_FALSE;
if(not llarp_main_is_running(ptr)) if(not llarp_main_is_running(ptr))
@ -68,13 +68,14 @@ extern "C"
Java_network_loki_lokinet_LokinetDaemon_InjectVPN(JNIEnv *env, jobject self, Java_network_loki_lokinet_LokinetDaemon_InjectVPN(JNIEnv *env, jobject self,
jobject vpn) jobject vpn)
{ {
llarp_main *ptr = FromObjectMember< llarp_main >(env, self, "impl"); llarp_main *ptr = GetImpl< llarp_main >(env, self);
lokinet_jni_vpnio *impl = lokinet_jni_vpnio *impl = GetImpl< lokinet_jni_vpnio >(env, vpn);
FromObjectMember< lokinet_jni_vpnio >(env, vpn, "impl");
if(ptr == nullptr || impl == nullptr) if(ptr == nullptr || impl == nullptr)
return JNI_FALSE; return JNI_FALSE;
if(impl->info.netmask == 0)
return JNI_FALSE;
if(not impl->Init(ptr)) if(not impl->Init(ptr))
return JNI_FASE; return JNI_FALSE;
return llarp_main_inject_default_vpn(ptr, &impl->io, impl->info) return llarp_main_inject_default_vpn(ptr, &impl->io, impl->info)
? JNI_TRUE ? JNI_TRUE
: JNI_FALSE; : JNI_FALSE;

@ -6,6 +6,7 @@
#include <functional> #include <functional>
/// visit string as native bytes /// visit string as native bytes
/// jvm uses some unholy encoding internally so we convert it to utf-8
template < typename T, typename V > template < typename T, typename V >
static T static T
VisitStringAsStringView(JNIEnv* env, jobject str, V visit) VisitStringAsStringView(JNIEnv* env, jobject str, V visit)
@ -19,10 +20,11 @@ VisitStringAsStringView(JNIEnv* env, jobject str, V visit)
(jbyteArray)env->CallObjectMethod(str, getBytes, charsetName); (jbyteArray)env->CallObjectMethod(str, getBytes, charsetName);
env->DeleteLocalRef(charsetName); env->DeleteLocalRef(charsetName);
const jsize length = env->GetArrayLength(stringJbytes); const size_t length = env->GetArrayLength(stringJbytes);
const jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL); jbyte* pBytes = env->GetByteArrayElements(stringJbytes, NULL);
T result = visit(llarp::string_view(pBytes, length)); T result =
visit(llarp::string_view(static_cast< const char* >(pBytes), length));
env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT); env->ReleaseByteArrayElements(stringJbytes, pBytes, JNI_ABORT);
env->DeleteLocalRef(stringJbytes); env->DeleteLocalRef(stringJbytes);
@ -65,13 +67,20 @@ VisitObjectMemberStringAsStringView(JNIEnv* env, jobject self,
/// get object member int called membername /// get object member int called membername
template < typename Int_t > template < typename Int_t >
void Int_t
GetObjectMemberAsInt(JNIEnv* env, jobject self, const char* membername, GetObjectMemberAsInt(JNIEnv* env, jobject self, const char* membername)
Int_t& result)
{ {
jclass cl = env->GetObjectClass(self); jclass cl = env->GetObjectClass(self);
jfieldID name = env->GetFieldID(cl, membername, "I"); jfieldID name = env->GetFieldID(cl, membername, "I");
result = env->GetIntField(self, name); return env->GetIntField(self, name);
}
/// get implementation on jni type
template < typename T >
T*
GetImpl(JNIEnv* env, jobject self)
{
return FromObjectMember< T >(env, self, "impl");
} }
#endif #endif

@ -6,6 +6,7 @@
#include <future> #include <future>
#include <util/string_view.hpp> #include <util/string_view.hpp>
#include <algorithm> #include <algorithm>
#include <jni.h>
namespace lokinet namespace lokinet
{ {
@ -27,7 +28,7 @@ namespace lokinet
Closed() Closed()
{ {
if(closeWaiter) if(closeWaiter)
closeWaiter->get_future().set_value(); closeWaiter->set_value();
} }
virtual void virtual void
@ -57,19 +58,27 @@ namespace lokinet
bool bool
Init(llarp_main *ptr) Init(llarp_main *ptr)
{ {
if(Ready())
return false;
return llarp_vpn_io_init(ptr, &io); return llarp_vpn_io_init(ptr, &io);
} }
bool
Ready() const
{
return io.impl != nullptr;
}
void void
Close() Close()
{ {
if(io.impl == nullptr) if(not Ready())
return; return;
if(closeWaiter) if(closeWaiter)
return; return;
closerWaiter = std::make_unique< std::promise< void > >(); closeWaiter = std::make_unique< std::promise< void > >();
llarp_vpn_io_close_async(&io); llarp_vpn_io_close_async(&io);
closeWaiter->wait(); closeWaiter->get_future().wait();
closeWaiter.reset(); closeWaiter.reset();
io.impl = nullptr; io.impl = nullptr;
} }
@ -89,7 +98,7 @@ namespace lokinet
ssize_t ssize_t
ReadPacket(void *dst, size_t len) ReadPacket(void *dst, size_t len)
{ {
if(io.impl == nullptr) if(not Ready())
return -1; return -1;
unsigned char *buf = (unsigned char *)dst; unsigned char *buf = (unsigned char *)dst;
return llarp_vpn_io_readpkt(Reader(), buf, len); return llarp_vpn_io_readpkt(Reader(), buf, len);
@ -98,7 +107,7 @@ namespace lokinet
bool bool
WritePacket(void *pkt, size_t len) WritePacket(void *pkt, size_t len)
{ {
if(io.impl == nullptr) if(not Ready())
return false; return false;
unsigned char *buf = (unsigned char *)pkt; unsigned char *buf = (unsigned char *)pkt;
return llarp_vpn_io_writepkt(Writer(), buf, len); return llarp_vpn_io_writepkt(Writer(), buf, len);

@ -29,8 +29,7 @@ extern "C"
JNIEXPORT void JNICALL JNIEXPORT void JNICALL
Java_network_loki_lokinet_LokinetVPN_Stop(JNIEnv *env, jobject self) Java_network_loki_lokinet_LokinetVPN_Stop(JNIEnv *env, jobject self)
{ {
lokinet_jni_vpnio *vpn = lokinet_jni_vpnio *vpn = GetImpl< lokinet_jni_vpnio >(env, self);
FromObjectMember< lokinet_jni_vpnio >(env, self, "impl");
if(vpn) if(vpn)
{ {
vpn->Close(); vpn->Close();
@ -41,8 +40,7 @@ extern "C"
Java_network_loki_lokinet_LokinetVPN_ReadPkt(JNIEnv *env, jobject self, Java_network_loki_lokinet_LokinetVPN_ReadPkt(JNIEnv *env, jobject self,
jobject pkt) jobject pkt)
{ {
lokinet_jni_vpnio *vpn = lokinet_jni_vpnio *vpn = GetImpl< lokinet_jni_vpnio >(env, self);
FromObjectMember< lokinet_jni_vpnio >(env, self, "impl");
if(vpn == nullptr) if(vpn == nullptr)
return -1; return -1;
void *pktbuf = env->GetDirectBufferAddress(pkt); void *pktbuf = env->GetDirectBufferAddress(pkt);
@ -56,8 +54,7 @@ extern "C"
Java_network_loki_lokinet_LokinetVPN_WritePkt(JNIEnv *env, jobject self, Java_network_loki_lokinet_LokinetVPN_WritePkt(JNIEnv *env, jobject self,
jobject pkt) jobject pkt)
{ {
lokinet_jni_vpnio *vpn = lokinet_jni_vpnio *vpn = GetImpl< lokinet_jni_vpnio >(env, self);
FromObjectMember< lokinet_jni_vpnio >(env, self, "impl");
if(vpn == nullptr) if(vpn == nullptr)
return false; return false;
void *pktbuf = env->GetDirectBufferAddress(pkt); void *pktbuf = env->GetDirectBufferAddress(pkt);
@ -71,8 +68,7 @@ extern "C"
Java_network_loki_lokinet_LokinetVPN_SetInfo(JNIEnv *env, jobject self, Java_network_loki_lokinet_LokinetVPN_SetInfo(JNIEnv *env, jobject self,
jobject info) jobject info)
{ {
lokinet_jni_vpnio *vpn = lokinet_jni_vpnio *vpn = GetImpl< lokinet_jni_vpnio >(env, self);
FromObjectMember< lokinet_jni_vpnio >(env, self, "impl");
if(vpn == nullptr) if(vpn == nullptr)
return; return;
VisitObjectMemberStringAsStringView< bool >( VisitObjectMemberStringAsStringView< bool >(
@ -85,6 +81,6 @@ extern "C"
vpn->SetIfName(val); vpn->SetIfName(val);
return true; return true;
}); });
GetObjectMemberAsInt(env, info, "netmask", vpn->info.netmask); vpn->info.netmask = GetObjectMemberAsInt< uint8_t >(env, info, "netmask");
} }
} }
Loading…
Cancel
Save