Feature: Support remote chat on Android hosts
The chat tools were Windows-only because the Android client had no COMMAND_CHAT handler: the server asked for a chat sub-connection and the command was silently dropped, leaving chat_open to time out. This adds the full path on the client and the matching server gating. Client: COMMAND_CHAT on the main connection spawns a chat thread that opens an authenticated sub-connection, sends TOKEN_CHAT_START and waits for the server to bind it. Incoming GBK lines are decoded and dispatched to ChatSession, which drives an in-app chat activity; when accessibility is available the activity is pulled to the front, otherwise a tappable notification is posted. Outgoing text is strictly GBK encoded so emoji and unmappable characters are rejected instead of becoming '?'. When the server ends the session the activity closes itself, matching the Windows client's DestroyWindow, and a new session starts from an empty log. Server: a new CLIENT_CAP_REMOTE_CHAT bit lets the server tell new APKs from old ones. ResolveHost now accepts Windows and Android hosts and rejects older APKs up front with -32005 rather than waiting for the command to expire. Tool platform masks, instructions, the docs matrix and mcp/tools.json follow the same change. Verified end to end on the Android 15 emulator: chat_open pops the activity, Chinese text from the server renders, a device reply reaches chat_wait, and ending the session from the master closes the activity instead of leaving a dead window. The committed ghost.apk is stale and must be rebuilt before release. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
f33a205fd9
commit
915c587964
14 files changed
+695
-29
No files matched your search
@@ -25,6 +25,7 @@
|
||||
#include "common/rtt_estimator.h"
|
||||
#include "client/IOCPClient.h"
|
||||
#include "ScreenHandler.h"
|
||||
#include "ChatHandler.h"
|
||||
#define XXH_INLINE_ALL
|
||||
#include "common/xxhash.h"
|
||||
#include "common/logger.h"
|
||||
@@ -246,6 +247,9 @@ static jmethodID g_nativeExitMid = nullptr; // CaptureService.onNativeEx
|
||||
static jmethodID g_updateReceivedMid = nullptr; // CaptureService.onUpdateReceived(String) — 升级 APK 已落盘
|
||||
static jmethodID g_clipboardMid = nullptr; // CaptureService.onClipboardText([B)V — 远程剪贴板
|
||||
static jmethodID g_uninstallMid = nullptr; // CaptureService.onUninstallRequested()V — TOKEN_UNINSTALL
|
||||
// ChatSession(远程交谈界面:帧入站文本、发送按钮)
|
||||
static jclass g_chatClass = nullptr;
|
||||
static jmethodID g_chatEventMid = nullptr; // ChatSession.onNativeEvent(I[B)V
|
||||
// ScreenShot(无障碍 takeScreenshot 抓屏,MCP get_screenshot 用)
|
||||
static jclass g_shotClass = nullptr;
|
||||
static jmethodID g_shotCanMid = nullptr; // ScreenShot.canCapture()Z
|
||||
@@ -332,6 +336,50 @@ void DispatchClipboardText(const uint8_t* data, size_t size)
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// 远程交谈:native 子连接 → ChatSession.onNativeEvent(kind, data)。
|
||||
// kind: 1 = 服务端已绑定,2 = 会话结束,3 = 一条 GBK 文本。data 只在 kind=3 时非空。
|
||||
// 与 DispatchClipboardText 同一套 attach/detach 套路,供 AndroidChatHandler::OnReceive 调用。
|
||||
void DispatchChatEvent(int kind, const uint8_t* data, size_t size)
|
||||
{
|
||||
if (!g_jvm || !g_chatClass || !g_chatEventMid) {
|
||||
LOGE("CHAT: JNI not ready (cls=%p method=%p)", g_chatClass, g_chatEventMid);
|
||||
return;
|
||||
}
|
||||
JNIEnv* jenv = nullptr;
|
||||
bool attached = false;
|
||||
jint st = g_jvm->GetEnv((void**)&jenv, JNI_VERSION_1_6);
|
||||
if (st == JNI_EDETACHED) {
|
||||
if (g_jvm->AttachCurrentThread(&jenv, nullptr) != JNI_OK || !jenv) return;
|
||||
attached = true;
|
||||
} else if (st != JNI_OK || !jenv) {
|
||||
return;
|
||||
}
|
||||
jbyteArray arr = nullptr;
|
||||
if (data && size) {
|
||||
arr = jenv->NewByteArray((jsize)size);
|
||||
if (!arr) {
|
||||
if (jenv->ExceptionOccurred()) jenv->ExceptionClear();
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
LOGE("CHAT: NewByteArray(%zu) failed", size);
|
||||
return;
|
||||
}
|
||||
jenv->SetByteArrayRegion(arr, 0, (jsize)size, (const jbyte*)data);
|
||||
if (jenv->ExceptionCheck()) {
|
||||
jenv->ExceptionClear();
|
||||
jenv->DeleteLocalRef(arr);
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
return;
|
||||
}
|
||||
}
|
||||
jenv->CallStaticVoidMethod(g_chatClass, g_chatEventMid, (jint)kind, arr);
|
||||
if (jenv->ExceptionCheck()) {
|
||||
jenv->ExceptionClear();
|
||||
LOGE("CHAT: onNativeEvent(%d) threw", kind);
|
||||
}
|
||||
if (arr) jenv->DeleteLocalRef(arr);
|
||||
if (attached) g_jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
// 锁屏:服务端下发 COMMAND_SCREEN_CTRL_ALT_DEL(Android 无此组合键,映射为
|
||||
// GLOBAL_ACTION_LOCK_SCREEN)。与 DispatchClipboardText 同一套 attach/detach 套路。
|
||||
void DispatchLockScreen()
|
||||
@@ -668,6 +716,71 @@ static void ClientLogThread()
|
||||
LOGI("ClientLogThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 远程交谈
|
||||
// 服务端 COMMAND_CHAT → 开子连接 → 首包 TOKEN_CHAT_START → 服务端绑定后回 [0]。
|
||||
// 同一时刻只允许一个交谈线程:服务端可能重复下发(重连后手动再点一次「远程交谈」),
|
||||
// 第二个子连接会被服务端当重复子连接吸收,这里直接忽略。
|
||||
static std::atomic<bool> g_chatInFlight{false};
|
||||
static std::mutex g_chatMx;
|
||||
static AndroidChatHandler* g_chatHandler = nullptr;
|
||||
|
||||
static void ChatThread()
|
||||
{
|
||||
// detached 线程:异常逃出去会 std::terminate 整个客户端;在途标志也必须无条件还回去,
|
||||
// 否则之后所有 COMMAND_CHAT 都被当成「已有交谈在跑」丢掉
|
||||
struct InFlightGuard {
|
||||
~InFlightGuard() { g_chatInFlight.store(false); }
|
||||
} guard;
|
||||
|
||||
LOGI("ChatThread start → %s:%d", g_serverIp.c_str(), g_serverPort);
|
||||
for (int attempt = 1; attempt <= 3 && S_CLIENT_NORMAL == g_bExit && g_running.load(); ++attempt) {
|
||||
std::unique_ptr<IOCPClient> client;
|
||||
std::unique_ptr<AndroidChatHandler> handler;
|
||||
try {
|
||||
client = std::make_unique<IOCPClient>(g_bExit, true);
|
||||
client->EnableSubConnAuth(true, g_myClientID);
|
||||
// 与屏幕/日志子连接相同:必须继承配置的传输方式,否则 WS/WSS 部署下连不上反代
|
||||
client->SetDefaultProto((int)(unsigned char)g_SETTINGS.protoType);
|
||||
if (!client->ConnectServer(g_serverIp.c_str(), g_serverPort)) {
|
||||
LOGE("ChatThread: connect failed (attempt %d/3)", attempt);
|
||||
Sleep(1000);
|
||||
continue;
|
||||
}
|
||||
// 构造即发 TOKEN_CHAT_START;服务端绑定后回单字节 [0] → Kotlin 弹聊天界面
|
||||
handler = std::make_unique<AndroidChatHandler>(client.get());
|
||||
client->setManagerCallBack(handler.get(),
|
||||
IOCPManager::DataProcess,
|
||||
IOCPManager::ReconnectProcess);
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_chatMx);
|
||||
g_chatHandler = handler.get();
|
||||
}
|
||||
while (client->IsRunning() && client->IsConnected() && S_CLIENT_NORMAL == g_bExit)
|
||||
Sleep(200);
|
||||
{
|
||||
// 先摘掉 Java 可见的指针,再让 handler 析构:nativeChatSend 持同一把锁,
|
||||
// 不会拿到正在销毁的对象
|
||||
std::lock_guard<std::mutex> lk(g_chatMx);
|
||||
g_chatHandler = nullptr;
|
||||
}
|
||||
client->setManagerCallBack(nullptr, nullptr, nullptr);
|
||||
LOGI("ChatThread: sub-conn closed (attempt %d)", attempt);
|
||||
break; // 会话结束/对端断开不重连:服务端会话已随子连接失效,需要新的 chat_open
|
||||
} catch (const std::exception& e) {
|
||||
LOGE("ChatThread exception (attempt %d): %s", attempt, e.what());
|
||||
{
|
||||
std::lock_guard<std::mutex> lk(g_chatMx);
|
||||
g_chatHandler = nullptr;
|
||||
}
|
||||
if (client) client->setManagerCallBack(nullptr, nullptr, nullptr);
|
||||
Sleep(1000);
|
||||
}
|
||||
}
|
||||
// 会话结束(含一直没连上):Kotlin 侧未打开过界面时会直接忽略
|
||||
DispatchChatEvent(2, nullptr, 0);
|
||||
LOGI("ChatThread exit");
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------ 历史活动上报
|
||||
// 主连接一次性快照(与 Windows/Linux 一致,服务端 TOKEN_REPORT_ACTIVITY 分支不 CancelIO)。
|
||||
// 采样机每秒问一次 ControlService 的前台应用;这里只取 Dump(),几十微秒的活。
|
||||
@@ -1057,6 +1170,22 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength)
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_CHAT: {
|
||||
// 远程交谈(主控右键「远程交谈」/ MCP chat_open):子连接 + 应用内聊天界面
|
||||
if (g_chatInFlight.exchange(true)) {
|
||||
LOGI("cmd: COMMAND_CHAT ignored (chat already in flight)");
|
||||
break;
|
||||
}
|
||||
LOGI("cmd: COMMAND_CHAT → sub-conn");
|
||||
try {
|
||||
std::thread(ChatThread).detach();
|
||||
} catch (const std::exception& e) {
|
||||
g_chatInFlight.store(false);
|
||||
LOGE("Chat thread failed: %s", e.what());
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case COMMAND_SCREEN_CONTROL: {
|
||||
if (ulLength < 1 + 48u) { LOGI("SCREEN_CONTROL(main): too short %u", ulLength); break; }
|
||||
const uint8_t* p = szBuffer + 1;
|
||||
@@ -1523,6 +1652,17 @@ Java_com_yama_client_YamaBridge_nativeInit(
|
||||
} else {
|
||||
LOGE("ScreenShot class not found");
|
||||
}
|
||||
// 远程交谈:ChatSession.onNativeEvent(int, byte[]) —— 入站事件与文本
|
||||
jclass chatCls = env->FindClass("com/yama/client/ChatSession");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); chatCls = nullptr; }
|
||||
if (chatCls) {
|
||||
g_chatClass = (jclass)env->NewGlobalRef(chatCls);
|
||||
g_chatEventMid = env->GetStaticMethodID(g_chatClass, "onNativeEvent", "(I[B)V");
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); g_chatEventMid = nullptr; }
|
||||
if (!g_chatEventMid) LOGE("ChatSession.onNativeEvent not found");
|
||||
} else {
|
||||
LOGE("ChatSession class not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1561,6 +1701,28 @@ Java_com_yama_client_YamaBridge_nativeReportMessage(JNIEnv* env, jobject, jstrin
|
||||
env->ReleaseStringUTFChars(msg, c);
|
||||
}
|
||||
|
||||
// 由 ChatSession 在用户点「发送」时调用。入参已是 GBK 字节(Kotlin 侧严格编码,
|
||||
// 表示不了的字符不会落到这里);native 只补结尾 NUL 并写入交谈子连接。
|
||||
JNIEXPORT jboolean JNICALL
|
||||
Java_com_yama_client_YamaBridge_nativeChatSend(JNIEnv* env, jobject, jbyteArray data)
|
||||
{
|
||||
if (!data) return JNI_FALSE;
|
||||
const jsize len = env->GetArrayLength(data);
|
||||
// 与 Windows 客户端输入缓冲一致:4000 GBK 字节上限(4096 缓冲留出 NUL 与余量)
|
||||
if (len <= 0 || len > 4000) return JNI_FALSE;
|
||||
std::vector<uint8_t> buf((size_t)len);
|
||||
env->GetByteArrayRegion(data, 0, len, (jbyte*)buf.data());
|
||||
if (env->ExceptionCheck()) { env->ExceptionClear(); return JNI_FALSE; }
|
||||
std::lock_guard<std::mutex> lk(g_chatMx);
|
||||
if (!g_chatHandler) {
|
||||
LOGI("CHAT: send dropped, no active session");
|
||||
return JNI_FALSE;
|
||||
}
|
||||
const bool ok = g_chatHandler->SendText(buf.data(), buf.size());
|
||||
LOGI("CHAT: send %d bytes -> %d", (int)len, ok ? 1 : 0);
|
||||
return ok ? JNI_TRUE : JNI_FALSE;
|
||||
}
|
||||
|
||||
// 由 CaptureService 在 MediaCodec 配置完成后调用,告知 C++ 侧实际捕获尺寸
|
||||
JNIEXPORT void JNICALL
|
||||
Java_com_yama_client_YamaBridge_nativeSetScreenSize(JNIEnv*, jobject, jint width, jint height)
|
||||
|
||||
Reference in new issue
Block a user