Skip to content

Commit 94a1f64

Browse files
committed
modify:The upload speed limit is now processed using a new mode, network events + algorithm events, and will not affect other network clients.
1 parent 525c446 commit 94a1f64

File tree

8 files changed

+125
-20
lines changed

8 files changed

+125
-20
lines changed

XEngine_Source/StorageModule_Session/Session_Define.h

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -283,22 +283,32 @@ extern "C" bool Session_UPStroage_Destory();
283283
类型:常量字符指针
284284
可空:N
285285
意思:输入文件地址
286-
参数.四:nFileSize
286+
参数.四:xhSpeed
287+
In/Out:Out
288+
类型:整数型
289+
可空:N
290+
意思:输入限速句柄
291+
参数.五:nFileSize
287292
In/Out:Out
288293
类型:整数型
289294
可空:N
290295
意思:输入文件大小
291-
参数.:bRewrite
296+
参数.:bRewrite
292297
In/Out:In
293298
类型:整数型
294299
可空:N
295300
意思:是否允许覆写
296-
参数.六:nPosStart
301+
参数.七:nSpeedLimit
302+
In/Out:In
303+
类型:整数型
304+
可空:Y
305+
意思:输入上传限速速率
306+
参数.八:nPosStart
297307
In/Out:In
298308
类型:整数型
299309
可空:Y
300310
意思:输入起始位置
301-
参数.:nPostEnd
311+
参数.:nPostEnd
302312
In/Out:In
303313
类型:整数型
304314
可空:Y
@@ -308,7 +318,7 @@ extern "C" bool Session_UPStroage_Destory();
308318
意思:是否成功
309319
备注:
310320
*********************************************************************/
311-
extern "C" bool Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, __int64x nFileSize, bool bRewrite, int nPosStart = 0, int nPostEnd = 0);
321+
extern "C" bool Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, XHANDLE xhSpeed, __int64x nFileSize, bool bRewrite, int nSpeedLimit = 0, int nPosStart = 0, int nPostEnd = 0);
312322
/********************************************************************
313323
函数名称:Session_UPStroage_GetInfo
314324
函数功能:获取上传客户端信息
@@ -329,6 +339,20 @@ extern "C" bool Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuc
329339
*********************************************************************/
330340
extern "C" bool Session_UPStroage_GetInfo(LPCXSTR lpszClientAddr, SESSION_STORAGEINFO* pSt_StorageInfo);
331341
/********************************************************************
342+
函数名称:Session_UPStroage_GetSpeed
343+
函数功能:获得速率限制句柄
344+
参数.一:lpszClientAddr
345+
In/Out:In
346+
类型:常量字符指针
347+
可空:N
348+
意思:输入要操作的客户端
349+
返回值
350+
类型:句柄
351+
意思:返回速率句柄
352+
备注:
353+
*********************************************************************/
354+
extern "C" XHANDLE Session_UPStroage_GetSpeed(LPCXSTR lpszClientAddr);
355+
/********************************************************************
332356
函数名称:Session_UPStroage_Write
333357
函数功能:写入数据到文件
334358
参数.一:lpszClientAddr

XEngine_Source/StorageModule_Session/Session_Stroage/Session_UPStroage.cpp

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,32 @@ bool CSession_UPStroage::Session_UPStroage_Destory()
8282
类型:常量字符指针
8383
可空:N
8484
意思:输入文件地址
85-
参数.四:nFileSize
85+
参数.四:xhSpeed
86+
In/Out:Out
87+
类型:整数型
88+
可空:N
89+
意思:输入限速句柄
90+
参数.五:nFileSize
8691
In/Out:Out
8792
类型:整数型
8893
可空:N
8994
意思:输入文件大小
90-
参数.:bRewrite
95+
参数.:bRewrite
9196
In/Out:In
9297
类型:整数型
9398
可空:N
9499
意思:是否允许覆写
95-
参数.六:nPosStart
100+
参数.七:nSpeedLimit
101+
In/Out:In
102+
类型:整数型
103+
可空:Y
104+
意思:输入上传限速速率
105+
参数.八:nPosStart
96106
In/Out:In
97107
类型:整数型
98108
可空:Y
99109
意思:输入起始位置
100-
参数.:nPostEnd
110+
参数.:nPostEnd
101111
In/Out:In
102112
类型:整数型
103113
可空:Y
@@ -107,7 +117,7 @@ bool CSession_UPStroage::Session_UPStroage_Destory()
107117
意思:是否成功
108118
备注:
109119
*********************************************************************/
110-
bool CSession_UPStroage::Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, __int64x nFileSize, bool bRewrite, int nPosStart /* = 0 */, int nPostEnd /* = 0 */)
120+
bool CSession_UPStroage::Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, XHANDLE xhSpeed, __int64x nFileSize, bool bRewrite, int nSpeedLimit, int nPosStart , int nPostEnd )
111121
{
112122
Session_IsErrorOccur = false;
113123

@@ -132,9 +142,11 @@ bool CSession_UPStroage::Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXST
132142
SESSION_STORAGEUPLOADER st_Client;
133143
memset(&st_Client, '\0', sizeof(SESSION_STORAGEUPLOADER));
134144
//填充下载信息
145+
st_Client.xhSpeed = xhSpeed;
135146
st_Client.st_StorageInfo.ullPosStart = nPosStart;
136147
st_Client.st_StorageInfo.ullPosEnd = nPostEnd;
137148
st_Client.st_StorageInfo.ullCount = nFileSize;
149+
st_Client.st_StorageInfo.nLimit = nSpeedLimit;
138150
_tcsxcpy(st_Client.st_StorageInfo.tszBuckKey, lpszBuckKey);
139151
_tcsxcpy(st_Client.st_StorageInfo.tszFileDir, lpszFileDir);
140152
_tcsxcpy(st_Client.st_StorageInfo.tszClientAddr, lpszClientAddr);
@@ -234,6 +246,43 @@ bool CSession_UPStroage::Session_UPStroage_GetInfo(LPCXSTR lpszClientAddr, SESSI
234246
return true;
235247
}
236248
/********************************************************************
249+
函数名称:Session_UPStroage_GetSpeed
250+
函数功能:获得速率限制句柄
251+
参数.一:lpszClientAddr
252+
In/Out:In
253+
类型:常量字符指针
254+
可空:N
255+
意思:输入要操作的客户端
256+
返回值
257+
类型:句柄
258+
意思:返回速率句柄
259+
备注:
260+
*********************************************************************/
261+
XHANDLE CSession_UPStroage::Session_UPStroage_GetSpeed(LPCXSTR lpszClientAddr)
262+
{
263+
Session_IsErrorOccur = false;
264+
265+
if ((NULL == lpszClientAddr))
266+
{
267+
Session_IsErrorOccur = true;
268+
Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_PARAMENT;
269+
return NULL;
270+
}
271+
272+
st_Locker.lock_shared();
273+
unordered_map<string, SESSION_STORAGEUPLOADER>::iterator stl_MapIterator = stl_MapStroage.find(lpszClientAddr);
274+
if (stl_MapIterator == stl_MapStroage.end())
275+
{
276+
Session_IsErrorOccur = true;
277+
Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_NOTFOUND;
278+
st_Locker.unlock_shared();
279+
return NULL;
280+
}
281+
XHANDLE xhSpeed = stl_MapIterator->second.xhSpeed;;
282+
st_Locker.unlock_shared();
283+
return xhSpeed;
284+
}
285+
/********************************************************************
237286
函数名称:Session_UPStroage_Write
238287
函数功能:写入数据到文件
239288
参数.一:lpszClientAddr

XEngine_Source/StorageModule_Session/Session_Stroage/Session_UPStroage.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ typedef struct
1414
{
1515
time_t nTimeStart;
1616
SESSION_STORAGEINFO st_StorageInfo;
17+
XHANDLE xhSpeed;
1718
}SESSION_STORAGEUPLOADER;
1819

1920
class CSession_UPStroage
@@ -24,8 +25,9 @@ class CSession_UPStroage
2425
public:
2526
bool Session_UPStroage_Init(int nMaxConnect, bool bUPResume = false);
2627
bool Session_UPStroage_Destory();
27-
bool Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, __int64x nFileSize, bool bRewrite, int nPosStart = 0, int nPostEnd = 0);
28+
bool Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, XHANDLE xhSpeed, __int64x nFileSize, bool bRewrite, int nSpeedLimit = 0, int nPosStart = 0, int nPostEnd = 0);
2829
bool Session_UPStroage_GetInfo(LPCXSTR lpszClientAddr, SESSION_STORAGEINFO* pSt_StorageInfo);
30+
XHANDLE Session_UPStroage_GetSpeed(LPCXSTR lpszClientAddr);
2931
bool Session_UPStroage_Write(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen);
3032
bool Session_UPStroage_Exist(LPCXSTR lpszClientAddr);
3133
bool Session_UPStorage_GetAll(SESSION_STORAGEINFO*** pppSt_StorageInfo, int* pInt_ListCount);

XEngine_Source/StorageModule_Session/StorageModule_Session.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ EXPORTS
1818
Session_UPStroage_Destory
1919
Session_UPStroage_Insert
2020
Session_UPStroage_GetInfo
21+
Session_UPStroage_GetSpeed
2122
Session_UPStroage_Write
2223
Session_UPStroage_Exist
2324
Session_UPStorage_GetAll

XEngine_Source/StorageModule_Session/pch.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,18 @@ extern "C" bool Session_UPStroage_Destory()
8383
{
8484
return m_UPStorage.Session_UPStroage_Destory();
8585
}
86-
extern "C" bool Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, __int64x nFileSize, bool bRewrite, int nPosStart, int nPostEnd)
86+
extern "C" bool Session_UPStroage_Insert(LPCXSTR lpszClientAddr, LPCXSTR lpszBuckKey, LPCXSTR lpszFileDir, XHANDLE xhSpeed, __int64x nFileSize, bool bRewrite, int nSpeedLimit, int nPosStart, int nPostEnd)
8787
{
88-
return m_UPStorage.Session_UPStroage_Insert(lpszClientAddr, lpszBuckKey, lpszFileDir, nFileSize, bRewrite, nPosStart, nPostEnd);
88+
return m_UPStorage.Session_UPStroage_Insert(lpszClientAddr, lpszBuckKey, lpszFileDir, xhSpeed, nFileSize, bRewrite, nSpeedLimit, nPosStart, nPostEnd);
8989
}
9090
extern "C" bool Session_UPStroage_GetInfo(LPCXSTR lpszClientAddr, SESSION_STORAGEINFO * pSt_StorageInfo)
9191
{
9292
return m_UPStorage.Session_UPStroage_GetInfo(lpszClientAddr, pSt_StorageInfo);
9393
}
94+
extern "C" XHANDLE Session_UPStroage_GetSpeed(LPCXSTR lpszClientAddr)
95+
{
96+
return m_UPStorage.Session_UPStroage_GetSpeed(lpszClientAddr);
97+
}
9498
extern "C" bool Session_UPStroage_Write(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen)
9599
{
96100
return m_UPStorage.Session_UPStroage_Write(lpszClientAddr, lpszMsgBuffer, nMsgLen);

XEngine_Source/XEngine_StorageApp/StorageApp_Network.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ void CALLBACK XEngine_Callback_UPLoaderRecv(LPCXSTR lpszClientAddr, XSOCKET hSoc
7676
}
7777
}
7878
SocketOpt_HeartBeat_ActiveAddrEx(xhHBUPLoader, lpszClientAddr);
79-
80-
int nCount = 0;
81-
__int64u nTimeWait = 0;
82-
Session_UPStorage_GetAll(NULL, &nCount);
83-
Algorithm_Calculation_SleepFlow(xhLimit, &nTimeWait, st_ServiceCfg.st_XLimit.nMaxUPLoader, nCount, nMsgLen);
84-
std::this_thread::sleep_for(std::chrono::microseconds(nTimeWait));
8579
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_DEBUG, _X("上传客户端:%s,投递包成功,大小:%d"), lpszClientAddr, nMsgLen);
8680
}
8781
void CALLBACK XEngine_Callback_UPLoaderLeave(LPCXSTR lpszClientAddr, XSOCKET hSocket, XPVOID lParam)
@@ -212,6 +206,7 @@ bool XEngine_Net_CloseClient(LPCXSTR lpszClientAddr, int nLeaveType, int nClient
212206
NetCore_TCPXCore_CloseForClientEx(xhNetUPLoader, lpszClientAddr);
213207
SocketOpt_HeartBeat_DeleteAddrEx(xhHBUPLoader, lpszClientAddr);
214208
}
209+
Algorithm_Calculation_Close(Session_UPStroage_GetSpeed(lpszClientAddr));
215210
Session_UPStroage_Delete(lpszClientAddr);
216211
HttpProtocol_Server_CloseClinetEx(xhUPHttp, lpszClientAddr);
217212
OPenSsl_Server_CloseClientEx(xhUPSsl, lpszClientAddr);

XEngine_Source/XEngine_StorageApp/StorageApp_UPLoader.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ XHTHREAD CALLBACK XEngine_UPLoader_HTTPThread(XPVOID lParam)
3939
}
4040
return 0;
4141
}
42+
void CALLBACK XEngine_UPLoader_UPFlow(XHANDLE xhToken, bool bSDFlow, bool bRVFlow, bool bTime, __int64u nSDFlow, __int64u nRVFlow, __int64u nTimeFlow, XPVOID lParam)
43+
{
44+
XCHAR tszIPAddr[128] = {};
45+
_tcsxcpy(tszIPAddr, (LPCXSTR)lParam);
46+
if (bSDFlow)
47+
{
48+
NetCore_TCPXCore_PasueRecvEx(xhNetUPLoader, tszIPAddr, false);
49+
}
50+
else
51+
{
52+
NetCore_TCPXCore_PasueRecvEx(xhNetUPLoader, tszIPAddr, true);
53+
}
54+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _X("上传客户端:%s,接受数据标志:%d,当前平均流量:%llu"), tszIPAddr, bSDFlow, nSDFlow);
55+
}
4256
bool XEngine_Task_HttpUPLoader(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen, RFCCOMPONENTS_HTTP_REQPARAM* pSt_HTTPParam, XCHAR** pptszListHdr, int nHdrCount)
4357
{
4458
int nSDLen = 2048;
@@ -292,7 +306,21 @@ bool XEngine_Task_HttpUPLoader(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, in
292306
}
293307
SystemApi_File_CreateMutilFolder(tszTmpPath);
294308
}
295-
if (!Session_UPStroage_Insert(lpszClientAddr, st_StorageBucket.tszBuckKey, tszFileDir, nPosCount, st_StorageBucket.st_PermissionFlags.bRewrite, nPosStart, nPosEnd))
309+
XHANDLE xhUPSpeed = NULL;
310+
if (nLimit > 0 || (st_ServiceCfg.st_XLimit.bLimitMode && st_ServiceCfg.st_XLimit.nMaxUPLoader > 0))
311+
{
312+
//处理限速情况
313+
XCHAR* ptszIPClient = (XCHAR*)malloc(MAX_PATH);
314+
memset(ptszIPClient, '\0', MAX_PATH);
315+
_tcsxcpy(ptszIPClient, lpszClientAddr);
316+
317+
nLimit = nLimit == 0 ? st_ServiceCfg.st_XLimit.nMaxUPLoader : nLimit;
318+
xhUPSpeed = Algorithm_Calculation_Create();
319+
Algorithm_Calculation_PassiveOPen(xhUPSpeed, XEngine_UPLoader_UPFlow, nLimit, 0, 0, false, ptszIPClient);
320+
NetCore_TCPXCore_PasueRecvEx(xhNetUPLoader, lpszClientAddr, false);
321+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _X("上传客户端:%s,上传限速被启用,文件:%s,限速:%d"), lpszClientAddr, tszFileDir, nLimit);
322+
}
323+
if (!Session_UPStroage_Insert(lpszClientAddr, st_StorageBucket.tszBuckKey, tszFileDir, xhUPSpeed, nPosCount, st_StorageBucket.st_PermissionFlags.bRewrite, nLimit, nPosStart, nPosEnd))
296324
{
297325
st_HDRParam.bIsClose = true;
298326
st_HDRParam.nHttpCode = 500;
@@ -354,6 +382,7 @@ bool XEngine_Task_HttpUPLoader(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, in
354382
{
355383
Session_UPStroage_Write(lpszClientAddr, lpszMsgBuffer, nMsgLen);
356384
}
385+
Algorithm_Calculation_ADDSDFlow(Session_UPStroage_GetSpeed(lpszClientAddr), nMsgLen);
357386
HttpProtocol_Server_GetRecvModeEx(xhUPHttp, lpszClientAddr, &nRVMode, &nRVCount, &nHDSize);
358387
if (nHDSize >= nRVCount)
359388
{
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma once
22

33
XHTHREAD CALLBACK XEngine_UPLoader_HTTPThread(XPVOID lParam);
4+
void CALLBACK XEngine_UPLoader_UPFlow(XHANDLE xhToken, bool bSDFlow, bool bRVFlow, bool bTime, XPVOID lParam);
45
bool XEngine_Task_HttpUPLoader(LPCXSTR lpszClientAddr, LPCXSTR lpszMsgBuffer, int nMsgLen, RFCCOMPONENTS_HTTP_REQPARAM* pSt_HTTPParam, XCHAR** pptszListHdr, int nHdrCount);

0 commit comments

Comments
 (0)