mirror of
https://github.com/ClassiCube/ClassiCube.git
synced 2025-09-12 09:06:55 -04:00
Android: Use cached method refs for http
This commit is contained in:
parent
aa809491cc
commit
7bc5decec6
@ -631,6 +631,8 @@ static cc_result HttpBackend_Do(struct HttpRequest* req, cc_string* url) {
|
||||
*-----------------------------------------------------Android backend-----------------------------------------------------*
|
||||
*#########################################################################################################################*/
|
||||
struct HttpRequest* java_req;
|
||||
static JMethodID JAVA_httpInit, JAVA_httpSetHeader, JAVA_httpPerform, JAVA_httpSetData;
|
||||
static JMethodID JAVA_httpDescribeError;
|
||||
|
||||
cc_bool Http_DescribeError(cc_result res, cc_string* dst) {
|
||||
char buffer[NATIVE_STR_LEN];
|
||||
@ -641,7 +643,7 @@ cc_bool Http_DescribeError(cc_result res, cc_string* dst) {
|
||||
|
||||
JavaGetCurrentEnv(env);
|
||||
args[0].i = res;
|
||||
obj = JavaCallObject(env, "httpDescribeError", "(I)Ljava/lang/String;", args);
|
||||
obj = JavaInstanceCall_Obj(env, JAVA_httpDescribeError, args);
|
||||
if (!obj) return false;
|
||||
|
||||
err = JavaGetString(env, obj, buffer);
|
||||
@ -658,7 +660,7 @@ static void Http_AddHeader(struct HttpRequest* req, const char* key, const cc_st
|
||||
args[0].l = JavaMakeConst(env, key);
|
||||
args[1].l = JavaMakeString(env, value);
|
||||
|
||||
JavaCallVoid(env, "httpSetHeader", "(Ljava/lang/String;Ljava/lang/String;)V", args);
|
||||
JavaInstanceCall_Void(env, JAVA_httpSetHeader, args);
|
||||
(*env)->DeleteLocalRef(env, args[0].l);
|
||||
(*env)->DeleteLocalRef(env, args[1].l);
|
||||
}
|
||||
@ -684,10 +686,20 @@ static const JNINativeMethod methods[2] = {
|
||||
{ "httpParseHeader", "(Ljava/lang/String;)V", java_HttpParseHeader },
|
||||
{ "httpAppendData", "([BI)V", java_HttpAppendData }
|
||||
};
|
||||
static void CacheMethodRefs(JNIEnv* env) {
|
||||
JAVA_httpInit = JavaGetMethod(env, "httpInit", "(Ljava/lang/String;Ljava/lang/String;)I");
|
||||
JAVA_httpSetHeader = JavaGetMethod(env, "httpSetHeader", "(Ljava/lang/String;Ljava/lang/String;)V");
|
||||
JAVA_httpPerform = JavaGetMethod(env, "httpPerform", "()I");
|
||||
JAVA_httpSetData = JavaGetMethod(env, "httpSetData", "([B)I");
|
||||
|
||||
JAVA_httpDescribeError = JavaGetMethod(env, "httpDescribeError", "(I)Ljava/lang/String;");
|
||||
}
|
||||
|
||||
static void HttpBackend_Init(void) {
|
||||
JNIEnv* env;
|
||||
JavaGetCurrentEnv(env);
|
||||
JavaRegisterNatives(env, methods);
|
||||
CacheMethodRefs(env);
|
||||
}
|
||||
|
||||
static cc_result Http_InitReq(JNIEnv* env, struct HttpRequest* req, cc_string* url) {
|
||||
@ -698,7 +710,7 @@ static cc_result Http_InitReq(JNIEnv* env, struct HttpRequest* req, cc_string* u
|
||||
args[0].l = JavaMakeString(env, url);
|
||||
args[1].l = JavaMakeConst(env, verbs[req->requestType]);
|
||||
|
||||
res = JavaCallInt(env, "httpInit", "(Ljava/lang/String;Ljava/lang/String;)I", args);
|
||||
res = JavaInstanceCall_Int(env, JAVA_httpInit, args);
|
||||
(*env)->DeleteLocalRef(env, args[0].l);
|
||||
(*env)->DeleteLocalRef(env, args[1].l);
|
||||
return res;
|
||||
@ -709,7 +721,7 @@ static cc_result Http_SetData(JNIEnv* env, struct HttpRequest* req) {
|
||||
jint res;
|
||||
|
||||
args[0].l = JavaMakeBytes(env, req->data, req->size);
|
||||
res = JavaCallInt(env, "httpSetData", "([B)I", args);
|
||||
res = JavaInstanceCall_Int(env, JAVA_httpSetData, args);
|
||||
(*env)->DeleteLocalRef(env, args[0].l);
|
||||
return res;
|
||||
}
|
||||
@ -729,7 +741,7 @@ static cc_result HttpBackend_Do(struct HttpRequest* req, cc_string* url) {
|
||||
|
||||
req->_capacity = 0;
|
||||
http_curProgress = HTTP_PROGRESS_FETCHING_DATA;
|
||||
res = JavaCallInt(env, "httpPerform", "()I", NULL);
|
||||
res = JavaInstanceCall_Int(env, JAVA_httpPerform, args);
|
||||
http_curProgress = 100;
|
||||
return res;
|
||||
}
|
||||
|
@ -244,9 +244,11 @@ extern jclass App_Class;
|
||||
extern jobject App_Instance;
|
||||
extern JavaVM* VM_Ptr;
|
||||
|
||||
#define JavaGetCurrentEnv(env) (*VM_Ptr)->AttachCurrentThread(VM_Ptr, &env, NULL);
|
||||
#define JavaMakeConst(env, str) (*env)->NewStringUTF(env, str);
|
||||
#define JavaGetCurrentEnv(env) (*VM_Ptr)->AttachCurrentThread(VM_Ptr, &env, NULL)
|
||||
#define JavaMakeConst(env, str) (*env)->NewStringUTF(env, str)
|
||||
|
||||
#define JavaRegisterNatives(env, methods) (*env)->RegisterNatives(env, App_Class, methods, Array_Elems(methods));
|
||||
#define JavaGetMethod(env, name, sig) (*env)->GetMethodID(env, App_Class, name, sig)
|
||||
|
||||
/* Creates a string from the given java string. buffer must be at least NATIVE_STR_LEN long. */
|
||||
/* NOTE: Don't forget to call env->ReleaseStringUTFChars. Only works with ASCII strings. */
|
||||
@ -271,5 +273,11 @@ void JavaCall_String_Void(const char* name, const cc_string* value);
|
||||
void JavaCall_Void_String(const char* name, cc_string* dst);
|
||||
/* Calls a method in the activity class that takes a string and returns a string. */
|
||||
void JavaCall_String_String(const char* name, const cc_string* arg, cc_string* dst);
|
||||
|
||||
#define JavaInstanceCall_Void(env, method, args) (*env)->CallVoidMethodA(env, App_Instance, method, args)
|
||||
#define JavaInstanceCall_Int(env, method, args) (*env)->CallIntMethodA(env, App_Instance, method, args)
|
||||
#define JavaInstanceCall_Long(env, method, args) (*env)->CallLongMethodA(env, App_Instance, method, args)
|
||||
#define JavaInstanceCall_Float(env,method, args) (*env)->CallFloatMethodA(env, App_Instance, method, args)
|
||||
#define JavaInstanceCall_Obj(env, method, args) (*env)->CallObjectMethodA(env,App_Instance, method, args)
|
||||
#endif
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user