我目前正在为 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();
}
}
公共 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
}
这段代码相当复杂,但它的作用如下:首先,它检查是否已经为此操作设置了回调,如果是,则将其删除。然后它为它接收到的回调对象创建一个新的全局引用,并将该引用以及 JNIEnv 指针保存到 messenger 结构。最后调用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