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
67 lines
2.8 KiB
C++
67 lines
2.8 KiB
C++
#pragma once
|
||
// Android 远程交谈(COMMAND_CHAT)子连接。
|
||
//
|
||
// 协议与 Windows 客户端完全一致(见 client/ChatManager.cpp、server/2015Remote/McpChat.cpp):
|
||
// 主连接 [COMMAND_CHAT] → 本线程开子连接(ConnAuth)→ 首包单字节 [TOKEN_CHAT_START]
|
||
// → 服务端绑定后回单字节 [COMMAND_NEXT_CHAT](0)→ 弹出聊天界面
|
||
// → 双向文本 [GBK 字节][NUL];服务端单字节 [COMMAND_CHAT_CLOSE](1)结束会话。
|
||
// 单字节包都是控制/心跳,不是文本(客户端发文本至少带尾部 NUL)。
|
||
#include "common/commands.h"
|
||
#include "client/IOCPClient.h"
|
||
#include <vector>
|
||
#include <cstring>
|
||
|
||
// 定义在 main.cpp:把会话事件交给 Kotlin(ChatSession.onNativeEvent)。
|
||
// kind: 1 = 服务端已绑定(可以显示聊天界面),2 = 会话结束,3 = 一条对方文本
|
||
// (data 为 GBK 字节,不含结尾 NUL)。必须从非 UI 线程调用,Kotlin 侧自己 post 到主线程。
|
||
extern void DispatchChatEvent(int kind, const uint8_t* data, size_t size);
|
||
|
||
class AndroidChatHandler : public IOCPManager {
|
||
public:
|
||
explicit AndroidChatHandler(IOCPClient* client) : m_client(client) {
|
||
BYTE bToken = TOKEN_CHAT_START;
|
||
m_client->Send2Server((char*)&bToken, 1);
|
||
}
|
||
|
||
// Java 侧点「发送」:GBK 字节 + 结尾 NUL(与 Windows 客户端 Send() 的载荷一致)。
|
||
bool SendText(const uint8_t* gbk, size_t size) {
|
||
if (!m_client || !m_client->IsConnected() || !gbk || size == 0) return false;
|
||
std::vector<char> pkt(size + 1);
|
||
memcpy(pkt.data(), gbk, size);
|
||
pkt[size] = '\0';
|
||
return m_client->Send2Server(pkt.data(), (ULONG)pkt.size()) != FALSE;
|
||
}
|
||
|
||
virtual VOID OnReceive(PBYTE data, ULONG size) override {
|
||
if (!data || !size) return;
|
||
|
||
if (size == 1) {
|
||
switch (data[0]) {
|
||
case COMMAND_NEXT_CHAT:
|
||
DispatchChatEvent(1, nullptr, 0);
|
||
return;
|
||
case COMMAND_CHAT_CLOSE:
|
||
DispatchChatEvent(2, nullptr, 0);
|
||
return;
|
||
case COMMAND_CHAT_SCREEN_LOCK:
|
||
case COMMAND_CHAT_SCREEN_UNLOCK:
|
||
// 服务端手动交谈时的「锁定屏幕/屏蔽功能键」在 Android 上没有对应语义
|
||
// (Windows 是 ClipCursor + 隐藏任务栏)。忽略,不锁设备。
|
||
return;
|
||
default:
|
||
// 单字节心跳等:聊天通道不承载,丢弃
|
||
break;
|
||
}
|
||
}
|
||
|
||
// GBK 文本,NUL 结尾;上限与 Windows 输入缓冲一致,防异常对端撑爆内存
|
||
const size_t kMaxIncoming = 8u * 1024u;
|
||
size_t n = 0;
|
||
while (n < size && n < kMaxIncoming && data[n] != 0) ++n;
|
||
if (n) DispatchChatEvent(3, data, n);
|
||
}
|
||
|
||
private:
|
||
IOCPClient* m_client;
|
||
};
|