Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove TTInstance from JNI API #2157

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
533 changes: 214 additions & 319 deletions Library/TeamTalkJNI/jni/teamtalk-jni.cpp

Large diffs are not rendered by default.

116 changes: 70 additions & 46 deletions Library/TeamTalkJNI/jni/teamtalksrv-jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,46 @@

#include "ttconvert-jni.h"

#include <string.h>
#include <assert.h>
#include <iostream>
#include <vector>
#include <map>
#include <mutex>

#include <TeamTalkSrv.h>

using namespace std;


typedef map<TTSInstance*, JNIEnv*> jenv_t;
typedef map<TTSInstance*, jobject> refs_t;
typedef std::map<TTSInstance*, JNIEnv*> jenv_t;
typedef std::map<TTSInstance*, jobject> refs_t;
jenv_t envs;
refs_t command_refs, logger_refs;

std::mutex ttsinstmutex;
std::map<jint, TTSInstance*> ttsinstances;

void AddTTSInstance(JNIEnv* env, jobject thiz, TTSInstance* ttsinst)
{
auto hash = hashCode(env, thiz);

std::lock_guard<std::mutex> g(ttsinstmutex);
ttsinstances[hash] = ttsinst;
}

TTSInstance* RemoveTTSInstance(JNIEnv* env, jobject thiz)
{
auto hash = hashCode(env, thiz);

std::lock_guard<std::mutex> g(ttsinstmutex);
TTSInstance* ttsinst = ttsinstances[hash];
ttsinstances.erase(hash);
return ttsinst;
}

TTSInstance* GetTTSInstance(JNIEnv* env, jobject thiz)
{
auto hash = hashCode(env, thiz);
std::lock_guard<std::mutex> g(ttsinstmutex);
return ttsinstances[hash];
}


