123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- using System;
- using System.Threading;
- using Vinno.IUS.Common.Network.Leaf;
- using Vinno.IUS.Common.Network.Tcp;
- using Vinno.IUS.Common.Network.Transfer;
- using Vinno.vCloud.Common.Storage.ObjectStorageInfo.Interface;
- using Vinno.vCloud.Common.Storage.ObjectStorageInfo.Solid;
- namespace Vinno.vCloud.Common.Storage.ObjectStorageInfo.SignatureClient.Connect
- {
- public class SimpleLeafAgency : ILeafAgency
- {
- private string _leafUrl = string.Empty;
- private ClientLeaf _clientLeaf = null;
- /// <summary>
- /// 构造LeafAgency
- /// </summary>
- /// <param name="leafUrl">服务地址</param>
- public SimpleLeafAgency(string leafUrl)
- {
- _leafUrl = leafUrl;
- }
- /// <summary>
- /// 构造LeafAgency
- /// </summary>
- /// <param name="storageServerUrl">服务地址</param>
- public SimpleLeafAgency(ClientLeaf clientLeaf)
- {
- _clientLeaf = clientLeaf;
- }
- /// <summary>
- /// 请求地址
- /// </summary>
- public string Url
- {
- get
- {
- return _leafUrl;
- }
- }
- /// <summary>
- /// 发送消息
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- public Message AgencySend(Message request)
- {
- return AgencyKeepSend(request);
- }
- /// <summary>
- /// 发送消息如果是网络异常则重发
- /// </summary>
- /// <param name="request"></param>
- /// <param name="tryTime"></param>
- /// <returns></returns>
- public Message AgencyKeepSend(Message request, int tryTime = 3)
- {
- var agencyLeaf = new ClientLeaf(new LeafIdContext(), LeafMode.Single, new TcpCreator(_leafUrl));
- try
- {
- if (!agencyLeaf.Online)
- {
- throw new NetWorkException($"AgencyLeaf is offline");
- }
- var token = agencyLeaf.ApplyToken();
- var result = agencyLeaf.Send(token, request);
- return result;
- }
- catch (Exception exception)
- {
- switch (exception.GetType().Name)
- {
- case "NetWorkException":
- case "CanNotConnectException":
- case "NotConnectedException":
- if (tryTime == 0)
- {
- return null;
- }
- Thread.Sleep(3000);
- return AgencyKeepSend(request, --tryTime);
- default:
- return null;
- }
- }
- finally
- {
- agencyLeaf?.Close();
- }
- }
- public void Dispose()
- {
- }
- }
- }
|