void connect() { while (true) { try { using (var client = new CertClient(_gatewayAddress, _gatewayClientCert)) { client.WriteServiceData(new GatewayCommand { Type = CommandType.ListenFileChange, Content = _dict.Keys.ToJsonString() }); client.ReadTimeout = 60000; while (true) { var ret = client.ReadServiceObject <InvokeResult <string> >(); if (ret.Data != null) { string filepath = ret.Data; _logger?.LogInformation("文件映射系统收到新的文件:{0}", filepath); int len = client.ReadInt(); var data = client.ReceiveDatas(len); try { var item = _dict[filepath]; string localpath = item.LocalPath; File.WriteAllBytes(localpath, data); if (item.Callback != null) { item.Callback(filepath, localpath); } } catch (Exception ex) { _logger?.LogError(ex, ex.Message); } } client.WriteServiceData(new InvokeResult()); } } } catch (Exception ex) { _logger?.LogError(ex, "文件监听异常"); } Thread.Sleep(1000); } }
/// <summary> /// 获取网关共享文件,并保存到本地 /// </summary> /// <param name="filepath">共享文件路径</param> /// <param name="localFilePath">保存到本地的路径</param> public void GetGatewayShareFile(string filepath, string localFilePath) { using (var client = new CertClient(_gatewayAddress, _gatewayClientCert)) { client.WriteServiceData(new GatewayCommand { Type = CommandType.GetShareFile, Content = filepath }); var ret = client.ReadServiceObject <InvokeResult <int?> >(); if (ret.Success == false) { throw new Exception(ret.Error); } int len = ret.Data.GetValueOrDefault(); var data = client.ReceiveDatas(len); File.WriteAllBytes(localFilePath, data); } }