Skip to content

Commit 74dcaf8

Browse files
committed
added:dynamic rate first Version
1 parent d29133d commit 74dcaf8

File tree

10 files changed

+155
-90
lines changed

10 files changed

+155
-90
lines changed

XEngine_Source/StorageModule_Session/Session_Define.h

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,20 @@
1515
//////////////////////////////////////////////////////////////////////////
1616
typedef struct
1717
{
18+
time_t nTimeError; //最后错误时间
19+
int nErrorCount; //错误次数
20+
int nTimeWait; //等待恢复时间,单位秒
21+
}SESSION_STORAGEDYNAMICRATE;
22+
typedef struct
23+
{
24+
SESSION_STORAGEDYNAMICRATE st_DynamicRate;
1825
TCHAR tszFileDir[MAX_PATH]; //文件地址
1926
TCHAR tszClientAddr[128]; //操作的用户地址
2027
__int64x ullCount; //总大小
2128
__int64x ullRWCount; //读取(写入)总大小
2229
__int64x ullRWLen; //已经读取(写入)的大小
2330
__int64x ullPosStart; //开始位置
2431
__int64x ullPosEnd; //结束位置
25-
int nErrorCount; //错误次数
2632
FILE* pSt_File;
2733
}SESSION_STORAGEINFO;
2834
//////////////////////////////////////////////////////////////////////////
@@ -152,31 +158,26 @@ extern "C" BOOL Session_DLStroage_Insert(LPCTSTR lpszClientAddr, LPCTSTR lpszFil
152158
类型:整数型
153159
可空:N
154160
意思:输入要操作的队列
155-
参数.二:nIndex
156-
In/Out:In
157-
类型:整数型
158-
可空:N
159-
意思:输入要操作的队列索引
160-
参数.三:ptszClientAddr
161+
参数.二:lpszClientAddr
161162
In/Out:In
162-
类型:整数型
163+
类型:常量字符指针
163164
可空:N
164-
意思:输出客户端地址
165-
参数.:ptszMsgBuffer
165+
意思:输入客户端地址
166+
参数.:ptszMsgBuffer
166167
In/Out:In
167-
类型:整数型
168+
类型:字符指针
168169
可空:N
169-
参数.:pInt_MsgLen
170+
参数.:pInt_MsgLen
170171
In/Out:In
171-
类型:整数型
172+
类型:整数型指针
172173
可空:N
173174
意思:输出获取的缓冲区大小
174175
返回值
175176
类型:逻辑型
176177
意思:是否成功
177178
备注:
178179
*********************************************************************/
179-
extern "C" BOOL Session_DLStroage_GetList(int nPool, int nIndex, TCHAR* ptszClientAddr, TCHAR* ptszMsgBuffer, int* pInt_MsgLen);
180+
extern "C" BOOL Session_DLStroage_GetList(int nPool, LPCTSTR lpszClientAddr, TCHAR * ptszMsgBuffer, int* pInt_MsgLen);
180181
/********************************************************************
181182
函数名称:Session_DLStroage_GetInfo
182183
函数功能:获取下载信息
@@ -185,11 +186,11 @@ extern "C" BOOL Session_DLStroage_GetList(int nPool, int nIndex, TCHAR* ptszClie
185186
类型:整数型
186187
可空:N
187188
意思:输入要操作的下载池
188-
参数.二:nIndex
189+
参数.二:lpszClientAddr
189190
In/Out:In
190-
类型:整数型
191+
类型:常量字符指针
191192
可空:N
192-
意思:输入要操作的索引
193+
意思:输入要操作的客户端
193194
参数.三:pSt_StorageInfo
194195
In/Out:Out
195196
类型:数据结构指针
@@ -200,7 +201,7 @@ extern "C" BOOL Session_DLStroage_GetList(int nPool, int nIndex, TCHAR* ptszClie
200201
意思:是否成功
201202
备注:
202203
*********************************************************************/
203-
extern "C" BOOL Session_DLStroage_GetInfo(int nPool, int nIndex, SESSION_STORAGEINFO* pSt_StorageInfo);
204+
extern "C" BOOL Session_DLStroage_GetInfo(int nPool, LPCTSTR lpszClientAddr, SESSION_STORAGEINFO* pSt_StorageInfo);
204205
/********************************************************************
205206
函数名称:Session_DLStroage_GetCount
206207
函数功能:获取队列拥有的个数
@@ -209,17 +210,17 @@ extern "C" BOOL Session_DLStroage_GetInfo(int nPool, int nIndex, SESSION_STORAGE
209210
类型:整数型
210211
可空:N
211212
意思:输入要操作的队列
212-
参数.二:pInt_ListCount
213+
参数.二:pStl_ListClient
213214
In/Out:Out
214-
类型:整数型指针
215+
类型:STL容器指针
215216
可空:N
216-
意思:输出队列个数
217+
意思:输出要发送的队列个数
217218
返回值
218219
类型:逻辑型
219220
意思:是否成功
220221
备注:
221222
*********************************************************************/
222-
extern "C" BOOL Session_DLStroage_GetCount(int nIndex, int* pInt_ListCount);
223+
extern "C" BOOL Session_DLStroage_GetCount(int nPool, list<string>*pStl_ListClient);
223224
/********************************************************************
224225
函数名称:Session_DLStorage_SetSeek
225226
函数功能:移动文件指针
@@ -233,12 +234,22 @@ extern "C" BOOL Session_DLStroage_GetCount(int nIndex, int* pInt_ListCount);
233234
类型:整数型
234235
可空:N
235236
意思:输入文件位置
237+
参数.三:bError
238+
In/Out:In
239+
类型:逻辑型
240+
可空:Y
241+
意思:是否因为错误引起的
242+
参数.四:pSt_StorageRate
243+
In/Out:In
244+
类型:数据结构指针
245+
可空:Y
246+
意思:输出速率错误信息
236247
返回值
237248
类型:逻辑型
238249
意思:是否成功
239250
备注:
240251
*********************************************************************/
241-
extern "C" BOOL Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek);
252+
extern "C" BOOL Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek, BOOL bError = TRUE, SESSION_STORAGEDYNAMICRATE * pSt_StorageRate = NULL);
242253
/********************************************************************
243254
函数名称:Session_DLStroage_Delete
244255
函数功能:删除一个队列

XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.cpp

Lines changed: 77 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -238,35 +238,30 @@ BOOL CSession_DLStroage::Session_DLStroage_Insert(LPCTSTR lpszClientAddr, LPCTST
238238
类型:整数型
239239
可空:N
240240
意思:输入要操作的队列
241-
参数.二:nIndex
241+
参数.二:lpszClientAddr
242242
In/Out:In
243-
类型:整数型
243+
类型:常量字符指针
244244
可空:N
245-
意思:输入要操作的队列索引
246-
参数.三:ptszClientAddr
245+
意思:输入客户端地址
246+
参数.三:ptszMsgBuffer
247247
In/Out:In
248-
类型:整数型
248+
类型:字符指针
249249
可空:N
250-
意思:输出客户端地址
251-
参数.四:ptszMsgBuffer
250+
参数.四:pInt_MsgLen
252251
In/Out:In
253-
类型:整数型
254-
可空:N
255-
参数.五:pInt_MsgLen
256-
In/Out:In
257-
类型:整数型
252+
类型:整数型指针
258253
可空:N
259254
意思:输出获取的缓冲区大小
260255
返回值
261256
类型:逻辑型
262257
意思:是否成功
263258
备注:
264259
*********************************************************************/
265-
BOOL CSession_DLStroage::Session_DLStroage_GetList(int nPool, int nIndex, TCHAR* ptszClientAddr, TCHAR* ptszMsgBuffer, int* pInt_MsgLen)
260+
BOOL CSession_DLStroage::Session_DLStroage_GetList(int nPool, LPCTSTR lpszClientAddr, TCHAR* ptszMsgBuffer, int* pInt_MsgLen)
266261
{
267262
Session_IsErrorOccur = FALSE;
268263

269-
if ((NULL == ptszClientAddr) || (NULL == ptszMsgBuffer))
264+
if ((NULL == lpszClientAddr) || (NULL == ptszMsgBuffer) || (NULL == pInt_MsgLen))
270265
{
271266
Session_IsErrorOccur = TRUE;
272267
Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_PARAMENT;
@@ -286,10 +281,8 @@ BOOL CSession_DLStroage::Session_DLStroage_GetList(int nPool, int nIndex, TCHAR*
286281
list<SESSION_STORAGEINFO>::iterator stl_ListIterator = stl_MapIterator->second.pStl_ListStorage->begin();
287282
for (int i = 0; stl_ListIterator != stl_MapIterator->second.pStl_ListStorage->end(); stl_ListIterator++, i++)
288283
{
289-
if (nIndex == i)
284+
if (0 == _tcsncmp(lpszClientAddr, stl_ListIterator->tszClientAddr, _tcslen(lpszClientAddr)))
290285
{
291-
_tcscpy(ptszClientAddr, stl_ListIterator->tszClientAddr);
292-
293286
if (stl_ListIterator->ullRWLen >= stl_ListIterator->ullRWCount)
294287
{
295288
*pInt_MsgLen = 0;
@@ -318,11 +311,11 @@ BOOL CSession_DLStroage::Session_DLStroage_GetList(int nPool, int nIndex, TCHAR*
318311
类型:整数型
319312
可空:N
320313
意思:输入要操作的下载池
321-
参数.二:nIndex
314+
参数.二:lpszClientAddr
322315
In/Out:In
323-
类型:整数型
316+
类型:常量字符指针
324317
可空:N
325-
意思:输入要操作的索引
318+
意思:输入要操作的客户端
326319
参数.三:pSt_StorageInfo
327320
In/Out:Out
328321
类型:数据结构指针
@@ -333,7 +326,7 @@ BOOL CSession_DLStroage::Session_DLStroage_GetList(int nPool, int nIndex, TCHAR*
333326
意思:是否成功
334327
备注:
335328
*********************************************************************/
336-
BOOL CSession_DLStroage::Session_DLStroage_GetInfo(int nPool, int nIndex, SESSION_STORAGEINFO* pSt_StorageInfo)
329+
BOOL CSession_DLStroage::Session_DLStroage_GetInfo(int nPool, LPCTSTR lpszClientAddr, SESSION_STORAGEINFO* pSt_StorageInfo)
337330
{
338331
Session_IsErrorOccur = FALSE;
339332

@@ -357,7 +350,7 @@ BOOL CSession_DLStroage::Session_DLStroage_GetInfo(int nPool, int nIndex, SESSIO
357350
list<SESSION_STORAGEINFO>::iterator stl_ListIterator = stl_MapIterator->second.pStl_ListStorage->begin();
358351
for (int i = 0; stl_ListIterator != stl_MapIterator->second.pStl_ListStorage->end(); stl_ListIterator++, i++)
359352
{
360-
if (nIndex == i)
353+
if (0 == _tcsncmp(lpszClientAddr, stl_ListIterator->tszClientAddr, _tcslen(lpszClientAddr)))
361354
{
362355
*pSt_StorageInfo = *stl_ListIterator;
363356
break;
@@ -375,21 +368,21 @@ BOOL CSession_DLStroage::Session_DLStroage_GetInfo(int nPool, int nIndex, SESSIO
375368
类型:整数型
376369
可空:N
377370
意思:输入要操作的队列
378-
参数.二:pInt_ListCount
371+
参数.二:pStl_ListClient
379372
In/Out:Out
380-
类型:整数型指针
373+
类型:STL容器指针
381374
可空:N
382-
意思:输出队列个数
375+
意思:输出要发送的队列个数
383376
返回值
384377
类型:逻辑型
385378
意思:是否成功
386379
备注:
387380
*********************************************************************/
388-
BOOL CSession_DLStroage::Session_DLStroage_GetCount(int nPool, int* pInt_ListCount)
381+
BOOL CSession_DLStroage::Session_DLStroage_GetCount(int nPool, list<string>* pStl_ListClient)
389382
{
390383
Session_IsErrorOccur = FALSE;
391384

392-
if (NULL == pInt_ListCount)
385+
if (NULL == pStl_ListClient)
393386
{
394387
Session_IsErrorOccur = TRUE;
395388
Session_dwErrorCode = ERROR_STORAGE_MODULE_SESSION_PARAMENT;
@@ -406,7 +399,34 @@ BOOL CSession_DLStroage::Session_DLStroage_GetCount(int nPool, int* pInt_ListCou
406399
return FALSE;
407400
}
408401
stl_MapIterator->second.st_Locker->lock_shared();
409-
*pInt_ListCount = stl_MapIterator->second.pStl_ListStorage->size();
402+
list<SESSION_STORAGEINFO>::iterator stl_ListIterator = stl_MapIterator->second.pStl_ListStorage->begin();
403+
for (; stl_ListIterator != stl_MapIterator->second.pStl_ListStorage->end(); stl_ListIterator++)
404+
{
405+
//是否需要等待恢复
406+
if (stl_ListIterator->st_DynamicRate.nTimeWait > 0)
407+
{
408+
time_t nTimeNow = time(NULL);
409+
if ((nTimeNow - stl_ListIterator->st_DynamicRate.nTimeError) > stl_ListIterator->st_DynamicRate.nTimeWait)
410+
{
411+
//等待时间超过,可以加入
412+
pStl_ListClient->push_back(stl_ListIterator->tszClientAddr);
413+
}
414+
//速率恢复测算
415+
if ((nTimeNow - stl_ListIterator->st_DynamicRate.nTimeError) > (stl_ListIterator->st_DynamicRate.nErrorCount * 2))
416+
{
417+
stl_ListIterator->st_DynamicRate.nErrorCount--;
418+
if (0 == stl_ListIterator->st_DynamicRate.nErrorCount)
419+
{
420+
stl_ListIterator->st_DynamicRate.nTimeWait = 0;
421+
stl_ListIterator->st_DynamicRate.nTimeError = 0;
422+
}
423+
}
424+
}
425+
else
426+
{
427+
pStl_ListClient->push_back(stl_ListIterator->tszClientAddr);
428+
}
429+
}
410430
stl_MapIterator->second.st_Locker->unlock_shared();
411431
st_Locker.unlock_shared();
412432
return TRUE;
@@ -424,12 +444,22 @@ BOOL CSession_DLStroage::Session_DLStroage_GetCount(int nPool, int* pInt_ListCou
424444
类型:整数型
425445
可空:N
426446
意思:输入文件位置
447+
参数.三:bError
448+
In/Out:In
449+
类型:逻辑型
450+
可空:Y
451+
意思:是否因为错误引起的
452+
参数.四:pSt_StorageRate
453+
In/Out:In
454+
类型:数据结构指针
455+
可空:Y
456+
意思:输出速率错误信息
427457
返回值
428458
类型:逻辑型
429459
意思:是否成功
430460
备注:
431461
*********************************************************************/
432-
BOOL CSession_DLStroage::Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek)
462+
BOOL CSession_DLStroage::Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek, BOOL bError /* = TRUE */, SESSION_STORAGEDYNAMICRATE* pSt_StorageRate /* = NULL */)
433463
{
434464
Session_IsErrorOccur = FALSE;
435465

@@ -445,10 +475,26 @@ BOOL CSession_DLStroage::Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int n
445475
if (0 == _tcsncmp(lpszClientAddr, stl_ListIterator->tszClientAddr, _tcslen(lpszClientAddr)))
446476
{
447477
bFound = TRUE;
448-
stl_ListIterator->nErrorCount++;
478+
if (bError)
479+
{
480+
time_t nTimeNow = time(NULL);
481+
//如果发送错误事件小于等于1秒
482+
if ((nTimeNow - stl_ListIterator->st_DynamicRate.nTimeError) <= 1)
483+
{
484+
//那么根据错误次数计算等待时间
485+
stl_ListIterator->st_DynamicRate.nTimeWait = stl_ListIterator->st_DynamicRate.nErrorCount + 1;
486+
}
487+
stl_ListIterator->st_DynamicRate.nErrorCount++;
488+
stl_ListIterator->st_DynamicRate.nTimeError = time(NULL);
489+
490+
if (NULL != pSt_StorageRate)
491+
{
492+
*pSt_StorageRate = stl_ListIterator->st_DynamicRate;
493+
}
494+
}
449495
fseek(stl_ListIterator->pSt_File, nSeek, SEEK_CUR);
450496
//如果超过次数.返回错误
451-
if (stl_ListIterator->nErrorCount > m_nTryTime)
497+
if (stl_ListIterator->st_DynamicRate.nErrorCount > m_nTryTime)
452498
{
453499
stl_MapIterator->second.st_Locker->unlock_shared();
454500
st_Locker.unlock_shared();

XEngine_Source/StorageModule_Session/Session_Stroage/Session_DLStroage.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ class CSession_DLStroage
2525
BOOL Session_DLStroage_Init(int nPoolCount = 1, int nTryTime = 3);
2626
BOOL Session_DLStroage_Destory();
2727
BOOL Session_DLStroage_Insert(LPCTSTR lpszClientAddr, LPCTSTR lpszFileDir, __int64x* pInt_Count, __int64x* pInt_LeftCount, int nPosStart = 0, int nPostEnd = 0);
28-
BOOL Session_DLStroage_GetList(int nPool, int nIndex, TCHAR* ptszClientAddr, TCHAR* ptszMsgBuffer, int* pInt_MsgLen);
29-
BOOL Session_DLStroage_GetInfo(int nPool, int nIndex, SESSION_STORAGEINFO* pSt_StorageInfo);
30-
BOOL Session_DLStroage_GetCount(int nPool, int* pInt_ListCount);
31-
BOOL Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek);
28+
BOOL Session_DLStroage_GetList(int nPool, LPCTSTR lpszClientAddr, TCHAR* ptszMsgBuffer, int* pInt_MsgLen);
29+
BOOL Session_DLStroage_GetInfo(int nPool, LPCTSTR lpszClientAddr, SESSION_STORAGEINFO* pSt_StorageInfo);
30+
BOOL Session_DLStroage_GetCount(int nPool, list<string>* pStl_ListClient);
31+
BOOL Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek, BOOL bError = TRUE, SESSION_STORAGEDYNAMICRATE* pSt_StorageRate = NULL);
3232
BOOL Session_DLStroage_Delete(LPCTSTR lpszClientAddr);
3333
private:
3434
int m_nTryTime;

XEngine_Source/StorageModule_Session/pch.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,21 @@ extern "C" BOOL Session_DLStroage_Insert(LPCTSTR lpszClientAddr, LPCTSTR lpszFil
6060
{
6161
return m_DLStorage.Session_DLStroage_Insert(lpszClientAddr, lpszFileDir, pInt_Count, pInt_LeftCount, nPosStart, nPostEnd);
6262
}
63-
extern "C" BOOL Session_DLStroage_GetList(int nPool, int nIndex, TCHAR * ptszClientAddr, TCHAR * ptszMsgBuffer, int* pInt_MsgLen)
63+
extern "C" BOOL Session_DLStroage_GetList(int nPool, LPCTSTR lpszClientAddr, TCHAR * ptszMsgBuffer, int* pInt_MsgLen)
6464
{
65-
return m_DLStorage.Session_DLStroage_GetList(nPool, nIndex, ptszClientAddr, ptszMsgBuffer, pInt_MsgLen);
65+
return m_DLStorage.Session_DLStroage_GetList(nPool, lpszClientAddr, ptszMsgBuffer, pInt_MsgLen);
6666
}
67-
extern "C" BOOL Session_DLStroage_GetInfo(int nPool, int nIndex, SESSION_STORAGEINFO * pSt_StorageInfo)
67+
extern "C" BOOL Session_DLStroage_GetInfo(int nPool, LPCTSTR lpszClientAddr, SESSION_STORAGEINFO * pSt_StorageInfo)
6868
{
69-
return m_DLStorage.Session_DLStroage_GetInfo(nPool, nIndex, pSt_StorageInfo);
69+
return m_DLStorage.Session_DLStroage_GetInfo(nPool, lpszClientAddr, pSt_StorageInfo);
7070
}
71-
extern "C" BOOL Session_DLStroage_GetCount(int nIndex, int* pInt_ListCount)
71+
extern "C" BOOL Session_DLStroage_GetCount(int nIndex, list<string>*pStl_ListClient)
7272
{
73-
return m_DLStorage.Session_DLStroage_GetCount(nIndex, pInt_ListCount);
73+
return m_DLStorage.Session_DLStroage_GetCount(nIndex, pStl_ListClient);
7474
}
75-
extern "C" BOOL Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek)
75+
extern "C" BOOL Session_DLStorage_SetSeek(LPCTSTR lpszClientAddr, int nSeek, BOOL bError, SESSION_STORAGEDYNAMICRATE * pSt_StorageRate)
7676
{
77-
return m_DLStorage.Session_DLStorage_SetSeek(lpszClientAddr, nSeek);
77+
return m_DLStorage.Session_DLStorage_SetSeek(lpszClientAddr, nSeek, bError, pSt_StorageRate);
7878
}
7979
extern "C" BOOL Session_DLStroage_Delete(LPCTSTR lpszClientAddr)
8080
{

0 commit comments

Comments
 (0)