extern "C" {

/* Client command callbacks */
Expand Down Expand Up @@ -747,15 +771,15 @@ extern "C" {
(JNIEnv *env, jobject thiz) {

TTSInstance* ttsInst = TTS_InitTeamTalk();

AddTTSInstance(env, thiz, ttsInst);
envs[ttsInst] = env;

return jlong(ttsInst);
}

JNIEXPORT void JNICALL Java_dk_bearware_TeamTalkSrv_closeTeamTalk
(JNIEnv *env, jobject thiz, jlong lpTTSInstance) {
TTSInstance* inst = reinterpret_cast<TTSInstance*>(lpTTSInstance);
(JNIEnv *env, jobject thiz) {
TTSInstance* inst = RemoveTTSInstance(env, thiz);

TTS_CloseTeamTalk(inst);

Expand All @@ -770,9 +794,9 @@ extern "C" {
}

JNIEXPORT void JNICALL Java_dk_bearware_TeamTalkSrv_registerServerCallback
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject servercallback) {
(JNIEnv *env, jobject thiz, jobject servercallback) {

TTSInstance* inst = reinterpret_cast<TTSInstance*>(lpTTSInstance);
TTSInstance* inst = GetTTSInstance(env, thiz);

command_refs[inst] = env->NewGlobalRef(servercallback);

Expand All @@ -795,9 +819,9 @@ extern "C" {
}

JNIEXPORT void JNICALL Java_dk_bearware_TeamTalkSrv_registerServerLogger
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject serverlogger) {
(JNIEnv *env, jobject thiz, jobject serverlogger) {

TTSInstance* inst = reinterpret_cast<TTSInstance*>(lpTTSInstance);
TTSInstance* inst = GetTTSInstance(env, thiz);

logger_refs[inst] = env->NewGlobalRef(serverlogger);

Expand Down Expand Up @@ -825,129 +849,129 @@ extern "C" {
}

JNIEXPORT jboolean JNICALL Java_dk_bearware_TeamTalkSrv_setEncryptionContext
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jstring szCertificateFile, jstring szPrivateKeyFile) {
return TTS_SetEncryptionContext(reinterpret_cast<TTSInstance*>(lpTTSInstance), ttstr(env, szCertificateFile),
(JNIEnv *env, jobject thiz, jstring szCertificateFile, jstring szPrivateKeyFile) {
return TTS_SetEncryptionContext(GetTTSInstance(env, thiz), ttstr(env, szCertificateFile),
ttstr(env, szPrivateKeyFile));
}

JNIEXPORT jboolean JNICALL Java_dk_bearware_TeamTalkSrv_setEncryptionContextEx
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject lpEncryptionContext) {
(JNIEnv *env, jobject thiz, jobject lpEncryptionContext) {

THROW_NULLEX(env, lpEncryptionContext, false);

EncryptionContext context = {};
setEncryptionContext(env, context, lpEncryptionContext, J2N);

return TTS_SetEncryptionContextEx(reinterpret_cast<TTSInstance*>(lpTTSInstance),
return TTS_SetEncryptionContextEx(GetTTSInstance(env, thiz),
&context);
}

JNIEXPORT jboolean JNICALL Java_dk_bearware_TeamTalkSrv_runEventLoop
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jint pnWaitMs) {
(JNIEnv *env, jobject thiz, jint pnWaitMs) {

envs[reinterpret_cast<TTSInstance*>(lpTTSInstance)] = env;
envs[GetTTSInstance(env, thiz)] = env;

INT32 _pnWaitMs = pnWaitMs;

return TTS_RunEventLoop(reinterpret_cast<TTSInstance*>(lpTTSInstance), &_pnWaitMs);
return TTS_RunEventLoop(GetTTSInstance(env, thiz), &_pnWaitMs);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_setChannelFilesRoot
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jstring szFilesRoot,
(JNIEnv *env, jobject thiz, jstring szFilesRoot,
jlong nMaxDiskUsage, jlong nDefaultChannelQuota) {

return TTS_SetChannelFilesRoot(reinterpret_cast<TTSInstance*>(lpTTSInstance),
return TTS_SetChannelFilesRoot(GetTTSInstance(env, thiz),
ttstr(env, szFilesRoot), nMaxDiskUsage, nDefaultChannelQuota);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_updateServer
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject lpServerProperties) {
(JNIEnv *env, jobject thiz, jobject lpServerProperties) {

ServerProperties srvprop;
setServerProperties(env, srvprop, lpServerProperties, J2N);
return TTS_UpdateServer(reinterpret_cast<TTSInstance*>(lpTTSInstance), &srvprop);
return TTS_UpdateServer(GetTTSInstance(env, thiz), &srvprop);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_makeChannel
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject lpChannel) {
(JNIEnv *env, jobject thiz, jobject lpChannel) {

Channel chan;
setChannel(env, chan, lpChannel, J2N);

return TTS_MakeChannel(reinterpret_cast<TTSInstance*>(lpTTSInstance), &chan);
return TTS_MakeChannel(GetTTSInstance(env, thiz), &chan);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_updateChannel
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject lpChannel) {
(JNIEnv *env, jobject thiz, jobject lpChannel) {

Channel chan;
setChannel(env, chan, lpChannel, J2N);

return TTS_UpdateChannel(reinterpret_cast<TTSInstance*>(lpTTSInstance), &chan);
return TTS_UpdateChannel(GetTTSInstance(env, thiz), &chan);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_removeChannel
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jint nChannelID) {
return TTS_RemoveChannel(reinterpret_cast<TTSInstance*>(lpTTSInstance), nChannelID);
(JNIEnv *env, jobject thiz, jint nChannelID) {
return TTS_RemoveChannel(GetTTSInstance(env, thiz), nChannelID);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_addFileToChannel
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jstring szLocalFilePath, jobject lpRemoteFile) {
(JNIEnv *env, jobject thiz, jstring szLocalFilePath, jobject lpRemoteFile) {
RemoteFile rmfile;
setRemoteFile(env, rmfile, lpRemoteFile, J2N);

return TTS_AddFileToChannel(reinterpret_cast<TTSInstance*>(lpTTSInstance),
return TTS_AddFileToChannel(GetTTSInstance(env, thiz),
ttstr(env, szLocalFilePath), &rmfile);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_removeFileFromChannel
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject lpRemoteFile) {
(JNIEnv *env, jobject thiz, jobject lpRemoteFile) {

RemoteFile rmfile;
setRemoteFile(env, rmfile, lpRemoteFile, J2N);

return TTS_RemoveFileFromChannel(reinterpret_cast<TTSInstance*>(lpTTSInstance),
return TTS_RemoveFileFromChannel(GetTTSInstance(env, thiz),
&rmfile);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_sendTextMessage
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jobject lpTextMessage) {
(JNIEnv *env, jobject thiz, jobject lpTextMessage) {

TextMessage msg;
setTextMessage(env, msg, lpTextMessage, J2N);

return TTS_SendTextMessage(reinterpret_cast<TTSInstance*>(lpTTSInstance),
return TTS_SendTextMessage(GetTTSInstance(env, thiz),
&msg);
}

JNIEXPORT jint JNICALL Java_dk_bearware_TeamTalkSrv_moveUser
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jint nUserID, jobject lpChannel) {
(JNIEnv *env, jobject thiz, jint nUserID, jobject lpChannel) {

Channel chan;
setChannel(env, chan, lpChannel, J2N);
return TTS_MoveUser(reinterpret_cast<TTSInstance*>(lpTTSInstance), nUserID, &chan);
return TTS_MoveUser(GetTTSInstance(env, thiz), nUserID, &chan);
}

JNIEXPORT jboolean JNICALL Java_dk_bearware_TeamTalkSrv_startServer
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jstring szBindIPAddr,
(JNIEnv *env, jobject thiz, jstring szBindIPAddr,
jint nTcpPort, jint nUdpPort, jboolean bEncrypted) {
return TTS_StartServer(reinterpret_cast<TTSInstance*>(lpTTSInstance),
return TTS_StartServer(GetTTSInstance(env, thiz),
ttstr(env, szBindIPAddr),
UINT16(nTcpPort), UINT16(nUdpPort), bEncrypted);
}

JNIEXPORT jboolean JNICALL Java_dk_bearware_TeamTalkSrv_startServerSysID
(JNIEnv *env, jobject thiz, jlong lpTTSInstance, jstring szBindIPAddr,
(JNIEnv *env, jobject thiz, jstring szBindIPAddr,
jint nTcpPort, jint nUdpPort, jboolean bEncrypted, jstring szSystemID) {
return TTS_StartServerSysID(reinterpret_cast<TTSInstance*>(lpTTSInstance),
return TTS_StartServerSysID(GetTTSInstance(env, thiz),
ttstr(env, szBindIPAddr),
UINT16(nTcpPort), UINT16(nUdpPort), bEncrypted,
ttstr(env, szSystemID));
}

JNIEXPORT jboolean JNICALL Java_dk_bearware_TeamTalkSrv_stopServer
(JNIEnv *env, jobject thiz, jlong lpTTSInstance) {
return TTS_StopServer(reinterpret_cast<TTSInstance*>(lpTTSInstance));
(JNIEnv *env, jobject thiz) {
return TTS_StopServer(GetTTSInstance(env, thiz));
}

}
8 changes: 8 additions & 0 deletions Library/TeamTalkJNI/jni/ttconvert-jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ const INT32* TO_INT32_ARRAY(const jint* jints, INT32* ttints, jsize N)
return ttints;
}

jint hashCode(JNIEnv* env, jobject thiz)
{
jclass cls = env->GetObjectClass(thiz);
jmethodID hashCodeMethod = env->GetMethodID(cls, "hashCode", "()I");
jint hash = env->CallIntMethod(thiz, hashCodeMethod);
return hash;
}

jobject newObject(JNIEnv* env, jclass cls_obj)
{
jmethodID midInit = env->GetMethodID(cls_obj, "<init>", "()V");
Expand Down
1 change: 1 addition & 0 deletions Library/TeamTalkJNI/jni/ttconvert-jni.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ enum JConvert
J2N = 2
};

jint hashCode(JNIEnv* env, jobject thiz);
jobject newObject(JNIEnv* env, jclass cls_obj);
jobject newSoundDevice(JNIEnv* env, const SoundDevice& dev);
jobject newVideoDevice(JNIEnv* env, VideoCaptureDevice& dev);
Expand Down
Loading
Loading