Skip to content

Commit 448b190

Browse files
committed
Improve:speed limit accuracy
1 parent 902ed85 commit 448b190

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

XEngine_Source/XEngine_StorageApp/StorageApp_Download.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ XHTHREAD CALLBACK XEngine_Download_HTTPThread(LPVOID lParam)
4545
void CALLBACK XEngine_Download_CBSend(LPCSTR lpszClientAddr, SOCKET hSocket, LPVOID lParam)
4646
{
4747
int nMsgLen = 4096;
48+
int nListCount = 0;
4849
__int64u nTimeWait = 0;
4950
TCHAR tszMsgBuffer[4096];
50-
list<string> stl_ListClient;
5151

5252
memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
5353

@@ -91,11 +91,9 @@ void CALLBACK XEngine_Download_CBSend(LPCSTR lpszClientAddr, SOCKET hSocket, LPV
9191
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("下载客户端:%s,获取用户对应文件内容失败,错误:%lX"), lpszClientAddr, Session_GetLastError());
9292
}
9393
//限速
94-
Session_DLStroage_GetCount(&stl_ListClient);
95-
Algorithm_Calculation_SleepFlow(&nTimeWait, st_ServiceCfg.st_XLimit.nMaxDNLoader, stl_ListClient.size(), 4096);
96-
stl_ListClient.clear();
97-
//计算机每次休眠时间是不一定的,*2作为修正,如果想要更准确的需要使用到 Algorithm_Calculation_Create来处理
98-
std::this_thread::sleep_for(std::chrono::microseconds(nTimeWait * 2));
94+
Session_DLStroage_GetCount(&nListCount);
95+
Algorithm_Calculation_SleepFlow(xhLimit, &nTimeWait, st_ServiceCfg.st_XLimit.nMaxDNLoader, nListCount, 4096);
96+
std::this_thread::sleep_for(std::chrono::microseconds(nTimeWait));
9997
}
10098

10199
BOOL XEngine_Task_HttpDownload(LPCTSTR lpszClientAddr, LPCTSTR lpszMsgBuffer, int nMsgLen, RFCCOMPONENTS_HTTP_REQPARAM* pSt_HTTPParam, TCHAR** pptszListHdr, int nHdrCount)
@@ -178,6 +176,11 @@ BOOL XEngine_Task_HttpDownload(LPCTSTR lpszClientAddr, LPCTSTR lpszMsgBuffer, in
178176
st_HDRParam.st_Range.nPosCount = ullCount;
179177
st_HDRParam.nHttpCode = 206;
180178
st_HDRParam.bIsClose = TRUE;
179+
180+
if (0 == st_HDRParam.st_Range.nPosEnd)
181+
{
182+
st_HDRParam.st_Range.nPosEnd = ullCount;
183+
}
181184
}
182185
else
183186
{
@@ -208,17 +211,14 @@ BOOL XEngine_Task_SendDownload(LPCTSTR lpszClientAddr, LPCTSTR lpszMsgBuffer, in
208211
{
209212
if (!XEngine_Net_SendMsg(lpszClientAddr, lpszMsgBuffer, nMsgLen, STORAGE_NETTYPE_HTTPDOWNLOAD))
210213
{
211-
SESSION_STORAGEDYNAMICRATE st_StorageRate;
212-
memset(&st_StorageRate, '\0', sizeof(SESSION_STORAGEDYNAMICRATE));
213-
214-
if (Session_DLStorage_SetSeek(lpszClientAddr, -nMsgLen, TRUE, &st_StorageRate))
214+
if (Session_DLStorage_SetSeek(lpszClientAddr, -nMsgLen))
215215
{
216-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN, _T("下载客户端:%s,正在发送文件数据,发送失败,移动指针:%d,错误次数:%d,等待时间:%llu 微妙,恢复次数:%d"), lpszClientAddr, -nMsgLen, st_StorageRate.nErrorCount, st_StorageRate.ullTimeWait, st_StorageRate.nAutoNumber);
216+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_WARN, _T("下载客户端:%s,正在发送文件数据,发送失败,移动指针:%d"), lpszClientAddr, -nMsgLen);
217217
}
218218
else
219219
{
220220
XEngine_Net_CloseClient(lpszClientAddr, STORAGE_LEAVETYPE_CLOSE, STORAGE_NETTYPE_HTTPDOWNLOAD);
221-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("下载客户端:%s,正在发送文件数据,大小:%d,发送超过重试次数,无法继续,移除发送队列,错误次数:%d,等待时间:%llu 微妙,恢复次数:%d"), lpszClientAddr, nMsgLen, st_StorageRate.nErrorCount, st_StorageRate.ullTimeWait, st_StorageRate.nAutoNumber);
221+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("下载客户端:%s,正在发送文件数据,大小:%d,发送超过重试次数,无法继续,移除发送队列"), lpszClientAddr, nMsgLen);
222222
}
223223
return FALSE;
224224
}

