JNI回调无法调用

时间:2013-09-19 15:49:57

标签: java callback java-native-interface

我正在为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方法只调用以下本机代码:


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指针保存到messenger结构中。最后,它调用Tox-API提供的回调方法,并注册回调static void callback_friendrequest

在opaque 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

1 个答案:

答案 0 :(得分:1)

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