A Go-managed host cannot receive the client upgrade that only the MFC master can push. Transfer moves its main link there and brings it back after N days; MFC has no reason to return it. Transfer is not share: ASSIGN writes the target and drops the current connection, so the old master sees the host go offline and the new master gets a normal main link it can remote control and upgrade. While a transfer is live the client refuses a second transfer and any share, so no host is handed to a third master that expiry would orphan. Server: admin-only `transfer_device` WS command, COMMAND_ASSIGN_MASTER + SHARE_TYPE_YAMA_FOREVER as `host:port:days::transport\0`, byte-compatible with the MFC dialog. The transport segment is always present (YAMA_TRANSFER_TRANSPORT, default 0/TCP) because clients keep their current link when it is missing: a WSS-managed host would otherwise retry WSS against the MFC master's raw TCP port and never connect. The empty shine segment keeps the host's header encryption. The button only renders when `device_list.transfer` is present, which MFC never sends, so no MFC change is needed. devicehistory keeps transfer_to/transfer_until for the card badge and drops the marker on expiry or on a fresh login here. Client: KernelManager.cpp writes the registry and disconnects; ClientDll.cpp re-reads the registry at the top of every reconnect, so a mid-session transfer lands on the next round. Expiry is checked before every connect attempt as well as inside the heartbeat loop, so an unreachable target cannot pin the host to an expired address until a restart; restoring rebuilds the link captured on entry (address, header encryption, transport). linux/main.cpp applies the transfer on the main loop and restores the captured endpoint at expiry; Android does the same; macOS has neither share nor transfer.
173 lines
5.2 KiB
C++
173 lines
5.2 KiB
C++
#pragma once
|
||
|
||
#include "Common.h"
|
||
#include "IOCPClient.h"
|
||
#include <IOSTREAM>
|
||
#include "LoginServer.h"
|
||
#include "KernelManager.h"
|
||
#include <iosfwd>
|
||
#include <vector>
|
||
#include <iostream>
|
||
#include <fstream>
|
||
#include <sstream>
|
||
#include <string>
|
||
#include <shellapi.h>
|
||
#include <corecrt_io.h>
|
||
#include "domain_pool.h"
|
||
#include "ClientApp.h"
|
||
|
||
BOOL IsProcessExit();
|
||
|
||
typedef BOOL(*IsRunning)(void* thisApp);
|
||
|
||
BOOL IsSharedRunning(void* thisApp);
|
||
|
||
BOOL IsClientAppRunning(void* thisApp);
|
||
|
||
DWORD WINAPI StartClientApp(LPVOID param);
|
||
|
||
// 客户端类:将全局变量打包到一起.
|
||
class ClientApp : public App
|
||
{
|
||
public:
|
||
State g_bExit; // 应用程序状态(1-被控端退出 2-主控端退出 3-其他条件)
|
||
BOOL g_bThreadExit; // 工作线程状态
|
||
HINSTANCE g_hInstance; // 进程句柄
|
||
CONNECT_ADDRESS* g_Connection; // 连接信息
|
||
HANDLE g_hEvent; // 全局事件
|
||
BOOL m_bShared; // 是否分享
|
||
int m_sharedKind; // SharedInstanceKind;0=普通/主实例
|
||
IsRunning m_bIsRunning; // 运行状态
|
||
unsigned m_ID; // 唯一标识
|
||
static int m_nCount; // 计数器
|
||
static CLock m_Locker;
|
||
ClientApp(CONNECT_ADDRESS*conn, IsRunning run, BOOL shared=FALSE)
|
||
{
|
||
g_bExit = S_CLIENT_NORMAL;
|
||
g_bThreadExit = FALSE;
|
||
g_hInstance = NULL;
|
||
g_Connection = new CONNECT_ADDRESS(*conn);
|
||
g_hEvent = NULL;
|
||
m_bIsRunning = run;
|
||
m_bShared = shared;
|
||
m_sharedKind = 0;
|
||
m_ID = 0;
|
||
g_bThreadExit = TRUE;
|
||
}
|
||
std::vector<std::string> GetSharedMasterList()
|
||
{
|
||
DomainPool pool = g_Connection->ServerIP();
|
||
auto list = pool.GetIPList();
|
||
return list;
|
||
}
|
||
~ClientApp()
|
||
{
|
||
SAFE_DELETE(g_Connection);
|
||
}
|
||
bool IsShared() override
|
||
{
|
||
return m_bShared != FALSE;
|
||
}
|
||
ClientApp* SetID(unsigned id)
|
||
{
|
||
m_ID = id;
|
||
return this;
|
||
}
|
||
static void AddCount(int n=1)
|
||
{
|
||
m_Locker.Lock();
|
||
m_nCount+=n;
|
||
m_Locker.Unlock();
|
||
}
|
||
static int GetCount()
|
||
{
|
||
m_Locker.Lock();
|
||
int n = m_nCount;
|
||
m_Locker.Unlock();
|
||
return n;
|
||
}
|
||
static void Wait()
|
||
{
|
||
while (GetCount())
|
||
Sleep(50);
|
||
}
|
||
bool IsThreadRun()
|
||
{
|
||
m_Locker.Lock();
|
||
BOOL n = g_bThreadExit;
|
||
m_Locker.Unlock();
|
||
return FALSE == n;
|
||
}
|
||
void SetThreadRun(BOOL run = TRUE)
|
||
{
|
||
m_Locker.Lock();
|
||
g_bThreadExit = !run;
|
||
m_Locker.Unlock();
|
||
}
|
||
void SetProcessState(State state = S_CLIENT_NORMAL)
|
||
{
|
||
m_Locker.Lock();
|
||
g_bExit = state;
|
||
m_Locker.Unlock();
|
||
}
|
||
virtual bool Initialize() override
|
||
{
|
||
g_Connection->SetType(CLIENT_TYPE_ONE);
|
||
return true;
|
||
}
|
||
virtual bool Start() override
|
||
{
|
||
StartClientApp(this);
|
||
return true;
|
||
}
|
||
virtual bool Stop() override
|
||
{
|
||
g_bExit = S_CLIENT_EXIT;
|
||
return true;
|
||
}
|
||
bool Run()
|
||
{
|
||
if (!Initialize()) return false;
|
||
if (!Start()) return false;
|
||
if (!Stop()) return false;
|
||
return true;
|
||
}
|
||
virtual bool IsMainInstance()override {
|
||
return !m_bShared;
|
||
}
|
||
};
|
||
|
||
// protoType:本实例要用的传输方式(PROTO_TCP / PROTO_WS / PROTO_WSS),-1 = 沿用出包时
|
||
// 烘焙的值。分享链路必须显式给出——它连的是另一台服务端,那边的入口未必和主控同构
|
||
// (主控经 nginx 走 WSS、分享目标直连设备端口时就得是 TCP),所以不能一律照抄主控。
|
||
ClientApp* NewClientStartArg(const char* remoteAddr, IsRunning run = IsClientAppRunning, BOOL shared=FALSE, int headerEnc = -1, int protoType = -1);
|
||
|
||
// 分享实例的种类。「转移主机」不再起分享实例(它直接让主连接搬到新主控),
|
||
// 所以现在只有一种;保留枚举是为了让 StartSharedClient / StopSharedInstances
|
||
// 的签名在后续再引入实例种类时不用改调用点。
|
||
enum SharedInstanceKind {
|
||
SHARED_KIND_SHARE = 1,
|
||
};
|
||
|
||
// 启动一个分享实例线程并在进程内登记,targetKey 为目标地址 "ip:port"。
|
||
// 取消分享需要能立刻找到并停掉这些线程,所以不能再像普通线程那样起完就把句柄丢掉。
|
||
BOOL StartSharedClient(ClientApp* app, SharedInstanceKind kind, const std::string& targetKey);
|
||
|
||
// 停止指定种类的分享实例:逐个置 S_CLIENT_EXIT,心跳/重连循环会在 1 秒内退出。
|
||
// 返回发出退出信号的实例数。
|
||
int StopSharedInstances(SharedInstanceKind kind);
|
||
|
||
// 停掉指向 targetKey 的全部分享/转移实例,并等待(最多约 1.5 秒)线程注销。
|
||
// 同一目标换传输方式重新分享、或长期分享接管转移临时连接时,必须先收掉旧连接,
|
||
// 否则同一 clientID 会有两条连接互相踢。返回发出退出信号的实例数。
|
||
int StopSharedInstancesForTarget(const std::string& targetKey);
|
||
|
||
// targetKey 上是否有指定种类的实例正在运行。
|
||
bool HasSharedInstanceForTarget(const std::string& targetKey, SharedInstanceKind kind);
|
||
|
||
// 启动核心线程,参数为:ClientApp
|
||
DWORD WINAPI StartClient(LPVOID lParam);
|
||
|
||
// 启动核心线程,参数为:ClientApp
|
||
DWORD WINAPI StartClientApp(LPVOID param);
|