XEngine_Source/XEngine_StorageApp/StorageApp_Hdr.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ extern XNETHANDLE xhDLPool;
9797
extern XNETHANDLE xhCTPool;
9898
extern XNETHANDLE xhP2XPPool;
9999

100+
extern XHANDLE xhLimit;
100101
extern XHANDLE xhUPHttp;
101102
extern XHANDLE xhDLHttp;
102103
extern XHANDLE xhCenterHttp;

XEngine_Source/XEngine_StorageApp/StorageApp_Network.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ void CALLBACK XEngine_Callback_UPLoaderRecv(LPCTSTR lpszClientAddr, SOCKET hSock
4242
int nCount = 0;
4343
__int64u nTimeWait = 0;
4444
Session_UPStorage_GetAll(NULL, &nCount);
45-
Algorithm_Calculation_SleepFlow(&nTimeWait, st_ServiceCfg.st_XLimit.nMaxUPLoader, nCount, nMsgLen);
46-
std::this_thread::sleep_for(std::chrono::microseconds(nTimeWait * 2));
45+
Algorithm_Calculation_SleepFlow(xhLimit, &nTimeWait, st_ServiceCfg.st_XLimit.nMaxUPLoader, nCount, nMsgLen);
46+
std::this_thread::sleep_for(std::chrono::microseconds(nTimeWait));
4747
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_DEBUG, _T("上传客户端:%s,投递包成功,大小:%d"), lpszClientAddr, nMsgLen);
4848
}
4949
void CALLBACK XEngine_Callback_UPLoaderLeave(LPCTSTR lpszClientAddr, SOCKET hSocket, LPVOID lParam)

XEngine_Source/XEngine_StorageApp/XEngine_StorageApp.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ XNETHANDLE xhDLPool = 0;
1818
XNETHANDLE xhCTPool = 0;
1919
XNETHANDLE xhP2XPPool = 0;
2020

21+
XHANDLE xhLimit = NULL;
2122
XHANDLE xhUPHttp = NULL;
2223
XHANDLE xhDLHttp = NULL;
2324
XHANDLE xhCenterHttp = NULL;
@@ -56,6 +57,7 @@ void ServiceApp_Stop(int signo)
5657
ManagePool_Thread_NQDestroy(xhCTPool);
5758
ManagePool_Thread_NQDestroy(xhP2XPPool);
5859

60+
Algorithm_Calculation_Close(xhLimit);
5961
HelpComponents_XLog_Destroy(xhLog);
6062

6163
Session_User_Destory();
@@ -165,6 +167,14 @@ int main(int argc, char** argv)
165167
signal(SIGABRT, ServiceApp_Stop);
166168
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,初始化服务器信号管理成功"));
167169

170+
xhLimit = Algorithm_Calculation_Create();
171+
if (NULL == xhLimit)
172+
{
173+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("启动服务中,创建流量限速对象模式失败,错误:%lX"), Algorithm_GetLastError());
174+
goto XENGINE_EXITAPP;
175+
}
176+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,创建流量限速对象模式成功"));
177+
168178
if (!NetXApi_Address_OpenQQWry(st_ServiceCfg.st_P2xp.tszQQWryFile))
169179
{
170180
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("启动服务中,初始化IP地址数据库失败,错误:%lX"), NetXApi_GetLastError());
@@ -265,12 +275,12 @@ int main(int argc, char** argv)
265275
goto XENGINE_EXITAPP;
266276
}
267277
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,启动用户管理服务成功"));
268-
if (!Session_DLStroage_Init(st_ServiceCfg.st_XLimit.nDLTry, st_ServiceCfg.st_XLimit.nDLError))
278+
if (!Session_DLStroage_Init(st_ServiceCfg.st_XLimit.nDLTry))
269279
{
270280
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("启动服务器中,启动下载会话服务失败,错误:%lX"), Session_GetLastError());
271281
goto XENGINE_EXITAPP;
272282
}
273-
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,启动下载会话服务成功"));
283+
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_INFO, _T("启动服务中,启动下载会话服务成功,下载错误重试次数:%d"), st_ServiceCfg.st_XLimit.nDLTry);
274284
if (!Session_UPStroage_Init())
275285
{
276286
XLOG_PRINT(xhLog, XENGINE_HELPCOMPONENTS_XLOG_IN_LOGLEVEL_ERROR, _T("启动服务器中,启动上传会话服务失败,错误:%lX"), Session_GetLastError());
@@ -434,6 +444,7 @@ int main(int argc, char** argv)
434444
ManagePool_Thread_NQDestroy(xhCTPool);
435445
ManagePool_Thread_NQDestroy(xhP2XPPool);
436446

447+
Algorithm_Calculation_Close(xhLimit);
437448
HelpComponents_XLog_Destroy(xhLog);
438449

439450
Session_User_Destory();

0 commit comments

Comments
 (0)