-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from DigitalPlatform/master
将 On handler 代码放到单独线程中处理
- Loading branch information
Showing
3 changed files
with
211 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
| ||
// 新版 API,测试中 | ||
public Task<SearchResult> SearchAsync( | ||
string strRemoteUserName, | ||
SearchRequest request, | ||
TimeSpan timeout, | ||
CancellationToken token) | ||
{ | ||
return Task.Run<SearchResult>( | ||
() => | ||
{ | ||
// DateTime start_time = DateTime.Now; | ||
ResultManager manager = new ResultManager(); | ||
List<string> errors = new List<string>(); | ||
List<string> codes = new List<string>(); | ||
|
||
SearchResult result = new SearchResult(); | ||
if (result.Records == null) | ||
result.Records = new List<Record>(); | ||
|
||
if (string.IsNullOrEmpty(request.TaskID) == true) | ||
request.TaskID = Guid.NewGuid().ToString(); | ||
|
||
using (WaitEvents wait_events = new WaitEvents()) // 表示中途数据到来 | ||
{ | ||
using (var handler = HubProxy.On<SearchResponse>( | ||
"responseSearch", | ||
(responseParam | ||
// taskID, resultCount, start, records, errorInfo, errorCode | ||
) => | ||
{ | ||
if (responseParam.TaskID != request.TaskID) | ||
return; | ||
|
||
#if NO | ||
if (responseParam.Records != null) | ||
{ | ||
foreach (Record record in responseParam.Records) | ||
{ | ||
// 校验一下 MD5 | ||
if (string.IsNullOrEmpty(record.MD5) == false) | ||
{ | ||
string strMD5 = StringUtil.GetMd5(record.Data); | ||
if (record.MD5 != strMD5) | ||
{ | ||
record.RecPath = "testclient1 : 记录 '" + record.RecPath + "' Data 的 MD5 校验出现异常"; | ||
// throw new Exception("testclient1 : 记录 '" + record.RecPath + "' Data 的 MD5 校验出现异常"); | ||
} | ||
} | ||
|
||
record.Data = ""; // testing | ||
} | ||
} | ||
#endif | ||
|
||
// start_time = DateTime.Now; // 重新计算超时 | ||
|
||
// 装载命中结果 | ||
if (responseParam.ResultCount == -1 && responseParam.Start == -1) | ||
{ | ||
if (result.ResultCount != -1) | ||
result.ResultCount = manager.GetTotalCount(); | ||
//result.ErrorInfo = responseParam.ErrorInfo; | ||
//result.ErrorCode = responseParam.ErrorCode; | ||
result.ErrorInfo = StringUtil.MakePathList(errors, "; "); | ||
result.ErrorCode = StringUtil.MakePathList(codes, ","); | ||
|
||
wait_events.finish_event.Set(); | ||
return; | ||
} | ||
|
||
// TODO: 似乎应该关注 start 位置 | ||
if (responseParam.Records != null) | ||
AddLibraryUID(responseParam.Records, responseParam.LibraryUID); | ||
|
||
result.Records.AddRange(responseParam.Records); | ||
if (string.IsNullOrEmpty(responseParam.ErrorInfo) == false | ||
&& errors.IndexOf(responseParam.ErrorInfo) == -1) | ||
{ | ||
errors.Add(responseParam.ErrorInfo); | ||
result.ErrorInfo = StringUtil.MakePathList(errors, "; "); | ||
} | ||
if (string.IsNullOrEmpty(responseParam.ErrorCode) == false | ||
&& codes.IndexOf(responseParam.ErrorCode) == -1) | ||
{ | ||
codes.Add(responseParam.ErrorCode); | ||
result.ErrorCode = StringUtil.MakePathList(codes, ","); | ||
} | ||
|
||
// 标记结束一个检索目标 | ||
// return: | ||
// 0 尚未结束 | ||
// 1 结束 | ||
// 2 全部结束 | ||
int nRet = manager.CompleteTarget(responseParam.LibraryUID, | ||
responseParam.ResultCount, | ||
responseParam.Records == null ? 0 : responseParam.Records.Count); | ||
|
||
if (responseParam.ResultCount == -1) | ||
result.ResultCount = -1; | ||
else | ||
result.ResultCount = manager.GetTotalCount(); | ||
|
||
if (nRet == 2) | ||
wait_events.finish_event.Set(); | ||
else | ||
wait_events.active_event.Set(); | ||
})) | ||
{ | ||
MessageResult message = HubProxy.Invoke<MessageResult>( | ||
"RequestSearch", | ||
strRemoteUserName, | ||
request).Result; | ||
if (message.Value == -1 || message.Value == 0) | ||
{ | ||
result.ErrorInfo = message.ErrorInfo; | ||
result.ResultCount = -1; | ||
return result; | ||
} | ||
|
||
if (manager.SetTargetCount(message.Value) == true) | ||
return result; | ||
|
||
// start_time = DateTime.Now; | ||
|
||
try | ||
{ | ||
Wait( | ||
request.TaskID, | ||
wait_events, | ||
timeout, | ||
token); | ||
} | ||
catch (TimeoutException) | ||
{ | ||
// 超时的时候实际上有结果了 | ||
if (result.Records != null | ||
&& result.Records.Count > 0) | ||
{ | ||
result.ErrorCode += ",_timeout"; // 附加一个错误码,表示虽然返回了结果,但是已经超时 | ||
return result; | ||
} | ||
throw; | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
}, | ||
token); | ||
} |