Pārlūkot izejas kodu

Z991239-6440 #comment feat: 新增连接虚机主备模式方法

Signed-Off-By: commit-hook
刘文涛80174520 1 mēnesi atpakaļ
vecāks
revīzija
87bc1237df

+ 4 - 0
Framework/Common/SpSecureClient.h

@@ -39,6 +39,8 @@ public:
 
 	// 使用集中配置项连接
 	bool ConnectFromCentralSetting();
+
+	bool ConnectFromAddress(CSimpleStringA mainServ, CAutoArray<CSimpleStringA> backupServArr, CSimpleStringA& connectedServ);
 	
 	bool IsConnectionOK();
 	bool IsSecureConn();	
@@ -83,6 +85,8 @@ private:
 	bool Connect_Single(ENTITY_CONNECT_INFO *connectInfo, int option);
 	//支持双活
 	bool Connect_Dual(ENTITY_CONNECT_INFO *connectInfo, int option);
+	
+	bool Connect_Standby(CSimpleStringA mainServ, CAutoArray<CSimpleStringA> backupServArr, int option, CSimpleStringA &connectedServ);
 	void OnReConnect();
 	void DbgWithLink_DualActive(const char* formatStr, ...);
 protected:

+ 83 - 1
Framework/spbase/SpSecureClient.cpp

@@ -300,6 +300,29 @@ bool SpSecureClient::ConnectFromCentralSetting()
 		return Connect_Single(&connectInfo, nOption);
 }
 
+
+
+bool SpSecureClient::ConnectFromAddress(CSimpleStringA mainServ, CAutoArray<CSimpleStringA> backupServArr, CSimpleStringA& connectedServ)
+{
+#ifdef RVC_OS_WIN
+	SetthreadGroup(GetCurrentThreadId(), m_pEntity->GetEntityName());
+#endif // RVC_OS_WIN
+
+	if (IsConnectionOK()) {
+		connectedServ = "";
+		return true;
+	}
+	int nOption = 3;
+	const char* pEntityName = m_pEntity->GetEntityName();
+	CSimpleStringA strVal;
+	if (Error_Succeed == m_pEntity->GetFunction()->GetSysVar("EntryPermit", strVal)) {
+		if (strVal == "L" && stricmp(pEntityName, "AccessAuthorization") != 0)
+			nOption = 2;	// 优先使用缓存密钥
+	}
+	bool connSucc = Connect_Standby(mainServ, backupServArr, nOption, connectedServ);
+	return connSucc;
+}
+
 int SpSecureClient::getCurrentLink()
 {
 	if (!IsConnectionOK())
@@ -439,6 +462,65 @@ bool SpSecureClient::Connect_Dual(ENTITY_CONNECT_INFO *connectInfo, int option)
 
 }
 
+bool SpSecureClient::Connect_Standby(CSimpleStringA mainServ, CAutoArray<CSimpleStringA> backupServArr, int option, CSimpleStringA &connectedServ)
+{
+	const char* pEntityName = m_pEntity->GetEntityName();
+	ENTITY_CONNECT_INFO connectInfo;
+
+	DbgWithLink_DualActive("mod %s using option %d", pEntityName, option);
+
+	ErrorCodeEnum configRc = Error_Bug;
+
+	//主用地址
+	CSimpleStringA strServer1 = "";
+	int nServer1Port = 0;
+	if (mainServ.IsNullOrEmpty())
+	{
+		DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read Main::Server config fail, %s", pEntityName);	
+	}
+	else {
+		if (Error_Succeed != (configRc = ParseIPAddress(mainServ.GetData(), strServer1, nServer1Port)))
+		{
+			DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("parse Main ip addr fail: %s, %d", mainServ.GetData(), configRc);
+		}
+		else {
+			if (Connect(strServer1, nServer1Port, option)) {
+				connectInfo.setParam(pEntityName, strServer1.GetData(), nServer1Port, "", 0, -1, 0);//主机连接上
+				setConnectInfo(&connectInfo);
+				DbgWithLink_DualActive("Standby Connection: EntityName:%s, server:%s %d, serverBackup:%s %d, DualOpen: %d, currentLink:%d", connectInfo.m_EntityName, connectInfo.m_ServerIP,
+					connectInfo.m_ServerPort, connectInfo.m_Server_BackupIP, connectInfo.m_Server_BackupPort, connectInfo.m_DualActive, connectInfo.m_currentLink);
+				connectedServ = mainServ;
+				return true;
+			}
+		}
+	}
+
+	//备用地址
+	CSimpleStringA strServer2 = "";
+	int nServer2Port = 0;
+	for (int i = 0; i < backupServArr.GetCount(); i++)
+	{
+		CSimpleStringA backupSerStr = backupServArr[i];
+		if (!backupSerStr.IsNullOrEmpty()) {
+			if (Error_Succeed != (configRc = ParseIPAddress(backupSerStr.GetData(), strServer2, nServer2Port)))
+			{
+				DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("parse backup ip addr fail: %s, %d", backupSerStr.GetData(), configRc);
+			}
+			else {
+				if (Connect(strServer2, nServer2Port, option)) {
+					connectInfo.setParam(pEntityName, strServer1.GetData(), nServer1Port, strServer2.GetData(), nServer2Port, -1, 1);//备机连接上
+					setConnectInfo(&connectInfo);
+					DbgWithLink_DualActive("Standby Connection: EntityName:%s, server:%s %d, serverBackup:%s %d, DualOpen: %d, currentLink:%d", connectInfo.m_EntityName, connectInfo.m_ServerIP,
+						connectInfo.m_ServerPort, connectInfo.m_Server_BackupIP, connectInfo.m_Server_BackupPort, connectInfo.m_DualActive, connectInfo.m_currentLink);
+					connectedServ = backupSerStr;
+					return true;
+				}
+			}
+		}
+	}
+	return false;
+}
+
 bool SpSecureClient::IsConnectionOK()
 {
 	return GetFunction()->IsConnectionOK();
@@ -535,7 +617,7 @@ struct OnDisconnectTask : public ITaskSp
 
 		m_pSecureClient->OnDisconnect();
 		m_pSecureClient->DecRefCount();
-		m_pSecureClient->OnReConnect();
+		//m_pSecureClient->OnReConnect();
 	}
 };