#pragma once #include "Common.h" #include "IOCPClient.h" #include #include "LoginServer.h" #include "KernelManager.h" #include #include #include #include #include #include #include #include #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 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);