Skip to content

Commit

Permalink
Merge pull request #45 from DigitalPlatform/master
Browse files Browse the repository at this point in the history
将 On handler 代码放到单独线程中处理
  • Loading branch information
renyh1013 committed Jun 3, 2016
2 parents 2cbfe74 + fba4e15 commit 8d6b13c
Show file tree
Hide file tree
Showing 3 changed files with 211 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<Compile Include="ResultManager.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="deleted.txt" />
<Content Include="readme.txt" />
</ItemGroup>
<ItemGroup>
Expand Down
128 changes: 59 additions & 69 deletions DigitalPlatform.MessageClient/MessageConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public Task<MessageResult> ConnectAsync(
// Connection.Error += Connection_Error;

if (this.Container != null && this.Container.TraceWriter != null)
Connection.TraceWriter = this.Container.TraceWriter;
Connection.TraceWriter = this.Container.TraceWriter;

// Connection.Credentials = new NetworkCredential("testusername", "testpassword");
Connection.Headers.Add("username", this.UserName);
Expand Down Expand Up @@ -839,82 +839,72 @@ public Task<SearchResult> SearchAsync(
// taskID, resultCount, start, records, errorInfo, errorCode
) =>
{
if (responseParam.TaskID != request.TaskID)
return;

#if NO
if (responseParam.Records != null)
{
foreach (Record record in responseParam.Records)
Task.Run(() =>
{
// 校验一下 MD5
if (string.IsNullOrEmpty(record.MD5) == false)
try
{
string strMD5 = StringUtil.GetMd5(record.Data);
if (record.MD5 != strMD5)
if (responseParam.TaskID != request.TaskID)
return;

// 装载命中结果
if (responseParam.ResultCount == -1 && responseParam.Start == -1)
{
record.RecPath = "testclient1 : 记录 '" + record.RecPath + "' Data 的 MD5 校验出现异常";
// throw new Exception("testclient1 : 记录 '" + record.RecPath + "' Data 的 MD5 校验出现异常");
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;
}
}

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);
// 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, ",");
}
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();
// 标记结束一个检索目标
// 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();
}
catch (Exception ex)
{
errors.Add("SearchAsync handler 内出现异常: " + ExceptionUtil.GetDebugText(ex));
result.ErrorInfo = StringUtil.MakePathList(errors, "; ");
wait_events.finish_event.Set();
}

if (nRet == 2)
wait_events.finish_event.Set();
else
wait_events.active_event.Set();
});
}))
{
MessageResult message = HubProxy.Invoke<MessageResult>(
Expand Down
151 changes: 151 additions & 0 deletions DigitalPlatform.MessageClient/deleted.txt
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);
}

0 comments on commit 8d6b13c

Please sign in to comment.