#ifdef ANDROID
#include <jni.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include "hev-main.h"
#include "hev-jni.h"
#ifndef PKGNAME
#define PKGNAME hev/htproxy
#endif
#define STR(s) STR_ARG (s)
#define STR_ARG(c) #c
#define N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
typedef struct _ThreadData ThreadData;
struct _ThreadData
{
char *path;
int fd;
};
static JavaVM *java_vm;
static pthread_t work_thread;
static pthread_key_t current_jni_env;
static void native_start_service (JNIEnv *env, jobject thiz, jstring conig_path,
jint fd);
static void native_stop_service (JNIEnv *env, jobject thiz);
static JNINativeMethod native_methods[] = {
{ "TProxyStartService", "(Ljava/lang/String;I)V",
(void *)native_start_service },
{ "TProxyStopService", "()V", (void *)native_stop_service },
};
static void
detach_current_thread (void *env)
{
(*java_vm)->DetachCurrentThread (java_vm);
}
jint
JNI_OnLoad (JavaVM *vm, void *reserved)
{
JNIEnv *env = NULL;
jclass klass;
java_vm = vm;
if (JNI_OK != (*vm)->GetEnv (vm, (void **)&env, JNI_VERSION_1_4)) {
return 0;
}
klass = (*env)->FindClass (env, STR (PKGNAME) "/TProxyService");
(*env)->RegisterNatives (env, klass, native_methods,
N_ELEMENTS (native_methods));
(*env)->DeleteLocalRef (env, klass);
pthread_key_create (¤t_jni_env, detach_current_thread);
return JNI_VERSION_1_4;
}
static void *
thread_handler (void *data)
{
ThreadData *tdata = data;
hev_socks5_tunnel_main (tdata->path, tdata->fd);
free (tdata->path);
free (tdata);
return NULL;
}
static void
native_start_service (JNIEnv *env, jobject thiz, jstring config_path, jint fd)
{
const jbyte *bytes;
ThreadData *tdata;
if (work_thread)
return;
tdata = malloc (sizeof (ThreadData));
tdata->fd = fd;
bytes = (const jbyte *)(*env)->GetStringUTFChars (env, config_path, NULL);
tdata->path = strdup ((const char *)bytes);
(*env)->ReleaseStringUTFChars (env, config_path, (const char *)bytes);
pthread_create (&work_thread, NULL, thread_handler, tdata);
}
static void
native_stop_service (JNIEnv *env, jobject thiz)
{
if (!work_thread)
return;
hev_socks5_tunnel_quit ();
pthread_join (work_thread, NULL);
work_thread = 0;
}
#endif