Skip to content

Commit 3b72e8a

Browse files
committed
added:download connect limit
1 parent 51a3b05 commit 3b72e8a

File tree

10 files changed

+110
-11
lines changed

10 files changed

+110
-11
lines changed

XEngine_Source/StorageModule_Config/Config_Define.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ typedef struct tag_XEngine_ServerConfig
8484
BOOL bLimitMode;
8585
__int64x nMaxUPLoader;
8686
__int64x nMaxDNLoader;
87+
int nMaxUPConnect;
88+
int nMaxDNConnect;
8789
}st_XLimit;
8890
struct
8991
{

XEngine_Source/StorageModule_Config/Config_Json/Config_Json.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ BOOL CConfig_Json::Config_Json_File(LPCTSTR lpszConfigFile, XENGINE_SERVERCONFIG
169169
_tcscpy(pSt_ServerConfig->st_XProxy.st_XProxyPass.tszDLPass, st_JsonXProxyPass["tszDLPass"].asCString());
170170
_tcscpy(pSt_ServerConfig->st_XProxy.st_XProxyPass.tszUPPass, st_JsonXProxyPass["tszUPPass"].asCString());
171171

172-
if (st_JsonRoot["XLimit"].empty() || (3 != st_JsonRoot["XLimit"].size()))
172+
if (st_JsonRoot["XLimit"].empty() || (5 != st_JsonRoot["XLimit"].size()))
173173
{
174174
Config_IsErrorOccur = TRUE;
175175
Config_dwErrorCode = ERROR_XENGINE_BLOGIC_CONFIG_JSON_XSTORAGE;
@@ -179,6 +179,8 @@ BOOL CConfig_Json::Config_Json_File(LPCTSTR lpszConfigFile, XENGINE_SERVERCONFIG
179179
pSt_ServerConfig->st_XLimit.bLimitMode = st_JsonXLimit["bLimitMode"].asBool();
180180
pSt_ServerConfig->st_XLimit.nMaxDNLoader = st_JsonXLimit["nMaxDNLoad"].asInt64();
181181
pSt_ServerConfig->st_XLimit.nMaxUPLoader = st_JsonXLimit["nMaxUPLoad"].asInt64();
182+
pSt_ServerConfig->st_XLimit.nMaxUPConnect = st_JsonXLimit["nMaxUPConnect"].asInt();
183+
pSt_ServerConfig->st_XLimit.nMaxDNConnect = st_JsonXLimit["nMaxDNConnect"].asInt();
182184

183185
if (st_JsonRoot["XP2xp"].empty() || (3 != st_JsonRoot["XP2xp"].size()))
184186
{

XEngine_Source/StorageModule_Session/Session_Define.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ extern "C" BOOL Session_User_Exist(LPCTSTR lpszUser, LPCTSTR lpszPass, int* pInt
9494
意思:是否成功
9595
备注:
9696
*********************************************************************/
97-
extern "C" BOOL Session_DLStroage_Init();
97+
extern "C" BOOL Session_DLStroage_Init(int nMaxConnect);
9898
/********************************************************************
9999
函数名称:Session_DLStroage_Destory
100100
函数功能:销毁下载管理器
@@ -267,6 +267,20 @@ extern "C" BOOL Session_DLStorage_GetAll(SESSION_STORAGEINFO*** pppSt_StorageInf
267267
*********************************************************************/
268268
extern "C" BOOL Session_DLStroage_Delete(LPCTSTR lpszClientAddr);
269269
/********************************************************************
270+
函数名称:Session_DLStroage_MaxConnect
271+
函数功能:判断一个地址是否超过连接数限制
272+
参数.一:lpszClientAddr
273+
In/Out:In
274+
类型:常量字符指针
275+
可空:N
276+
意思:输入要处理的地址
277+
返回值
278+
类型:逻辑型
279+
意思:是否成功
280+
备注:
281+
*********************************************************************/
282+
extern "C" BOOL Session_DLStroage_MaxConnect(LPCTSTR lpszClientAddr);
283+
/********************************************************************
270284
函数名称:Session_UPStroage_Init
271285
函数功能:初始化上传会话管理器
272286
参数.一:bUPResume

XEngine_Source/StorageModule_Session/Session_Error.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
#define ERROR_STORAGE_MODULE_SESSION_OPENFILE 0x0010003
1919
#define ERROR_STORAGE_MODULE_SESSION_NOTFOUND 0x0010004
2020
#define ERROR_STORAGE_MODULE_SESSION_PASSWORD 0x0010005
21-
#define ERROR_STORAGE_MODULE_SESSION_EMPTY 0x0010006
21+
#define ERROR_STORAGE_MODULE_SESSION_EMPTY 0x0010006
22+
#define ERROR_STORAGE_MODULE_SESSION_MAXCONNECT 0x0010007

XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.cpp

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,26 @@ CSession_DLStroage::~CSession_DLStroage()
2323
/********************************************************************
2424
函数名称:Session_DLStroage_Init
2525
函数功能:初始化下载会话管理器
26+
参数.一:nMaxConnect
27+
In/Out:
28+
类型:
29+
可空:
30+
意思:
31+
参数.二:
32+
In/Out:
33+
类型:
34+
可空:
35+
意思:
2636
返回值
2737
类型:逻辑型
2838
意思:是否成功
2939
备注:
3040
*********************************************************************/
31-
BOOL CSession_DLStroage::Session_DLStroage_Init()
41+
BOOL CSession_DLStroage::Session_DLStroage_Init(int nMaxConnect)
3242
{
3343
Session_IsErrorOccur = FALSE;
3444

45+
m_nMaxConnect = nMaxConnect;
3546
return TRUE;
3647
}
3748
/********************************************************************
@@ -432,4 +443,53 @@ BOOL CSession_DLStroage::Session_DLStroage_Delete(LPCTSTR lpszClientAddr)
432443
}
433444
st_Locker.unlock_shared();
434445
return TRUE;
446+
}
447+
/********************************************************************
448+
函数名称:Session_DLStroage_MaxConnect
449+
函数功能:判断一个地址是否超过连接数限制
450+
参数.一:lpszClientAddr
451+
In/Out:In
452+
类型:常量字符指针
453+
可空:N
454+
意思:输入要处理的地址
455+
返回值
456+
类型:逻辑型
457+
意思:是否成功
458+
备注:
459+
*********************************************************************/
460+
BOOL CSession_DLStroage::Session_DLStroage_MaxConnect(LPCTSTR lpszClientAddr)
461+
{
462+
Session_IsErrorOccur = FALSE;
463+
464+
int nExistNumber = 0;
465+
st_Locker.lock_shared();
466+
unordered_map<string, SESSION_STORAGEINFO>::iterator stl_MapIterator = stl_MapStroage.begin();
467+
for (; stl_MapIterator != stl_MapStroage.end(); stl_MapIterator++)
468+
{
469+
TCHAR tszIPSource[128];
470+
TCHAR tszIPDest[128];
471+
472+
memset(tszIPSource, '\0', sizeof(tszIPSource));
473+
memset(tszIPDest, '\0', sizeof(tszIPDest));
474+
475+
_tcscpy(tszIPSource, stl_MapIterator->first.c_str());
476+
_tcscpy(tszIPDest, lpszClientAddr);
477+
478+
BaseLib_OperatorIPAddr_SegAddr(tszIPSource);
479+
BaseLib_OperatorIPAddr_SegAddr(tszIPDest);
480+
481+
if (0 == _tcscmp(tszIPSource, tszIPDest))
482+
{
483+
nExistNumber++;
484+
}
485+
}
486+
st_Locker.unlock_shared();
487+
488+
if (nExistNumber > m_nMaxConnect)
489+
{
490+
Session_IsErrorOccur = TRUE;
491+
Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_MAXCONNECT;
492+
return FALSE;
493+
}
494+
return TRUE;
435495
}

XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class CSession_DLStroage
1717
CSession_DLStroage();
1818
~CSession_DLStroage();
1919
public:
20-
BOOL Session_DLStroage_Init();
20+
BOOL Session_DLStroage_Init(int nMaxConnect);
2121
BOOL Session_DLStroage_Destory();
2222
BOOL Session_DLStroage_Insert(LPCTSTR lpszClientAddr, LPCTSTR lpszBuckKey, LPCTSTR lpszFileDir, __int64x* pInt_Count, __int64x* pInt_LeftCount, int nPosStart = 0, int nPostEnd = 0, LPCTSTR lpszFileHash = NULL, int nLimit = 0, XHANDLE xhToken = NULL);
2323
BOOL Session_DLStroage_GetBuffer(LPCTSTR lpszClientAddr, TCHAR* ptszMsgBuffer, int* pInt_MsgLen);
@@ -26,8 +26,9 @@ class CSession_DLStroage
2626
BOOL Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek);
2727
BOOL Session_DLStorage_GetAll(SESSION_STORAGEINFO*** pppSt_StorageInfo, int* pInt_ListCount);
2828
BOOL Session_DLStroage_Delete(LPCTSTR lpszClientAddr);
29+
BOOL Session_DLStroage_MaxConnect(LPCTSTR lpszClientAddr);
2930
private:
30-
int m_nTryTime;
31+
int m_nMaxConnect;
3132
shared_mutex st_Locker;
3233
private:
3334
unordered_map<string, SESSION_STORAGEINFO> stl_MapStroage;

XEngine_Source/StorageModule_Session/StorageModule_Session.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ EXPORTS
1616
Session_DLStorage_SetSeek
1717
Session_DLStorage_GetAll
1818
Session_DLStroage_Delete
19+
Session_DLStroage_MaxConnect
1920

2021
Session_UPStroage_Init
2122
Session_UPStroage_Destory

XEngine_Source/StorageModule_Session/pch.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ extern "C" BOOL Session_User_Exist(LPCTSTR lpszUser, LPCTSTR lpszPass, int* pInt
4848
/************************************************************************/
4949
/* 存储会话导出的函数 */
5050
/************************************************************************/
51-
extern "C" BOOL Session_DLStroage_Init()
51+
extern "C" BOOL Session_DLStroage_Init(int nMaxConnect)
5252
{
53-
return m_DLStorage.Session_DLStroage_Init();
53+
return m_DLStorage.Session_DLStroage_Init(nMaxConnect);
5454
}
5555
extern "C" BOOL Session_DLStroage_Destory()
5656
{
@@ -84,6 +84,10 @@ extern "C" BOOL Session_DLStroage_Delete(LPCTSTR lpszClientAddr)
8484
{
8585
return m_DLStorage.Session_DLStroage_Delete(lpszClientAddr);
8686
}
87+
extern "C" BOOL Session_DLStroage_MaxConnect(LPCTSTR lpszClientAddr)
88+
{
89+
return m_DLStorage.Session_DLStroage_MaxConnect(lpszClientAddr);
90+
}
8791
extern "C" BOOL Session_UPStroage_Init(BOOL bUPResume)
8892
{
8993
return m_UPStorage.Session_UPStroage_Init(bUPResume);

XEngine_Source/XEngine_StorageApp/StorageApp_Download.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,21 @@ BOOL XEngine_Task_HttpDownload(LPCTSTR lpszClientAddr, LPCTSTR lpszMsgBuffer, in
141141
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("下载客户端:%s,发送的方法不支持"), lpszClientAddr);
142142
return FALSE;
143143
}
144-
144+
//连接数限制处理
145+
if (st_ServiceCfg.st_XLimit.nMaxDNConnect > 0)
146+
{
147+
if (!Session_DLStroage_MaxConnect(lpszClientAddr))
148+
{
149+
st_HDRParam.bIsClose = TRUE;
150+
st_HDRParam.nHttpCode = 503;
151+
152+
RfcComponents_HttpServer_SendMsgEx(xhDLHttp, tszSDBuffer, &nSDLen, &st_HDRParam);
153+
XEngine_Net_SendMsg(lpszClientAddr, tszSDBuffer, nSDLen, STORAGE_NETTYPE_HTTPDOWNLOAD);
154+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("下载客户端:%s,请求失败,连接数超过限制:%d,无法继续"), lpszClientAddr, st_ServiceCfg.st_XLimit.nMaxDNConnect);
155+
return FALSE;
156+
}
157+
}
158+
//验证用户
145159
if (st_ServiceCfg.st_XProxy.st_XProxyAuth.bAuth)
146160
{
147161
TCHAR tszUserName[64];

XEngine_Source/XEngine_StorageApp/XEngine_StorageApp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,12 @@ int main(int argc, char** argv)
234234
}
235235
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,初始化HTTP下载服务成功,IO线程个数:%d"), st_ServiceCfg.st_XMax.nStorageDLThread);
236236

237-
if (!Session_DLStroage_Init())
237+
if (!Session_DLStroage_Init(st_ServiceCfg.st_XLimit.nMaxDNConnect))
238238
{
239239
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("启动服务中,启动下载会话服务失败,错误:%lX"), Session_GetLastError());
240240
goto XENGINE_EXITAPP;
241241
}
242-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,启动下载会话服务成功,下载限速模式:%s"), st_ServiceCfg.st_XLimit.bLimitMode ? "" : "");
242+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,启动下载会话服务成功,下载限速模式:%s,连接数限制;%d"), st_ServiceCfg.st_XLimit.bLimitMode ? "" : "", st_ServiceCfg.st_XLimit.nMaxDNConnect);
243243

244244
if (st_ServiceCfg.st_XCert.bDLEnable)
245245
{

0 commit comments

Comments
 (0)