gpt4 book ai didi

java - JNI 回调调用失败

转载 作者:行者123 更新时间:2023-12-01 14:05:59 28 4
gpt4 key购买 nike

我目前正在为 Tox 聊天库编写 JNI 包装器。很多事情都是通过回调来处理的(比如接收消息、好友请求等)。为了处理这些问题,我已经实现了一些有效的东西(到目前为止,我不明白为什么它停止工作):

在主类中,我定义了一个允许设置回调的方法,如下所示:

    /**
* Native call to tox_callback_friendrequest
*
* @param messengerPointer
* pointer to the internal messenger struct
* @param callback
* the callback to set for receiving friend requests
*/
private native void tox_onfriendrequest(long messengerPointer,
OnFriendRequestCallback callback);

/**
* Method used to set a callback method for receiving friend requests. Any
* time a friend request is received on this Tox instance, the
* {@link OnFriendRequestCallback#execute(String, String)} method will be
* executed.
*
* @param callback
* the callback to set for receiving friend requests
* @throws ToxException
* if the instance has been killed
*/
public void setOnFriendRequestCallback(OnFriendRequestCallback callback)
throws ToxException {
lock.lock();
try {
checkPointer();

tox_onfriendrequest(this.messengerPointer, callback);
} finally {
lock.unlock();
}
}

公共(public)java方法只需调用以下 native 代码:


JNIEXPORT void JNICALL Java_im_tox_jtoxcore_JTox_tox_1onfriendrequest(
JNIEnv * env, jobject obj, jlong messenger, jobject callback) {
tox_jni_globals_t *_messenger = (tox_jni_globals_t *) messenger;
if (_messenger->frqc) {
if (_messenger->frqc->jobj) {
(*env)->DeleteGlobalRef(env, _messenger->frqc->jobj);
}
free(_messenger->frqc);
}

friendrequest_callback_t *data = malloc(sizeof(friendrequest_callback_t));
data->env = env;
data->jobj = (*env)->NewGlobalRef(env, callback);
(*env)->DeleteLocalRef(env, callback);
_messenger->frqc = data;
tox_callback_friendrequest(_messenger->tox, (void *) callback_friendrequest,
data);
}

static void callback_friendrequest(uint8_t *pubkey, uint8_t *message,
uint16_t length, void *ptr) {
friendrequest_callback_t *data = ptr;

jclass clazz = (*data->env)->GetObjectClass(data->env, data->jobj);
jmethodID meth = (*data->env)->GetMethodID(data->env, clazz, "execute",
"(Ljava/lang/String;[B)V");

char buf[ADDR_SIZE_HEX] = { 0 };
addr_to_hex(pubkey, buf);
jstring _pubkey = (*data->env)->NewStringUTF(data->env, buf);
jbyteArray _message = (*data->env)->NewByteArray(data->env, length);
(*data->env)->SetByteArrayRegion(data->env, _message, 0, length, message);

printf("definitely happening"); //printf debugging. This IS being printed.
fflush(stdout);
(*data->env)->CallVoidMethod(data->env, data->jobj, meth, _pubkey, message); //this does not seem to be called
}

这段代码相当复杂,但它的作用如下:首先,它检查是否已经为此操作设置了回调,如果是,则将其删除。然后,它为收到的 Callback 对象创建一个新的全局引用,并保存该引用以及指向信使结构的 JNIEnv 指针。最后,它调用Tox-API提供的回调方法,并注册回调static void callback_friendrequest

在不透明的void指针ptr中,我们存储JNIEnv和Callback对象。回调类如下所示:

package im.tox.jtoxcore.test;

import im.tox.jtoxcore.JTox;
import im.tox.jtoxcore.ToxException;
import im.tox.jtoxcore.callbacks.OnFriendRequestCallback;

public class TestOnFriendRequestCallback extends OnFriendRequestCallback {

public TestOnFriendRequestCallback(JTox jtox) {
super(jtox);
}

@Override
public void execute(String publicKey, byte[] message) {
String msg;
if (message == null) {
msg = "No mesage";
} else {
try {
msg = JTox.getByteString(message);
} catch (ToxException e) {
msg = "ERROR: Unable to get message";
}
}
System.out.println("We received a friend request from " + publicKey
+ " with the following message: " + msg);

try {
jtox.confirmRequest(publicKey);
System.out.println("confirmed!");
} catch (ToxException e) {
System.out.println("Unable to confirm friend request");
}
}
}

现在,当我通过另一个客户端发送好友请求时,注册回调后,它会打印“肯定会发生”,这是我引入的调试语句。 CallVoidMetod 调用不会被执行,因为它绝对不会打印我收到了 FriendRequest,也不会打印它已被确认。

对我来说,这意味着回调已成功注册,但由于某种原因,Java 方法从未被调用。

完整代码可在 GitHub 上获取:https://github.com/Tox/jToxcore

最佳答案

我怀疑失败的原因是您将 Java 环境指针存储在名为 data 的对象中。 env 指针无法以这种方式缓存,并且在调用回调时可能无效。有关如何处理此问题的更多信息,请参阅 this question .

关于java - JNI 回调调用失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18899384/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com